我有一个conftest文件来处理在pytest中运行测试时selenium驱动程序的安装和拆卸。我试图添加一个命令行选项来确定我是否运行本地内置的硒和Web驱动程序或远程硒服务器和驱动程序等...如何从pytest_addoptions访问pytest conftest中的命令行输入并将其用于夹具参数?
我已经添加了一个名为“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..
我然后要运行使用像这样的命令的测试套件: 蟒-m pytest -v --html = \结果\ testrunX.html --self-contained- html --url = https://myotherdomain.com.au/ --runenv = chrome_remote
到目前为止的url命令行选项的作品,我可以用它来覆盖url或让它使用默认值。
但我无法从runenv命令行选项获取值。在下面的if语句中,它总是默认为else语句。 runenv似乎并不具有价值,即使默认我为parser.addoption是“本地”
if runenv == 'remote':
BROWSERS = {'chrome_remote': DesiredCapabilities.CHROME}
else:
BROWSERS = {'chrome': DesiredCapabilities.CHROME}
我试着在pdb.trace(if语句之前把),所以我能看到什么是在runenv,但它只会告诉我这是一个功能,我似乎无法从它获得价值,这使我认为它没有得到充分填充。
我不确定如何调试conftest文件,因为输出通常不会出现在控制台输出中。有什么建议么? pytest_addoption实际上是否接受2个或更多的自定义命令行参数?
我使用Python的 3.5.3 Pytest 3.2.1 在VIRTUALENV在Windows 10
谢谢@Chanda那真棒!非常感谢你。我会尽快尝试一下。 – Roochiedoor
答案正是我所需要的。非常感谢。 – Roochiedoor