I am trying to inherit browser class in test. Can someone please point out what am i doing wrong here. I am new to python
This is my test class where I am trying to inherit browser class
import unittest
from Configurations.Browser import Browser
class GoogleTest(Browser):
def test_homepage(self):
driver = self.driver
self.driver.implicitly_wait(10) self.driver.find_element_by_xpath("/html/body/div/div[4]/form/div[2]/div[1]/div[1]/div/div[2]/input").send_keys("Test")
Browser.py:
import unittest
from selenium import webdriver
class Browser(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox(executablepath=r"C:\Setups\Selenium\Drivers\geckodriver.exe")
self.driver.implicitly_wait(10)
self.driver.maximize_window()
self.driver.get("https://www.google.com")
def tearDown(self):
if(self.driver != None):
self.driver.close()
self.driver.quit()
if __name__ == '__main__':
unittest.main()
You have to change the "excutablepath" to "executable_path".(Browser.py)
like this
self.driver = webdriver.Firefox(executable_path=r"C:\Setups\Selenium\Drivers\geckodriver.exe")
Related
I am getting this error while trying to run the below code in a demo site:
self.driver.find_element(By.XPATH, "//input[#id='txtUsername']").Clear()
"AttributeError: 'WebElement' object has no attribute 'Clear'"
I create a POM format with 2 .py files but I can't get the root cause of the error.
This is the main class:
import time
import unittest
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from POMProjectDemo.Pages.LoginPage import LoginPage
from POMProjectDemo.Pages.HomePage import HomePage
class LoginTest(unittest.TestCase):
#classmethod
def setUpClass(cls):
s = Service("C:/drivers/chromedriver.exe")
cls.driver = webdriver.Chrome(service=s)
cls.driver.implicitly_wait(10)
cls.driver.maximize_window()
def test_login_valid(self):
driver = self.driver
self.driver.get("https://opensource-demo.orangehrmlive.com/index.php/auth/login")
login = LoginPage(driver)
login.enter_username("Admin")
login.enter_password("admin123")
login.click_login()
#classmethod
def tearDownClass(cls):
cls.driver.close()
cls.driver.quit()
print("Test Completed")
This is the LoginPage:
from selenium.webdriver.common.by import By
class LoginPage:
def __init__(self, driver):
self.driver = driver
self.username_textbox_xpath = "//input[#id='txtUsername']"
self.password_textbox_cssSelector = "input[type='password']"
self.login_button_xpath = "//input[#value='LOGIN']"
def enter_username(self, username):
self.driver.find_element(By.XPATH, self.username_textbox_xpath).Clear()
self.driver.find_element(By.XPATH, self.username_textbox_xpath).send_keys(username)
def enter_password(self, password):
self.driver.find_element(By.CSS_SELECTOR, self.password_textbox_cssSelector).Clear()
self.driver.find_element(By.CSS_SELECTOR, self.password_textbox_cssSelector).send_keys(password)
def click_login(self):
self.driver.find_element(By.XPATH, self.login_button_xpath).Click()
Any thoughts on this error?
These methods are case sensitive
Wrong Case : .Click() and .Clear() // which you have used instead use
Correct Case : .click() and .clear()
I tries below unit test case and it doesnt open web browser and print directly "done" message.
from selenium import webdriver
import unittest
class GoogleSearch(unittest.TestCase):
# driver = None
#classmethod
def setUpClass(cls):
cls.driver = webdriver.Chrome(executable_path='../Drivers/chromedriver')
cls.driver.maximize_window()
def test_search(self):
self.driver.get('https://www.google.com')
self.driver.find_element_by_name("q").send_keys("facebook")
self.driver.implicitly_wait(10)
self.driver.find_element_by_name("btnI").click()
# driver.find_element_by_name("btnI").send_keys(Keys.ENTER)
#classmethod
def tearDownClass(cls):
# driver.implicitly_wait(5)
cls.driver.quit()
cls.print("test completed")
print("done")
After defining your unittest, you have to call it. Call the test with unittest.main().
from selenium import webdriver import unittest
class GoogleSearch(unittest.TestCase):
#classmethod
def setUpClass(cls):
cls.driver = webdriver.Chrome(executable_path='../Drivers/chromedriver')
cls.driver.maximize_window()
def test_search(self):
self.driver.get('https://www.google.com')
self.driver.find_element_by_name("q").send_keys("facebook")
self.driver.implicitly_wait(10)
self.driver.find_element_by_name("btnI").click()
# driver.find_element_by_name("btnI").send_keys(Keys.ENTER)
#classmethod
def tearDownClass(cls):
# driver.implicitly_wait(5)
cls.driver.quit()
cls.print("test completed")
if __name__ == '__main__':
unittest.main() # <- runs your unittest
print("done")
i would like to know why this code opens mozilla twice, and why it doesn´t close it when finishes. Furthermore, i don´t understand 100% why login is a class with a function, and not a function directly.
> import unittest
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class LoginDetails(object):
def __init__ (self):
self.driver = webdriver.Firefox()
def logindetails(self, username, password):
driver = self.driver
driver.maximize_window()
driver.get("https://miclaro.claro.com.ar/")
driver.implicitly_wait(30)
driver.find_element_by_id("_58_login_movil").send_keys(username)
driver.find_element_by_id("_58_password_movil").send_keys(password)
driver.find_element_by_id("btn-home-login").click()
# Login Success
class TestLogin(unittest.TestCase):
def setUp(self):
self.ld = LoginDetails()
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
def test_sr_Login(self):
self.ld.logindetails("user", "pass")
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testName']
unittest.main()
Thank you very much!
This is because you instantiate webdriver twice - once inside the TestCase and once inside the LoginDetails class.
Why the other answer is not entirely correct
The WebDriver should not be controlled by the LoginDetails class in this case. LoginDetails class is very close to a Page Object notation representation and, hence, should be given the driver "from outside". Plus, opening browser in one class and closing it in the other is making the code close to "Spaghetti".
Better solution
Control the webdriver from the TestCase class and "share" with the LoginDetails:
import unittest
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class LoginDetails(object):
def __init__ (self, driver):
self.driver = driver
def logindetails(self, username, password):
driver = self.driver
driver.maximize_window()
driver.get("https://miclaro.claro.com.ar/")
driver.implicitly_wait(30)
driver.find_element_by_id("_58_login_movil").send_keys(username)
driver.find_element_by_id("_58_password_movil").send_keys(password)
driver.find_element_by_id("btn-home-login").click()
class TestLogin(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.ld = LoginDetails(self.driver)
def test_sr_Login(self):
self.ld.logindetails("user", "pass")
def tearDown(self):
self.driver.close()
Firefox opens twice
In your test self.ld = LoginDetails() runs the __init__ function of LoginDetails() which in turn runs webdriver.Firefox() then you issue the same in the next line in the test case. That is why Firefox opens twice.
Firefox does not close
For the same reason as above Firefox is not closed. The tearDown of your test case only closes the instance of webdriver.Firefox() defined in the test case itself not the one opened via the __init__ function of the class.
Why LoginDetails is a class
LoginDetails is a class in this case to keep webdriver.Firefox() persistent throughout your code. If it would be a function you would open one Firefox session each time you run the function. Unless you specify webdriver.Firefox() outside the function and then pass it to the function.
Corrected Code
The following code uses the class functionality:
import unittest
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class LoginDetails(object):
def __init__ (self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
def logindetails(self, username, password):
self.driver.maximize_window()
self.driver.get("https://miclaro.claro.com.ar/")
self.driver.implicitly_wait(30)
self.driver.find_element_by_id("_58_login_movil").send_keys(username)
self.driver.find_element_by_id("_58_password_movil").send_keys(password)
self.driver.find_element_by_id("btn-home-login").click()
def __del__(self):
''' ADDED based on comment by alecxe '''
self.driver.close()
class TestLogin(unittest.TestCase):
def setUp(self):
self.ld = LoginDetails()
def test_sr_Login(self):
self.ld.logindetails("user", "pass")
def tearDown(self):
# driver is closed by LoginDetails
pass
if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testName']
unittest.main()
In my automation page object model script I have created 2 TestCases so far with some test cases, methods.
class LoginPage_TestCase(unittest.TestCase):
class AdministrationPage_TestCase(unittest.TestCase):
LoginPage has a test for testing a valid user login
AdministrationPage has 1 method so far add_Project (user can add a project after having logged in)
In the PyCharm editor I have AdministrationPage open. I click the green run icon to run the test case. I want to see if my method add_project works before i continue writing more methods.
When the test runs it runs the LoginPage Test Case and then it stops there.
How can i run the AdministrationPage Test Case?
Also If i wanted to run LoginPage Test Case first and then AdministrationPage to run when LoginPage has completed. How can i do this?
Thanks!
My code is snippet for LoginPage and AdministraionPage is as follows:
LoginPage_TestCase.py
from selenium import webdriver
from Pages import page
from Locators import locators
from Locators import element
class LoginPage_TestCase(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Ie("C:\QA\Automation\Python_projects\Selenium Webdriver\IEDriverServer_Win32_2.45.0\IEDriverServer.exe")
self.driver.get("http://riaz-pc.company.local:8080/clearcore")
self.login_page = page.LoginPage(self.driver)
self.driver.implicitly_wait(30)
def test_login_valid_user(self):
print "test_login_valid_user"
login_page = page.LoginPage(self.driver)
login_page.userLogin_valid()
login_page.isAdministration_present()
print login_page.isAdministration_present()
assert login_page.isAdministration_present(), "Administration link not found"
def test_login_invalid_user(self):
print "test_login_invalid_user"
#login_page = page.login(self.driver)
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main()
AdministrationPage_TestCase.py
import unittest
import time
from selenium import webdriver
from locators import locators
from locators import element
from Pages import page
from Pages.administrationPage import AdministrationPage
from Pages.page import LoginPage
class AdministrationPage_TestCase(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Ie("C:\QA\Automation\Python_projects\Selenium Webdriver\IEDriverServer_Win32_2.45.0\IEDriverServer.exe")
self.driver.get("http://riaz-pc.company.local:8080/clearcore")
self.login_page = page.LoginPage(self.driver)
print "I am here in setUp self.login_page = page.LoginPage(self.driver)"
self.driver.implicitly_wait(30)
def add_Project(self):
login_page = page.LoginPage(self.driver)
login_page.userLogin_valid()
administration_page = login_page.clickAdministration(self.driver)
administration_page.AdministrationPage.add_project()
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main()
AdministrationPage.py
from selenium.common.exceptions import NoSuchElementException
from Locators.locators import MainPageLocators
from Locators import locators
from Locators import element
from Locators.element import BasePageElement
class BasePage(object):
def __init__(self, driver):
self.driver = driver
class AdministrationPage(BasePage):
# Add a project, enter project name & description, save
def add_project(self):
add_project_button = self.driver.find_element(*MainPageLocators.addButton_project)
add_project_button.click()
project_name_textfield = self.driver.find_element(*MainPageLocators.projectName_textfield)
project_name_textfield.click()
project_name_textfield.clear()
project_name_textfield.sendkeys('LADEMO_IE_nn_')
project_description_textfield = self.driver.find_element(*MainPageLocators.projectDescription_textfield)
project_description_textfield.click()
project_description_textfield.clear()
project_name_textfield.sendkeys("LADEMO create a basic project test script - Selenium Webdriver/Python Automated test")
1) Your test methods should start with test_.
2) You should configure pycharm as:
I have a functional test 'y1.py' which I have exported from the selenium IDE. It looks like:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re
class Y1(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "https://www.yahoo.com/"
self.verificationErrors = []
self.accept_next_alert = True
def test_y1(self):
driver = self.driver
driver.get(self.base_url)
driver.find_element_by_link_text("Weather").click()
driver.save_screenshot('out11.png')
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
I am trying to call this directly from within a python/django function. while investigating this I came across: AttributeError 'module' object has no attribute 'runserver' in django, where Udi states:
Are you trying to run unitest and selenium from a view? You should
consider launching a second process for that.
How can I do this?
You can start django server as new process with subprocess module.