2017-09-07 33 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 
相关问题