Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Fail on unhandled exception? #16355

Closed
tslocke opened this issue Aug 8, 2022 · 5 comments
Closed

[Question] Fail on unhandled exception? #16355

tslocke opened this issue Aug 8, 2022 · 5 comments

Comments

@tslocke
Copy link

tslocke commented Aug 8, 2022

If an unhandled JS exception happens in the browser, i.e. an error that would show up in the devtools console, does that cause the current test to fail?

If not, is there a way to make that happen?

Alternatively, I have a "something went wrong" error indicator that pops up in the UI. I know how to write an assertion that says the error indicator is not present, but that only applies at a particular moment in the test script.

Say I'm waiting for some action to complete, I also want to be watching to see if the error appears, and fail the test if it does. How can I code that? e.g. "wait for X to be visible" and simultaneously "fail if Y becomes visible"

Thanks

@tslocke
Copy link
Author

tslocke commented Aug 8, 2022

I figured out a way to do this. Not sure if it's good practice or not.

I run this as the first line of the test. The key is to wait for the error message, but not with an await, so the script can continue.

page.waitForSelector('.error-message').then(() => expect('But it did').toBe('Error bar not to show up'))

The strings in expect/to-be were just a way to have something meaningful in the report (Expected: "Error bar not to show up" Received: "But it did"). I would rather have a more semantic way to fail the test, e.g. assert(false, "Error message showed up") but I couldn't find anything like that.

@tslocke
Copy link
Author

tslocke commented Aug 8, 2022

Urgh. This only works for the failure case. If the test "passes", playwright complains that "Target closed" while still waiting for something.

@tslocke
Copy link
Author

tslocke commented Aug 8, 2022

OK I found page.on('pageerror', ...)

Was nice chatting to me : )

@rwoll rwoll added the triaging label Aug 8, 2022
@rwoll
Copy link
Member

rwoll commented Aug 8, 2022

Closing per above 😄 😁

Yes, page.on('pageerror') is the recommended approach.

@rwoll rwoll closed this as completed Aug 8, 2022
@karlhorky
Copy link
Contributor

karlhorky commented Apr 29, 2024

@rwoll @dgozman @mxschmitt would the Playwright team consider adding a config option for failing on error to Playwright?

I'm aware that the following code achieves "failing on error" to add to every test (also multiple tests at once via overriding the page fixture), but I think this would be widespread enough that some option like failOnPageError or failOnUnhandledException in the Playwright config would make sense.

page.on('pageerror', (error) => {
  throw error;
});

References:

Prior Art / Similar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants