【发布时间】:2018-02-15 17:12:03
【问题描述】:
在 pytest 中运行测试时,我有一个 conftest 文件来处理 selenium 驱动程序的设置和拆卸。我正在尝试添加一个命令行选项来确定我是运行本地内置的 selenium 和 web 驱动程序还是远程 selenium 服务器和驱动程序等...
我添加了一个名为“runenv”的命令行选项,我正在尝试从中获取通过命令行输入的字符串值,以确定系统是否应该运行本地或远程 webdriver 配置。这允许测试人员在他们自己的机器上进行本地开发,但也意味着我们可以编写测试脚本以作为构建管道的一部分在远程机器上运行。
我遇到的问题是下面文件中显示的 parser.addoption 未处理。它似乎没有返回我可以使用的值(无论是默认值还是通过命令行传递的值)。
我的 conftest.py 文件如下(*注意 url 和远程 IP 只是示例以涵盖公司隐私)
#conftest.py
import pytest
import os
import rootdir_ref
import webdriverwrapper
from webdriverwrapper import DesiredCapabilities, FirefoxProfile
#when running tests from command line we should be able to pass --url=www..... for a different website, check what order these definitions need to be in
def pytest_addoption(parser):
parser.addoption("--url", action="store", default="https://mydomain1.com.au")
parser.addoption("--runenv", action="store", default="local")
@pytest.fixture(scope='session')
def url(request):
return request.config.option.url
@pytest.fixture(scope='session')
def runenv(request):
return request.config.option.runenv
BROWSERS = {}
if runenv == 'remote':
BROWSERS = {'chrome_remote': DesiredCapabilities.CHROME}
else:
BROWSERS = {'chrome': DesiredCapabilities.CHROME}
# BROWSERS = {
# #'firefox': DesiredCapabilities.FIREFOX,
# # 'chrome': DesiredCapabilities.CHROME,
# 'chrome_remote': DesiredCapabilities.CHROME,
# # 'firefox_remote': DesiredCapabilities.FIREFOX
# }
@pytest.fixture(scope='function', params=BROWSERS.keys())
def browser(request):
if request.param == 'firefox':
firefox_capabilities = BROWSERS[request.param]
firefox_capabilities['marionette'] = True
firefox_capabilities['acceptInsecureCerts'] = True
theRootDir = os.path.dirname(rootdir_ref.__file__)
ffProfilePath = os.path.join(theRootDir, 'DriversAndTools', 'FirefoxSeleniumProfile')
geckoDriverPath = os.path.join(theRootDir, 'DriversAndTools', 'geckodriver.exe')
profile = FirefoxProfile(profile_directory=ffProfilePath)
# Testing with local Firefox Beta 56
binary = 'C:\\Program Files\\Mozilla Firefox\\firefox.exe'
b = webdriverwrapper.Firefox(firefox_binary=binary, firefox_profile=profile, capabilities=firefox_capabilities,
executable_path=geckoDriverPath)
elif request.param == 'chrome':
desired_cap = BROWSERS[request.param]
desired_cap['chromeOptions'] = {}
desired_cap['chromeOptions']['args'] = ['--disable-plugins', '--disable-extensions']
desired_cap['browserName'] = 'chrome'
desired_cap['javascriptEnabled'] = True
theRootDir = os.path.dirname(rootdir_ref.__file__)
chromeDriverPath = os.path.join(theRootDir, 'DriversAndTools', 'chromedriver.exe')
b = webdriverwrapper.Chrome(chromeDriverPath, desired_capabilities=desired_cap)
elif request.param == 'chrome_remote':
desired_cap = BROWSERS[request.param]
desired_cap['chromeOptions'] = {}
desired_cap['chromeOptions']['args'] = ['--disable-plugins', '--disable-extensions']
desired_cap['browserName'] = 'chrome'
desired_cap['javascriptEnabled'] = True
b = webdriverwrapper.Remote(command_executor='http://192.168.1.1:4444/wd/hub', desired_capabilities=desired_cap)
elif request.param == 'firefox_remote':
firefox_capabilities = BROWSERS[request.param]
firefox_capabilities['marionette'] = True
firefox_capabilities['acceptInsecureCerts'] = True
firefox_capabilities['browserName'] = 'firefox'
firefox_capabilities['javascriptEnabled'] = True
theRootDir = os.path.dirname(rootdir_ref.__file__)
ffProfilePath = os.path.join(theRootDir, 'DriversAndTools', 'FirefoxSeleniumProfile')
profile = FirefoxProfile(profile_directory=ffProfilePath)
b = webdriverwrapper.Remote(command_executor='http://192.168.1.1:4444/wd/hub',
desired_capabilities=firefox_capabilities, browser_profile=profile)
else:
b = BROWSERS[request.param]()
request.addfinalizer(lambda *args: b.quit())
return b
@pytest.fixture(scope='function')
def driver(browser, url):
driver = browser
driver.set_window_size(1260, 1080)
driver.get(url)
return driver
在 conftest 设置页面后,我的测试将简单地使用生成的“驱动程序”夹具。示例测试可能:
import pytest
from testtools import login, dashboard, calendar_helper, csvreadtool, credentials_helper
import time
@pytest.mark.usefixtures("driver")
def test_new_appointment(driver):
testId = 'Calendar01'
credentials_list = credentials_helper.get_csv_data('LoginDetails.csv', testId)
# login
assert driver.title == 'Patient Management cloud solution'
rslt = login.login_user(driver, credentials_list)
.... etc..
然后我想使用如下命令运行测试套件: python -m pytest -v --html=.\Results\testrunX.html --self-contained-html --url=https://myotherdomain.com.au/ --runenv=chrome_remote
到目前为止,url 命令行选项有效,我可以使用它来覆盖 url 或让它使用默认值。
但我无法从 runenv 命令行选项中获取值。在下面的 if 语句中,它将始终默认为 else 语句。 runenv 似乎没有值,即使我对该 parser.addoption 的默认值是 'local'
if runenv == 'remote':
BROWSERS = {'chrome_remote': DesiredCapabilities.CHROME}
else:
BROWSERS = {'chrome': DesiredCapabilities.CHROME}
我尝试在 if 语句之前放入 pdb.trace() 以便我可以看到 runenv 中的内容,但它只会告诉我它是一个函数,我似乎无法从中获取值,这让我觉得它根本没有被填充。
我不太确定如何调试 conftest 文件,因为输出通常不会出现在控制台输出中。有什么建议? pytest_addoption 实际上是否接受 2 个或更多自定义命令行参数?
我正在使用 Python 3.5.3 pytest 3.2.1 在 Windows 10 上的 VirtualEnv 中
【问题讨论】:
标签: python parameters pytest fixtures