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

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

Related

Python selenium - How to run a Selenium Python Unittest

I'm having an issue with running python selenium for the first time :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import unittest
class segfam(unittest.TestCase):
def setUp(self):
self.driver=webdriver.chrome("/Users/tomersegal/Downloads/chromedriver")
def test_blabla(self):
driver=self.driver
driver.get("https://www.google.co.il/")
assert "Google" in driver.title
This is my error :
Ran 0 tests in 0.000s
OK
Launching unittests with arguments python -m unittest discover -s /Users/tomersegal/PycharmProjects/pythonProject1 -t /Users/tomersegal/PycharmProjects/pythonProject1 in /Users/tomersegal/PycharmProjects/pythonProject1
Process finished with exit code 0
Empty suite
As you are using unittest framework you have to call it from the __main__ function as:
if __name__ == "__main__":
unittest.main()
So your effective code block will be:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import unittest
class segfam(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Chrome("/Users/tomersegal/Downloads/chromedriver")
def test_blabla(self):
driver=self.driver
driver.get("https://www.google.co.il/")
assert "Google" in driver.title
if __name__ == "__main__":
unittest.main()
PS: Note the change of chrome to Chrome
References
You can find a couple of relevant detailed discussions in:
Python + WebDriver -- No browser launched while using unittest module
What is unittest in selenium Python?

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

Pytest fixture not able to quit the test after execution in python pytest

I am using below fixture in conftest.py for open and close browser :
conftest.py:
#pytest.fixture(scope='session')
def browser():
print("Setting up webdriver connection...")
global driver
if driver is None:
profile = webdriver.FirefoxProfile()
profile.accept_untrusted_certs = True
profile.set_preference("network.proxy.type",1)
driver = webdriver.Firefox(firefox_profile=profile)
driver.maximize_window()
yield driver
driver.quit()
print("Webdriver connection closed..")
In my Test Class I have below step test_login :
def test_login(self, browser):
login_page = pagep.LoginPage(browser)
login_page.login('hptest376#gmail.com', "test12345*")
login_page.login('user', "pwd")
assert "Sum" in browser.title
And after test_login I have one more test step in my Test Class:
def test_ppt(a):
a=12
print a
Issue : I am getting error in fixture at driver.quit() and browser is not closing.
If I remove the "test_ppt(a)" step after "test_login(self, browser)" then the test runs fine.
Want to know what i need to update in my fixture "browser()" so that driver.quit() is executed.
Changed the scope of fixture to 'function' and the test is passing now.
#pytest.fixture(scope='function')
def browser():

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:

How to to run unitest and selenium from a django view?

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.

Categories