Home > other >  playwright conditional page.waitForNavigation
playwright conditional page.waitForNavigation

Time:01-25

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 ?

CodePudding user response:

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
}

CodePudding user response:

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.

  •  Tags:  
  • Related