2017-09-07 32 views
0

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

回答

1

在這裏,你爲什麼做urlrunenv爲燈具?您可以使用它象下面這樣:

在你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') 

現在,無論你想訪問urlrunenv你只需要編寫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 

或者像在你的代碼中,

if os.getenv('runenv')== 'remote': 
    BROWSERS = {'chrome_remote': DesiredCapabilities.CHROME} 
else: 
    BROWSERS = {'chrome': DesiredCapabilities.CHROME} 

這裏,url和r unenv將被保存在OS環境變量中,並且您可以在任何地方無需夾具就可以訪問它os.getenv()

希望它能幫助您!

+0

謝謝@Chanda那真棒!非常感謝你。我會盡快嘗試一下。 – Roochiedoor

+0

答案正是我所需要的。非常感謝。 – Roochiedoor

1

BROWSERSconftest.py進口和導入時runenv被填充是一個函數。如果你想使用runenv作固定BROWSERS也必須是一個夾具:

@pytest.fixture(scope='session') 
def BROWSERS(runenv): 
    if runenv == 'remote': 
     return {'chrome_remote': DesiredCapabilities.CHROME} 
    else: 
     return {'chrome': DesiredCapabilities.CHROME} 
+0

謝謝@phd,我確實嘗試將BROWSERS變成一個fixture函數,但後來我在使用參數的瀏覽器函數時遇到了問題:@pytest.fixture(scope ='function',params = BROWSERS.keys()) '後面跟着'def browser(request):'''''''''''''''''''''''''''''''後來我用'def browser(request):'''''''''''''''''根據這裏的文檔,我認爲我應該可以在燈具定義https://docs.pytest.org/en/latest/builtin.html#fixtures-and-requests中使用「Name」,但不能完全工作它出 – Roochiedoor

0

好這樣一個概念證明它看起來像我的問題的主要部分後,我不能使用命令行選項來更改函數的輸出(夾具函數或非夾具函數),然後將其用作另一個請求夾具函數上動態參數的列表。在閱讀完它之後,看起來這是與加載夾具功能期間的處理順序有關的。除了玩metafunc之外,我幾乎嘗試了一切。

我嘗試過使用各種pytest.mark.fixture或params = section中的任何變體,他們不會產生可迭代列表(在某些情況下,我可以讓它給我整個列表但不能迭代)

我也嘗試了lazyfixture模型沒有成功。

我試着在夾具功能中使用字典作爲輸出。我在函數之外嘗試了它們,我嘗試了類,並在夾具函數中創建填充對象。我嘗試在params =中使用pytest.mark.getfixturevalue,我嘗試使用pytest.mark.use裝飾器,我嘗試了paramatize裝飾器。它們都不起作用。

它看起來似乎可能在這裏工作的唯一可能是這裏提出的替代解決方案,但實際上並沒有開發出來。 https://docs.pytest.org/en/latest/proposals/parametrize_with_fixtures.html

最後我決定包含所有的邏輯一體的大型燈具的功能,這似乎爲那一刻的工作,但不是最好我怎麼想這樣做,因爲不幸的是我不能有基於可變PARAMS我想測試瀏覽器的命令行條目。我不得不手動更新conftest文件,以確定我是否運行了一個或兩個瀏覽器,並在每個測試中都通過這兩個瀏覽器進行迭代。

# 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://mytestdomain.com.au/', help='target machine url') 
    parser.addoption('--runenv', action='store', default='remote', help='select remote or local') 


@pytest.fixture(scope='session') 
def url(request): 
    return request.config.getoption('url') 


@pytest.fixture(scope='session') 
def runenv(request): 
    return request.config.getoption('runenv') 

BROWSERS = { 
    # 'firefox': DesiredCapabilities.FIREFOX, 
    'chrome': DesiredCapabilities.CHROME 
} 

@pytest.fixture(scope='function', params=BROWSERS.keys()) 
def browser(request, runenv): 
    if request.param == 'firefox': 

     if runenv == 'local': 
      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 runenv == 'remote': 
      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='https://selenium.mytestserver.com.au/wd/hub', 
             desired_capabilities=firefox_capabilities, browser_profile=profile) 
     else: 
      b = webdriverwrapper.Firefox() 
    elif request.param == 'chrome': 
     if runenv == 'local': 
      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 runenv == '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='https://selenium.mytestserver.com.au/wd/hub', 
             desired_capabilities=desired_cap) 
     else: 
      b = webdriverwrapper.Chrome() 
    else: 
     b = webdriverwrapper.Chrome() 
    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 
相關問題