Unit test case not working in python selenium - python

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")

Related

Error: AttributeError: 'WebElement' object has no attribute 'Clear' in Selenium with Python

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()

Not able to launch browser using python selenium

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")

ran 0 tests in 0.000s - while executing Python-unittest with Selenium

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
class loginAvaliador(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome('/Users/r13/dev/chromedriver')
def login_avaliador(self):
driver = self.driver
driver.get("http://d3dyod5mwyu6xk.cloudfront.net/")
assert "FGV" in driver.title
cpf = driver.find_element_by_xpath('//input[#placeholder="CPF"]')
cpf.send_keys("27922797885")
password = driver.find_element_by_xpath('//input[#placeholder="SENHA"]')
password.send_keys("enccejaregular")
login = driver.find_element_by_tag_name('button')
login.click()
assert "No results found." not in driver.page_source
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main()
when i try to run this test script it returns "ran 0 tests in 0.000s" why this is happening? i'm new to python and writting this test scripts so i can't find the error
With the unittest module, you need to use their built in assertion methods. Instead of just writing
assert a not in b
you write
self.assertNotIn("No results found.",driver.page_source)
for example. For a list of all the assert methods look here:
https://docs.python.org/2/library/unittest.html#unittest.TestCase
While working through Python's unittest module with Selenium you have to consider a few facts as follows :
You need to take care of the indents. Indentation for class and test_method are different.
While you define the #Tests name the tests starting with test e.g.
def test_login_avaliador(self):
Always invoke driver.quit() within tearDown(){} method to close & destroy the WebDriver and Web Client instances gracefully.
Here is your own code with the required minor modifications :
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
class loginAvaliador(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome(executable_path=r'C:\WebDrivers\chromedriver.exe')
def test_login_avaliador(self):
driver = self.driver
driver.get("http://d3dyod5mwyu6xk.cloudfront.net/")
assert "FGV" in driver.title
cpf = driver.find_element_by_xpath('//input[#placeholder="CPF"]')
cpf.send_keys("27922797885")
password = driver.find_element_by_xpath('//input[#placeholder="SENHA"]')
password.send_keys("enccejaregular")
login = driver.find_element_by_tag_name('button')
login.click()
assert "No results found." not in driver.page_source
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main()
Execution Result:
C:\path\to\PyPrograms>python -m unittest 52560471_unittest.py
DevTools listening on ws://127.0.0.1:12022/devtools/browser/078fc4e9-3ca6-4bbb-b318-0b8f04318d32
.
----------------------------------------------------------------------
Ran 1 test in 40.796s
OK

Python Selenium 'module' object is not callable in python selenium script

Learning Selenium driven by Python and in my practice I keep getting the following error. I am stuck and could use some guidance
Traceback (most recent call last): File "test_login.py", line 14, in
test_Login
loginpage = homePage(self.driver) TypeError: 'module' object is not callable
Here is my code
test_login.py
import unittest
import homePage
from selenium import webdriver
class Login(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.get("https://hub.docker.com/login/")
def test_Login(self):
loginpage = homePage(self.driver)
loginpage.login(email,password)
def tearDown(self):
self.driver.close()
if __name__ == '__main__': unittest.main()
homePage.py
from selenium.webdriver.common.by import By
class BasePage(object):
def __init__(self, driver):
self.driver = drive
class LoginPage(BasePage):
locator_dictionary = {
"userID": (By.XPATH, '//input[#placeholder="Username"]'),
"passWord": (By.XPATH, '//input[#placeholder="Password"]'),
"submittButton": (By.XPATH, '//button[text()="Log In"]'),
}
def set_userID(self, id):
userIdElement = self.driver.find_element(*LoginPage.userID)
userIdElement.send_keys(id)
def login_error_displayed(self):
notifcationElement = self.driver.find_element(*LoginPage.loginError)
return notifcationElement.is_displayed()
def set_password(self, password):
pwordElement = self.driver.find_element(*LoginPage.passWord)
pwordElement.send_keys(password)
def click_submit(self):
submitBttn = self.driver.find_element(*LoginPage.submitButton)
submitBttn.click()
def login(self, id, password):
self.set_password(password)
self.set_email(id)
self.click_submit()
Any help is appreciated
I think here:
loginpage = homePage(self.driver)
you meant to instantiate the LoginPage class:
loginpage = homePage.LoginPage(self.driver)

How do i run more than 1 Test Case in Python Webdriver. Only 1 of my test case class runs

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:

Categories