I'm learning to write automatic tests using Selenium WebDriver with Python. I have couple of tests in WidgetArea
class, I would like to run them all in one instance of Firefox, I know that I need to specify setUp
and tearDown
as @classmethod
but I do not know what else should I change to achieve that goal? Code can be found below:
import unittest
from selenium import webdriver
class WidgetArea(unittest.TestCase):
@classmethod
def setUp(cls):
# create new firefox session
cls.driver = webdriver.Firefox()
cls.driver.implicitly_wait(30)
cls.driver.maximize_window()
# navigate to aplication page
cls.driver.get("http://demoqa.com/")
cls.driver.title
def test_widget_area(self):
elements = self.driver.find_elements_by_xpath("//div[@id='secondary']/aside")
self.assertEqual(4, len(elements))
def test_widget_list(self):
elements = self.driver.find_elements_by_xpath("//ul[@id='menu-widget']/li")
self.assertEqual(7, len(elements))
def test_interaction(self):
elements = self.driver.find_elements_by_xpath("//ul[@id='menu-interactions']/li")
self.assertEqual(5, len(elements))
@classmethod
def tearDown(cls):
cls.driver.quit()
if __name__ == '__main__':
unittest.main
I don't use unittests
with selenium
, so I can advise tests in specific framework which you can adjust and update as you like despite of unittests
rules:
from selenium import webdriver
class WidgetArea():
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.driver.maximize_window()
self.driver.get("http://demoqa.com/")
def tearDown(self):
self.driver.quit()
def widget_area(self):
elements = self.driver.find_elements_by_xpath("//div[@id='secondary']/aside")
try:
assert len(elements) == 4
print("Test pass")
except AssertionError:
print("Assertion failed")
def widget_list(self):
elements = self.driver.find_elements_by_xpath("//ul[@id='menu-widget']/li")
try:
assert len(elements) == 7
print("Test pass")
except AssertionError:
print("Assertion failed")
def interaction(self):
elements = self.driver.find_elements_by_xpath("//ul[@id='menu-interactions']/li")
try:
assert len(elements) == 5
print("Test pass")
except AssertionError:
print("Assertion failed")
def main(self):
self.setUp()
self.widget_area()
self.widget_list()
self.interaction()
self.tearDown()
if __name__ == '__main__':
new = WidgetArea()
new.main()
All assertions will be performed during single browser session.