I have a conftest.py which initializes the firefox instance and opens the page. But as soon as I want to use in my test_functional.py by inheriting that fixture. I get a webdriver=None error. I am not sure how to pass driver variable the values from conftest.py. Can someone help? Thanks in advance.
#conftest.py
@pytest.fixture
def webdriver(request):
from selenium import webdriver
request.instance.driver = webdriver.Firefox()
request.instance.driver.get("http://localhost:8443/")
request.addfinalizer(request.instance.driver.quit)`
test_functional.py looks like:
# test_functional.py
import pytest
@pytest.mark.usefixtures("webdriver")
class TestFunction:
def test_username(self, webdriver):
self.driver = webdriver
elem = driver.find_element_by_id("username")
s = "pass"
print(s)`
The error I am getting is:
self = <test_functional.TestHighchar object at 0x03E30590>, **webdriver = None**
def test_series(self, webdriver):
self.driver = webdriver
> elem = driver.find_element_by_id("username")
E NameError: name 'driver' is not defined
test_functional.py:13: NameError
========================== 1 failed in 19.95 seconds ==========================
The value of a pytest fixture is the return value of its constructor function . Because your webdriver
fixture constructor function returns nothing, your test function receives nothing.
Simply return the instantiated driver from your fixture constructor function and it will be passed to your test function:
@pytest.fixture
def webdriver(request):
from selenium import webdriver
driver = webdriver.Firefox()
request.addfinalizer(driver.quit)
return driver
PS The use of @pytest.mark.usefixtures("webdriver")
is unnecessary. The fixture will be automatically constructed and provided to your test function because the webdriver
argument matches the fixture name.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.