Created
October 20, 2012 02:22
-
-
Save adamgoucher/3921739 to your computer and use it in GitHub Desktop.
how to use webdriver with py.test fixtures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
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.
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
@dsisson-twist did you find a way to have a fixture passing the same browser session?