我有一個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