Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if a pressed button performs a redirect to a correct page?

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.

like image 397
Ciccios_1518 Avatar asked Nov 07 '25 09:11

Ciccios_1518


2 Answers

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'),
]);
like image 103
Jason Hu Avatar answered Nov 09 '25 21:11

Jason Hu


As waitForNavigation is deprecated, I recommend to use page.waitForURL(url).

More: https://playwright.dev/docs/api/class-page#page-wait-for-url

like image 30
Mateusz Piwowarski Avatar answered Nov 09 '25 22:11

Mateusz Piwowarski