6

A previous answer pointed me into the direction, on how to catch an async validation error or redirect. But in my scenario an iframe comes into play and it's keeping me busy, all day. Some pointers into the right direction would be really helpfull, since I can't get it right, eventhough the error states clearly what goes wrong.

The scenario's:

  1. An input is left empty. A button is clicked inside an iframe and a validation error is returned from an async request
  2. An input has a value. A button is clicked inside an iframe and the user is redirected to a page stating "yaay".

The error that is thrown in the second scenario states waitForFunction failed: frame got detached. Which makes sense since the frame no longer exists... I found the frame.isDetached() but that still keeps throwing the same error. What am I missing or how can I use this method to make it work?

let frame = (await page.frames())[0];
...
for (let action of actions) {
    if (action.type === '...') {
        // ...
    }

    if (action.type === 'click') {
        frame.click("#btn");        

        // works for scenario 1 
        await Promise.race([
            page.waitForNavigation({ waitUntil: "networkidle2" }),
            // ERROR THROWN HERE FOR SCENARIO 2
            // `waitForFunction failed: frame got detached`

            // the line below doesn't seem to work as well...
            // frame.isDetached() ? Promise.resolve() : frame.waitForSelector(".error")
            frame.waitForSelector(".error"),
        ]);

        if (await frame.$(".error")) {
            // there was an error
        } else {
            // the page changed
           if (await page.waitForFunction('document.querySelector("body").innerText === "finished"')) {
            // yeeey 
           }
        }
    }

}
Dalie
  • 626
  • 1
  • 7
  • 19
  • Why do you use the `Promise.race` instead of `Promise.all`? I'm not sure that will help, but really don't understand what for there `Promise.race`. – Yevhen Laichenkov Jul 11 '19 at 19:15
  • 1
    `race` means whichever promise resolves first. It is the key to the mechanism of detecting a page change vs a validation error returned from an async request. Changing it to `all` would break things, since it will never resolve on a page navigation, since the `.error` will never show up – Dalie Jul 12 '19 at 06:13
  • Did you ever resolve this? If so, how? – John Doherty Jan 05 '21 at 16:53

0 Answers0