Skip to content

Instantly share code, notes, and snippets.

@adamgoucher
Created October 20, 2012 02:22
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamgoucher/3921739 to your computer and use it in GitHub Desktop.
Save adamgoucher/3921739 to your computer and use it in GitHub Desktop.
how to use webdriver with py.test fixtures
from selenium.webdriver import Firefox
import pytest
class TestHighcharts(object):
@pytest.fixture(autouse=True)
def fixture(self, request):
request.instance.driver = Firefox()
request.instance.driver.get("http://localhost:9000")
def cleanup():
request.instance.driver.quit()
request.addfinalizer(cleanup)
def test_series(self):
s = self.driver.execute_script('return chart.series;')
print(s)
@hpk42
Copy link

hpk42 commented Oct 24, 2012

Using it like this gives no benefit over setup_method. But what about this:

# content of conftest.py


@pytest.fixture
def webdriver(request):
     from selenium.webdriver import firefox
     request.instance.driver = Firefox()
     request.instance.driver.get("http://localhost:9000")
     request.addfinalizer(request.instance.driver.quit)

And then test files can use it like this:

@pytest.mark.usefixtures("webdriver")
class TestHighchar:
    def test_series(self):
        s = self.driver.execute_script('return chart.series;')
        print(s)

This way all test classes wanting the web driver can declare its usage. Moreover, if you later decide you want to run against another browser, you can do so by parametrizing the "webdriver" fixture in conftest.py without having to change test code.

@dsisson-twist
Copy link

Will this fixture be in effect for all collected tests marked with "webdriver"? They will all get the same browser session? or will the browser be started before and killed after each test marked with "webdriver"?

I need to run some test of each time: some that share the same browser session, and some that require a clean session.

@mabouelfadl
Copy link

@dsisson-twist did you find a way to have a fixture passing the same browser session?

@glibincts
Copy link

Using it like this gives no benefit over setup_method. But what about this:

# content of conftest.py


@pytest.fixture
def webdriver(request):
     from selenium.webdriver import firefox
     request.instance.driver = Firefox()
     request.instance.driver.get("http://localhost:9000")
     request.addfinalizer(request.instance.driver.quit)

And then test files can use it like this:

@pytest.mark.usefixtures("webdriver")
class TestHighchar:
    def test_series(self):
        s = self.driver.execute_script('return chart.series;')
        print(s)

This way all test classes wanting the web driver can declare its usage. Moreover, if you later decide you want to run against another browser, you can do so by parametrizing the "webdriver" fixture in conftest.py without having to change test code.

Pretty clear and it works for me.

@salunkhe-ravi
Copy link

How bout this?:

#conftest.py

@pytest.fixture(scope="session")
def setup(request):
print("initiating chrome driver")
driver = webdriver.Chrome(ChromeDriverManager().install())
session = request.node
for item in session.items:
cls = item.getparent(pytest.Class)
setattr(cls.obj, "driver", driver)
yield driver
driver.quit()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment