I'm using python. Clicking a button takes me to the desired page 90% of the time; however, 10% of the time I get an error page. The desired page contains a known locator. My goal is to understand which of the two pages has loaded.
So my idea was to use
page.get_by_role("button", name="click").click() on the button, then
page.wait_for_load_state("domcontentloaded"), then check
page.locator('known_locator').count() to see if it's 0 or 1.
wait_for_load_state() doesn't seem to be working, so I'm forced to do
page.wait_for_selector('known_locator', timeout=....) after the button has been clicked, but this forces a needless wait for the timeout when the error page is loaded, which takes time. Is there a way to make
page.wait_for_load_state() work after a click?
This thread is trying to answer question "Is there a way to make `page.wait_for_load_state()` work after a click in Python?"
One strategy is to use PlayWright's internal assertions. Furthermore, if you want your test to not fail because the wrong page loaded, why not use a try/catch on the assertion and customize the error message? https://playwright.dev/java/docs/test-assertions
Yes, pw needs the next page to be loaded otherwise the locator cannot be activated and passed to assertions. I haven't played yet with not(); it could be interesting: https://playwright.dev/java/docs/api/class-locatorassertions#locator-assertions-not
As your test is flaky, did you manage to understand its root cause? Maybe the devs could do something and fix the bug.
opened bug here https://github.com/microsoft/playwright/issues/24125
Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email firstname.lastname@example.org.