首页 > 解决方案 > 编剧:如果http状态为500则停止

问题描述

我希望剧作家早点失败,并在一个 http 请求不是200或时停止302

def test_it(live_server):
    os.environ['DEBUG']='pw:api'
    with sync_playwright() as playwright:
        run_test_id(live_server, playwright)

def run_test_id(live_server, playwright):
    browser = playwright.webkit.launch(headless=False)
    context = browser.new_context()
    page = context.new_page()
    page.goto(live_server.url)
    page.click("text=Create Test Data")

不幸的是page.click()返回无,所以我不知道如何检查点击的 http 状态。

如何在 Playwright 中实现“异常 http 状态失败”?

标签: playwrightplaywright-python

解决方案


一个选项是使用page.on()

def handle_response(response):
    print(f"{response.status}")
    if response.status == 500:
        exit(1)

page.on("response", handle_request)

但是,这不仅会检查单击后的状态代码,还会检查请求的任何响应,例如,如果您page.goto()返回 500,它也会在此处停止。

(我更习惯用 JS 编写剧本,所以我的例子可能有点偏离。)


推荐阅读