Search code examples
python-3.xselenium-webdriverpycharmpython-unittest

How do I transfer WebDriver to an imported test?


Main module tests and imported ones must be executed in the same browser window. But something goes wrong:

main_tests.py:

import unittest
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import import_test_set

class MainTests(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.url = "https://www.google.com/"
        s = Service("../chromedriver/chromedriver")
        cls.driver = webdriver.Chrome(service=s)
        cls.driver.maximize_window()

    def test_00_01_open_site(self):
        print('test_00_01_open_site')

        self.driver.get(self.url)
        self.assertIn("Google", self.driver.title)

    def test_00_02_imported_tests(self):
        print('test_00_02_imported_tests')

        imported_tests = import_test_set.ImportTestSet()
        imported_tests.set_driver(self.driver)
        suite = unittest.TestLoader().loadTestsFromModule(import_test_set)

        result = unittest.TextTestRunner(verbosity=2).run(suite)
        self.assertEqual(result.failures, [])

import_test_set.py:

import unittest
from selenium.webdriver.common.by import By

class ImportTestSet(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.i_wait = 5

    def set_driver(self, driver):
        self.driver = driver

    def test_00_02_01_enter(self):
        print('test_00_02_01_enter')

        self.driver.implicitly_wait(self.i_wait)
        a_enter = self.driver.find_element(By.LINK_TEXT, 'Log in')
        a_enter.click()
        page_title = self.driver.title
        self.assertEqual(page_title, 'Google')
   
    def runTest(self):
        pass

The execution of the test method test_00_02_01_enter, implemented in the module import_test_set.py, produces the following error:

File "C:\Users...\import_test_set.py", line 17, in test_00_02_01_enter self.driver.implicitly_wait(self.i_wait) AttributeError: 'ImportTestSet' object has no attribute 'driver'

I can’t understand why the driver is not passed to the imported test. what is wrong in the code?


Solution

  • To solve the problem about the driver attribute try with the following modifications.
    In the file import_test_set.py:

    • add a class attribute driver
    • the method set_driver() can be removed (stop to use it is enough)
    class ImportTestSet(unittest.TestCase):
    
        # ----> add the following attribute driver
        driver = None
    
        @classmethod
        def setUpClass(cls):
            cls.i_wait = 5
    
        # ----> remove or stop to use the method set_driver()
        #def set_driver(self, driver):
        #    self.driver = driver
    
        ...
    

    In the file main_tests.py modify the method test_00_02_imported_tests() as following:

    def test_00_02_imported_tests(self):
        print('test_00_02_imported_tests')
    
        # ---> remove the following instructions
        #imported_tests = import_test_set.ImportTestSet()
        #imported_tests.set_driver(self.driver)
    
        # ---> add the following instruction to set the attribute driver of ImportTestSet
        import_test_set.ImportTestSet.driver = self.driver
    
        suite = unittest.TestLoader().loadTestsFromModule(import_test_set)
        result = unittest.TextTestRunner(verbosity=2).run(suite)
        self.assertEqual(result.failures, [])