Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playwright: Stop if http status is 500

I would like playwright to fail early, and stop as soon as one http request is not 200 or 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")

Unfortunately page.click() returns None, so I have no clue how I can check for the http status of the click.

How could I implement "Fail on unusual http status" in Playwright?

like image 373
guettli Avatar asked Sep 19 '25 06:09

guettli


1 Answers

An option is to use page.on():

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

page.on("response", handle_response)

This, however, doesn't check for only a status code after the click, but for any response for a request, so for example if your page.goto() returns 500, it will stop here as well.

(I'm more used to Playwright with JS, so my example might be a tiny bit off.)

like image 83
pavelsaman Avatar answered Sep 23 '25 14:09

pavelsaman