how to prevent py.test run new webdriver each time when start new method

320 views Asked by At

I have very long test scenario where I should pass through checkout (few pages).
And firefox start on each method. But I need use first one (first) driver instance through all pages.

Question: Can I get and use only One driver instance through all methods?

class TestHomePage():

@pytest.yield_fixture(autouse=True)
def driver(self):
    _driver=webdriver.Firefox()
    _driver.maximize_window()
    yield _driver
    _driver.quit()

def test_title(self, driver):
    driver.get('http://website.com/index.html')
    assert('Home' in driver.title)
    sleep(0.5)

def test_item1(self, driver):
    ...

def test_item2(self, driver):
    ...
1

There are 1 answers

0
hpk42 On

You should be able to use yield_fixture(autouse=True, scope="session") to only create one instance throughout your tests. See https://pytest.org/latest/fixture.html#sharing-a-fixture-across-tests-in-a-module-or-class-session for some more information.