从命令行以 head 模式运行 pytest playwright 脚本

问题描述 投票:0回答:1

我正在尝试从命令行以有头模式运行一些脚本,但它始终以无头模式运行。问题是,当我直接从 pycharm IDE 运行该类时,它在 head 模式下成功运行,但使用以下任何设置从命令行运行时无法执行相同的操作。

我的 pytest.ini 文件如下

[pytest]
addopts = --html=report/report.html --self-contained-html --reruns 0 --reruns-delay 0 -v --browser chromium --headed --slowmo 2000

我有如下的conftest文件

def pytest_addoption(parser):
    parser.addoption("--headed", action="store_true", default=True, help="Run browser in headed mode")


    @pytest.fixture(scope="session")
    def playwright():
        with sync_playwright() as playwright_instance:
            yield playwright_instance
    
    
    @pytest.fixture(scope="session")
    def browser(playwright):
        browser = playwright.firefox.launch(headless=False)  # Set headless=True for headless mode
        yield browser
        browser.close()
    
    
    @pytest.fixture(scope="function")
    def browser_context(browser):
        context = browser.new_context()
        yield context
        context.close()
    
    
    @pytest.fixture(scope="function")
    def set_up_tear_down(browser_context):
        page = browser_context.new_page()
        page.set_viewport_size({"width": 1536, "height": 800})
        page.goto("https://www.saucedemo.com/")
        yield page
        page.close()

命令:

py -m pytest -v -s test_place_order.py --headed

是否需要修改任何隐藏配置才能使用命令行在 head 模式下运行测试?

python automation pytest playwright playwright-python
1个回答
0
投票
    @pytest.fixture(scope="session")
    def browser(playwright):
        headless_mode = request.config.getoption("--headed")
        browser = playwright.firefox.launch(headless=headless_mode)  # Set headless=True for headless mode
        yield browser
        browser.close()

[pytest]
addopts = 
    --html=report/report.html 
    --self-contained-html 
    --reruns 0 
    --reruns-delay 0 
    -v 
    --browser chromium 
    --headed=1
    --slowmo 2000
© www.soinside.com 2019 - 2024. All rights reserved.