[英]pytest, initialize webdriver for each test file with teardown quit
Current part of code in the test module:测试模块中的当前代码部分:
def test_01():
driver.get('https://www.google.com')
Conftest code:比赛代码:
import pytest
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
@pytest.fixture(autouse=True)
def browser():
driver = webdriver.Chrome(executable_path=r"C:\webdrivers\1\chromedriver.exe")
driver.implicitly_wait(5)
wait = WebDriverWait(driver, 10)
driver.maximize_window()
yield
driver.quit()
Result : "E NameError: name 'driver' is not defined"结果:“E NameError:未定义名称'驱动程序'”
Target result : initialize webdriver without class, setup webdriver as driver into each test function, run function with it and quit with fixtures postcondition from conftest.目标结果:在没有类的情况下初始化 webdriver,将 webdriver 作为驱动程序设置到每个测试函数中,使用它运行函数并使用来自 conftest 的装置后置条件退出。 I have a lot of test files and thats why I should to do it once.
我有很多测试文件,这就是为什么我应该做一次。
I've also tried return variable from fixture, but as I understood the test function still need to have variable for fixture and it looks wrong as for me.我也尝试过从固定装置返回变量,但据我所知,测试函数仍然需要有固定装置的变量,对我来说它看起来是错误的。 For example: fixture - return x, testfunction(fixture): x = fixture.
例如:fixture - return x, testfunction(fixture): x = fixture。 And it still not works with webdriver\\driver (or rather I didn't figure it out).
它仍然不适用于 webdriver\\driver(或者我没有弄清楚)。
Your test function needs to take the fixture as an argument, that's the first part of the problem.您的测试函数需要将夹具作为参数,这是问题的第一部分。
For instance:例如:
def test_01(driver):
driver.get('https://www.google.com')
But you don't have a driver
fixture yet, just one called browser
, so you'll need to change the name of the fixture:但是您还没有
driver
装置,只有一个名为browser
,因此您需要更改装置的名称:
@pytest.fixture(autouse=True)
def driver(request):
...
Finally, the fixture needs to return the driver, so that you can use it.最后,fixture 需要返回驱动程序,以便您可以使用它。
@pytest.fixture(autouse=True)
def driver(request):
...
yield driver
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.