gpt4 book ai didi

python - 如何通过命令行将 firefox/chrome headless 模式传递给 pytest

转载 作者:行者123 更新时间:2023-12-04 16:45:01 27 4
gpt4 key购买 nike

我需要在 headless 模式下远程运行我的 selenium 测试用例。

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

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

对于 headless 模式,我需要在 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

最佳答案

看看这是否有帮助。 An example app

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()

关于python - 如何通过命令行将 firefox/chrome headless 模式传递给 pytest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58957699/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com