I am trying to create a session-scope fixture that creates and pass instance of the driver class for every single test class in my suite. I was expecting that the following code would work:
import pytest
from pages.home.home_page import HomeAdmin
from base.webdriver_factory import WebDriverFactory
@pytest.fixture(scope='session', autouse=True)
def startup(request):
print("SESSION SET UP")
wdf = WebDriverFactory("firefox")
driver = wdf.get_web_driver_instance() # returns driver instance
return driver
I was expecting to have acces to the driver instance from my test code:
from pages.home.home_page import HomeAdmin
import unittest
import pytest
@pytest.mark.usefixtures("startup")
class HomeAdminTest(unittest.TestCase):
@pytest.fixture(autouse=True)
def setup(self, startup):
print("TEST")
self.ha = HomeAdmin(self.driver)
def test_login(self):
print("test run")
Buy this results in an error:
@pytest.fixture(autouse=True)
def setup(self, startup):
print("TEST")
> self.ha = HomeAdmin(self.driver)
E AttributeError: 'HomeAdminTest' object has no attribute 'driver'
testcases\home\home_test.py:11: AttributeError
What I'm trying to achieve in general: Open browser only once for all tests (all classes and modules) and then run various other classes to manipulate with the same driver instance. (I know that this is not best practice for testing, but this is special case and I am going rather automate some processes than making real tests).
Thank you in advance,
Wojciech