Question

I have the following scenario:

Load Page A
Check if Element_A exists in Page A
Click Button_A
On Clicking Button_A, Page B is loaded
Check if Element_B exists in Page B and Click Button_B
and so on...

I have written my code like this:

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

class MyWebsiteTest(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()

    def test_element_a_in_page_a(self):
        driver = self.driver
        driver.get(PAGE_A)
        element = driver.get_element_by_id("element_a")
        self.assertIsNotNone(element,"Oops.")

    def load_page_b_if_previous_function_is_true(self):
        #WHAT DO I DO HERE?
        #I Need to run this function given test_element_a_in_page_a succeeds.

    def tearDown(self):
        self.driver.close()

if __name__ == "__main__":
    unittest.main()

How do I do this? How do I write load_page_b_if_element_a_in_page_a?

If I write the entire flow of tests in ONE function, then I can achieve it easily, but how do I do seperation of concerns? How do I divide the tasks into different functions without making firefox open multiple times?

Was it helpful?

Solution

Unit tests are independent from each other by design. The ordering can be changed, but they shouldn't depend on each other's results.

In your case, you need to put load_page_b_if_previous_function_is_true inside test_element_a_in_page_a.

In general, you will need to write a single test function for each Selenium scenario. You cannot split actions which depend on each other into several test cases.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top