Skip to content
This repository has been archived by the owner on Jan 30, 2025. It is now read-only.

Commit

Permalink
Update examples to use Promise.all
Browse files Browse the repository at this point in the history
We can now use Promise.all when we interact with an element that causes
a navigation to occur, and so we need to also use waitForNavigation
before carrying on to the next steps in the tests. This helps prevent
race conditions between clicking and waiting.

Resolves: #467 (comment)
  • Loading branch information
ankur22 committed Aug 8, 2022
1 parent 41ae327 commit 886eda5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 29 deletions.
26 changes: 17 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
15 changes: 8 additions & 7 deletions examples/fillform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!',
});
Expand Down
26 changes: 17 additions & 9 deletions examples/locator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
12 changes: 8 additions & 4 deletions examples/locator_pom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 886eda5

Please sign in to comment.