diff --git a/README.md b/README.md index 70d3ff503..a45d90864 100644 --- a/README.md +++ b/README.md @@ -357,24 +357,32 @@ export default function () { const currentBet = page.locator("//p[starts-with(text(),'Your bet: ')]"); - // the tails locator clicks on the tails button by using the - // locator's selector. - tails.click(); + // In the following Promise.all the tails locator clicks + // on the tails button by using the locator's selector. // Since clicking on each button causes page navigation, - // waitForNavigation is needed. It's because the page + // waitForNavigation is needed -- this is because the page // won't be ready until the navigation completes. - page.waitForNavigation().then(() => { + // Setting up the waitForNavigation first before the click + // is important to avoid race conditions. + Promise.all([ + page.waitForNavigation(), + tails.click(), + ]).then(() => { console.log(currentBet.innerText()); }).then(() => { // the heads locator clicks on the heads button // by using the locator's selector. - heads.click(); - return page.waitForNavigation() + return Promise.all([ + page.waitForNavigation(), + heads.click(), + ]); }).then(() => { console.log(currentBet.innerText()); }).then(() => { - tails.click(); - return page.waitForNavigation() + return Promise.all([ + page.waitForNavigation(), + tails.click(), + ]); }).then(() => { console.log(currentBet.innerText()); }).finally(() => { diff --git a/examples/fillform.js b/examples/fillform.js index 906a009b4..c7aa8b89e 100644 --- a/examples/fillform.js +++ b/examples/fillform.js @@ -15,13 +15,14 @@ export default function() { // Enter login credentials and login page.$('input[name="login"]').type('admin'); page.$('input[name="password"]').type('123'); - }).then(() => - page.$('input[type="submit"]').click() - ).then(() => - // We expect the above form submission to trigger a navigation, so wait for it - // and the page to be loaded. - page.waitForNavigation() - ).then(() => { + // We expect the form submission to trigger a navigation, so to prevent a + // race condition, setup a waiter concurrently while waiting for the click + // to resolve. + return Promise.all([ + page.waitForNavigation(), + page.$('input[type="submit"]').click(), + ]); + }).then(() => { check(page, { 'header': page.$('h2').textContent() == 'Welcome, admin!', }); diff --git a/examples/locator.js b/examples/locator.js index d99f1ff12..6455dc49a 100644 --- a/examples/locator.js +++ b/examples/locator.js @@ -29,24 +29,32 @@ export default function () { const currentBet = page.locator("//p[starts-with(text(),'Your bet: ')]"); - // the tails locator clicks on the tails button by using the - // locator's selector. - tails.click(); + // In the following Promise.all the tails locator clicks + // on the tails button by using the locator's selector. // Since clicking on each button causes page navigation, - // waitForNavigation is needed. It's because the page + // waitForNavigation is needed -- this is because the page // won't be ready until the navigation completes. - page.waitForNavigation().then(() => { + // Setting up the waitForNavigation first before the click + // is important to avoid race conditions. + Promise.all([ + page.waitForNavigation(), + tails.click(), + ]).then(() => { console.log(currentBet.innerText()); }).then(() => { // the heads locator clicks on the heads button // by using the locator's selector. - heads.click(); - return page.waitForNavigation() + return Promise.all([ + page.waitForNavigation(), + heads.click(), + ]); }).then(() => { console.log(currentBet.innerText()); }).then(() => { - tails.click(); - return page.waitForNavigation() + return Promise.all([ + page.waitForNavigation(), + tails.click(), + ]); }).then(() => { console.log(currentBet.innerText()); }).finally(() => { diff --git a/examples/locator_pom.js b/examples/locator_pom.js index ac95dea9c..3b9537614 100644 --- a/examples/locator_pom.js +++ b/examples/locator_pom.js @@ -22,13 +22,17 @@ export class Bet { } heads() { - this.headsButton.click(); - return this.page.waitForNavigation(); + return Promise.all([ + this.page.waitForNavigation(), + this.headsButton.click(), + ]); } tails() { - this.tailsButton.click(); - return this.page.waitForNavigation(); + return Promise.all([ + this.page.waitForNavigation(), + this.tailsButton.click(), + ]); } current() {