I'm writing a test with playwright, and I'd like to know the most correct way to check if - after a press of a button - it emitted a redirect to the expected URL, in order to pass the test.
I've found this snippet but it gets stuck:
const [request] = await Promise.all([
// Waits for the next request with the specified url
page.waitForRequest('*URL*'),
// Triggers the request
page.click('button'),
]);
It first clicks on the button, the redirect happens, but then it keeps waiting for the request anyway.
Will waitForNavigation() work for you?
const [response] = await Promise.all([
// Waits for the main frame navigation and returns the main resource response
page.waitForNavigation({url: '*URL*'}),
// Triggers the request
page.click('button'),
]);
As waitForNavigation is deprecated, I recommend to use page.waitForURL(url).
More: https://playwright.dev/docs/api/class-page#page-wait-for-url
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With