python 剧作家和pytest,代码执行后,将再打开一个空白选项卡

j2cgzkjk  于 2023-09-29  发布在  Python
关注(0)|答案(1)|浏览(139)

打开了一个不创建任何内容的空白选项卡。它在等待超时,因为它没有执行任何步骤。之后,选项卡关闭,测试将失败。我有一个不同的文件pytest.ini,在那里我定义了addopts = --headed --slowmo=1000 --browser=chromium第二个浏览器为该上下文打开。

def navigate_to_edit_page():       
        with sync_playwright() as p:
            browser = p.chromium.launch(headless=False, slow_mo=1000)
            context = browser.new_context(storage_state="playwright/.auth/storage_login.json")
            page = context.new_page()
            BasicAuthentication.execute(page)
  
            page.goto("https://example.org/edit")
            page.locator("input#org_name").fill("This is a test!")
            page.get_by_role("button", name="Save").click()
            expect(page.locator(".notice")).to_have_text("Successful update")
            expect(page).to_have_title("New page")
            context.close()

我试着用其他方法而不是新页面和其他工作人员来改进这个功能。

k5hmc34c

k5hmc34c1#

您是否尝试过在context.new_page()中使用上下文管理器?

def navigate_to_edit_page():       
        with sync_playwright() as p:
            browser = p.chromium.launch(headless=False, slow_mo=1000)
            context = browser.new_context(storage_state="playwright/.auth/storage_login.json")
            with context.new_page () as page:
                BasicAuthentication.execute(page)
  
                page.goto("https://example.org/edit")
                page.locator("input#org_name").fill("This is a test!")
                page.get_by_role("button", name="Save").click()
                expect(page.locator(".notice")).to_have_text("Successful update")
                expect(page).to_have_title("New page")
            context.close()

相关问题