Roo*_*oor 4 python parameters fixtures pytest
我有一个 conftest 文件,用于在 pytest 中运行测试时处理 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
Run Code Online (Sandbox Code Playgroud)
在页面已经由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..
Run Code Online (Sandbox Code Playgroud)
然后,我想使用以下命令运行测试套件: 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}
Run Code Online (Sandbox Code Playgroud)
我尝试在 if 语句之前放入 pdb.trace() ,这样我就可以看到 runenv 中的内容,但它只会告诉我这是一个函数,而且我似乎无法从中获取值,这使得我认为它根本没有人口。
我不太确定如何调试 conftest 文件,因为输出通常不会出现在控制台输出中。有什么建议么?pytest_addoption 实际上接受 2 个或更多自定义命令行参数吗?
我在 Windows 10 上的 VirtualEnv 中使用 Python 3.5.3 Pytest 3.2.1
在这里,你为什么要制作url和runenv作为固定装置?您可以像下面这样使用它:
在你的conftest.py中
def pytest_addoption(parser):
parser.addoption('--url', action='store', default='https://mytestdomain.com.au/', help='target machine url')
parser.addoption('--runenv', action='store', default='remote', help='select remote or local')
def pytest_configure(config):
os.environ["url"] = config.getoption('url')
os.environ["runenv"] = config.getoption('runenv')
Run Code Online (Sandbox Code Playgroud)
现在,无论你想访问哪里url,runenv你只需要os.getenv('Variable_name')这样写:
@pytest.fixture(scope='function')
def driver(browser):
driver = browser
driver.set_window_size(1260, 1080)
driver.get(os.getenv('url'))
return driver
Run Code Online (Sandbox Code Playgroud)
或者像你的代码一样,
if os.getenv('runenv')== 'remote':
BROWSERS = {'chrome_remote': DesiredCapabilities.CHROME}
else:
BROWSERS = {'chrome': DesiredCapabilities.CHROME}
Run Code Online (Sandbox Code Playgroud)
这里,url和runenv将保存在操作系统环境变量中,您可以在任何地方访问它,而无需固定装置,只需通过os.getenv()
希望它能帮助你!
| 归档时间: |
|
| 查看次数: |
5934 次 |
| 最近记录: |