selenium-webdriver - 如何防止每次启动新方法时 py.test 运行新的 webdriver

标签 selenium-webdriver pytest

我有很长的测试场景,我应该通过结帐(几页)。
和 firefox 启动每个方法。但是我需要在所有页面中使用第一个(第一个)驱动程序实例。

问题:我可以通过所有方法只获取和使用一个驱动程序实例吗?

类 TestHomePage():

@pytest.yield_fixture(autouse=True)
def driver(self):
    _driver=webdriver.Firefox()
    _driver.maximize_window()
    yield _driver
    _driver.quit()

def test_title(self, driver):
    driver.get('http://website.com/index.html')
    assert('Home' in driver.title)
    sleep(0.5)

def test_item1(self, driver):
    ...

def test_item2(self, driver):
    ...

最佳答案

您应该可以使用 yield_fixture(autouse=True, scope="session")在整个测试过程中只创建一个实例。见https://pytest.org/latest/fixture.html#sharing-a-fixture-across-tests-in-a-module-or-class-session了解更多信息。

关于selenium-webdriver - 如何防止每次启动新方法时 py.test 运行新的 webdriver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30914403/

相关文章:

django - 在 pytest-factoryboy 中参数化具有多表继承的 Django 模型的属性

java - org.openqa.selenium.WebDriverException 错误的根本原因是什么?

javascript - 无法使用 JavaScript 关闭 chrome 浏览器中的选项卡

javascript - Python Selenium - 如何单击没有框架的视频的全屏按钮

python - 按顺序执行pytest

python-3.x - 如何在当前测试中使用 fixture 转换后的数据进行参数化?

Python 和 pytest - 模拟 db 对象

Python CLIck,测试线程应用程序

python - 无法创建着色器缓存条目 - 在通过其 Css 选择器定位元素时出错

java - 如何使用 Selenium WebDriver java 从下拉列表中获取所有选项