cape
cape

Reputation: 434

handle puppeteer click and waitForNavigation promise on error 500

Scenario:

After Click on submit button a post navigation occurs, and 3 situations can happen:

  1. Ok. Got green image "img-ok"
  2. KO1. Navigation response is 200 and got red image "img-red-light"
  3. KO3. Navigation response is 500 and got red lock image "img-lock"

This is my code

await Promise.all([
            page.waitForNavigation({waitUntil: "networkidle0"}),
            page.click(BUTTON_SIGN)
        ]
        ).then (ok => {
            if(page.$("img-ok") != null) {
               //Case 1 
            } else { 
               //Case 2
            }
        }).catch (err => {
            //case 3
        })

When 3) happens, process does not goes through catch, instead it ends with default timeout.

How can I make it go through catch? Should I follow a different approach?

Upvotes: 0

Views: 911

Answers (1)

vsemozhebuty
vsemozhebuty

Reputation: 13792

I think this doc note for page.goto() may also be valid for page.waitForNavigation():

page.goto will not throw an error when any valid HTTP status code is returned by the remote server, including 404 "Not Found" and 500 "Internal Server Error". The status code for such responses can be retrieved by calling response.status().

So maybe try something like this:

const [response] = await Promise.all([
  page.waitForNavigation({waitUntil: "networkidle0"}),
  page.click(BUTTON_SIGN)
]);

if (response.status() === 500) {
  //Case 3 
} else if(page.$("img-ok") != null) { 
  //Case 1
} else { 
  //Case 2
}

Upvotes: 1

Related Questions