如何通过浏览器打开pytest函数来执行

时间:2017-05-30 21:54:25

标签: python selenium web-applications automated-tests pytest

我正在尝试使用pytest和selenium在python中编写Web应用程序的测试代码。 我的代码如下:

from selenium import webdriver
import pytest
import time

def test_OpenBE(browser):
    browser.get('urlremoved.com')

def test_navigateToSubmit(browser):
        browser.get('urlremoved.com')
        time.sleep(10)
        browser.find_element_by_id('button').click()

def test_Submission(browser):
        browser.get('urlremoved.com')
        browser.find_element_by_id('Name').send_keys("Name here")
        browser.find_element_by_id("ID").send_keys("123456")
        browser.find_element_by_id("email").send_keys("email@email.com")

我找到了以这种方式传递浏览器的示例。但是,当我尝试使用pytest运行它时,我收到了错误:

找不到夹具'浏览器'

  available fixtures: cache, capfd, capsys, doctest_namespace, monkeypatch, pytestconfig, record_xml_property, recwarn, tmpdir, tmpdir_factory
  use 'pytest --fixtures [testpath]' for help on them.

有谁知道如何实现这一点,以便测试可以按预期运行?

编辑:我下载了chromedriver并设置了一个变量

driver= webdriver.Chrome('chromedriver.exe')

这似乎已经取得了一些进展,现在我收到了一个错误:

ValueError:插件已注册:pytest_webdriver =)>

2 个答案:

答案 0 :(得分:3)

例如,您可以在调用浏览器之前初始化浏览器。

browser = webdriver.Chrome()

或者您通过在名为conftest.py的文件中理想地定义函数来自己制作一个夹具。您可以在pytest网站上阅读更多内容。这是一个例子:

@pytest.yield_fixture(scope='session')
def browser():
    driver = webdriver.Chrome()
    yield driver
    driver.quit()

答案 1 :(得分:2)

pytest正在寻找browser灯具。可能你没有声明这个夹具。你可以在这里阅读有关灯具的信息 - > https://docs.pytest.org/en/latest/fixture.html

你可以使用pytest-selenium plugin http://pytest-selenium.readthedocs.io/en/latest/index.html

但是,您可以尝试在同一个文件中实现类似的内容......

@pytest.fixture
def browser():
    "pytest fixture for browser"
     return pytest.config.getoption("-B")