-
-
Save adamgoucher/3921739 to your computer and use it in GitHub Desktop.
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) | |
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.
@dsisson-twist did you find a way to have a fixture passing the same browser session?
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()
Using it like this gives no benefit over setup_method. But what about this:
And then test files can use it like this:
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.