Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

playwright conditional page.waitForNavigation

When automating a login for, I perform click on an element that it could lead to navigation to another page but in case of invalid password it will just display error message on the same page without actually causing a navigation.

I have tried:

await Promise.all([
      this.page.submitButton.click(),
      this.page.waitForNavigation()
    ]); 

But it will fail for the case when no navigation happens. I was thinking about interception the response, to check if the login succeed/failed, but may be there is a more clear way to do so ?

like image 711
Zaky Avatar asked Oct 18 '25 02:10

Zaky


2 Answers

This code should give you an example of how to understand if you stay on the same page or navigate:

await this.page.submitButton.click();

try {
    await this.page.waitForNavigation();
    // if we are here it means login was successful
} catch(e) {
    // if we are here it means login failed
}
like image 104
Vadim Avatar answered Oct 20 '25 16:10

Vadim


I don't think you need some extra code besides this.page.submitButton.click().
The click function will wait for the navigation for you. So you could check after the click if your are in the same URL or not.

await this.page.submitButton.click(); // This will wait for a possible navigation

if(this.page.url() == destinationOnValidLogin) {
 // Good login stuff
} else {
 // Login failed
}
like image 44
hardkoded Avatar answered Oct 20 '25 16:10

hardkoded



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!