如何通过命令行在pytest中使用Firefox/Chrome无头模式

5
我可以帮您进行翻译。以下为您需要翻译的内容:

我需要在无头模式下远程运行我的Selenium测试用例。

目前,我正在运行以下py.test命令:

py.test --driver remote --host selenium.host --port 4444 --capability browserName firefox --capability platform LINUX

对于无头模式,我需要在conftest.py文件中处理。

但我需要将该选项放在命令行中,而不是在conftest.py文件中处理。

@pytest.fixture
def chrome_options(chrome_options):
    chrome_options.add_argument('--headless')
    return chrome_options


@pytest.fixture
def firefox_options(firefox_options):
    firefox_options.add_argument('-headless')
    return firefox_options
1个回答

1

看看这个是否有帮助。一个示例应用程序

def pytest_addoption(parser):
    parser.addoption("--browser", action="store", default="firefox", help="Type in browser type")
    parser.addoption("--executor", action="store", default="standalone", help="For selenium grid.")
    parser.addoption("--url", action="store", default="http://the-internet.herokuapp.com", help="url")




@pytest.fixture(scope="function")
def open_browser(request):
    browser = request.config.getoption("--browser")
    executor = request.config.getoption("--executor")

    if executor == "local" or executor == "" or executor == "standalone":
        if browser == 'chrome':
            driver = webdriver.Chrome()
        else:
            driver = webdriver.Firefox()
    else:
        if executor == "remote":
            command_executor = 'http://localhost:4444/wd/hub'
        else:
            command_executor = 'http://' + executor + '/wd/hub'  ## Expecting IP and Port. Eg. 1.1.1.1:4444

        caps = {'browserName': os.getenv('BROWSER', browser)}
        driver = webdriver.Remote(
            command_executor=command_executor,
            desired_capabilities=caps)

    driver.implicitly_wait(10)
    driver.maximize_window()

    yield driver # Teardown
    driver.close()
    driver.quit()


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接