简体   繁体   English

pytest,使用拆卸退出为每个测试文件初始化 webdriver

[英]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.

相关问题 如何在pytest中完成每次测试后完全拆解烧瓶应用程序? - How to Completely Teardown Flask App After Each Test in pytest? 如果退出调试,执行 pytest fixture teardown - Execute pytest fixture teardown if debugging is quit 如何在另一个文件 test_1.py 中处理 pytest 的设置和拆卸方法中生成的 session - How to handle session which is generated in the setup and teardown methods in pytest in another file test_1.py 在python pytest中执行后,pytest固定装置无法退出测试 - Pytest fixture not able to quit the test after execution in python pytest pytest-如何将特定的设置和拆卸功能链接到特定的测试功能? - Pytest - how to link a specific setup and teardown functions to a specific test function? 如何在“ pytest”框架的夹具中的“拆卸”中使用测试结果 - How to use test result in “teardown” in fixtures in “pytest” framework 在pytest中执行teardown_method后测试失败 - Failing a test after teardown_method has executed in pytest 如何在测试失败时拆解 pytest 中间结果? - How to do a teardown of pytest intermediate results on test fail? 如何使用每个模块的测试数据初始化数据库? pytest-django - How to initialize the database with your test data for each module? Pytest-django pytest 中单例类的拆解 - teardown of a singleton class in pytest
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM