question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[REGRESSION]: context.pages() now works differently

See original GitHub issue

Context:

  • GOOD Playwright Version: [what Playwright version worked nicely?]
  • BAD Playwright Version: [what Playwright version doesn’t work any more?]
  • Operating System: [e.g. Windows, Linux or Mac]
  • Extra: [any specific details about your environment]

-GOOD Playwright 1.0.1

-BAD Playwright 1.2.0

-Operating System: Windows 10

Code Snippet

Help us help you! Put down a short code snippet that illustrates your bug and that we can run and debug locally. For example:

const {chromium, webkit, firefox} = require('playwright');

(async () => {
  const browser = await chromium.launch();
  const context = await browser.newContext();
  const page = await context.newPage();
 
await page.click('[name="Add new child window"]')

//This code worked back in 1.0.1
const allPages = await context.pages();
const childWindowPage = allPages[allPages.length - 1];

///////////////////////////////////////////////////////////////////

// Now you need to do it this way (1.2.0 version)

await page.waitForTimeout(5000);

const allPages = await context.pages();
const childWindowPage = allPages[allPages.length - 1];
})();

Describe the bug

For some reason in current (1.2.0) version you must wait till page is loaded, before you can grab it and assign it to a variable via context.pages() method… This wasnt the case in 1.0.1 version.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
dgozmancommented, Jul 8, 2020

The old snippet from 1.0.1 happened to work accidentally, because sometimes page.click() was resolving after the new page has been created. In 1.2, this happens less frequently - that’s why you see that snippet does not work anymore.

Overall, Playwright does not guarantee that by the time page.click() is resolved, all the pages opened by this click are available. Moreover, this is almost impossible to guarantee, because it is hard to determine whether the new page was indeed a result of a click or not, and whether the click will open a page after a timeout.

Instead, we recommend the following way to capture popups from clicks:

const [childWindowPage] = await Promise.all([
  page.waitForEvent('popup'),
  page.click('[name="Add new child window"]')
]);

// Wait for the child page to load before accessing the content.
await childWindowPage.waitForLoadState();

// Ready to use.
const title = await childWindowPage.title();

You can find even more samples in our documentation.

1reaction
dgozmancommented, Jul 15, 2020

@MihailPertsev The actual change in 1.2.0 is that page.click() returns earlier instead of taking long enough time for the new page to appear in context.pages().

It would indeed be handy if context.pages() would give you pages immediately after click, but we cannot guarantee that because:

  • click does not guarantee to create the new page instantly, for example it might do so after a timeout;
  • it is hard to determine whether the new page was indeed a result of a click or not, for example it might as well be a result of some fetch() finishing;
  • the new page is not initialized instantly in the browser and Playwright. Therefore, we cannot be sure that there is a page by the time page.click() resolves. The alternative would be to wait for undetermined amount of time in page.click(), but we cannot afford that.

Hopefully, this explanation helps a bit.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why Do I Get Different Results Each Time in Machine Learning?
That means, when the algorithm is given the same dataset, it learns the same model every time. An example is a linear regression...
Read more >
How To Interpret R-squared in Regression Analysis
Statisticians say that a regression model fits the data well if the differences between the observations and the predicted values are small and...
Read more >
Interactions in Multiple Linear Regression
Interaction: An interaction occurs when an independent variable has a different effect on the outcome depending on the values of another independent variable....
Read more >
Hypothesis Testing in the Multiple regression model - UCL
with the simple two variable regression model. • Now suppose we wish to test that a number of coefficients or ... the regression...
Read more >
Introduce config context to make original config and different ...
Set a default configuration context for the page request, ... config() calls in // API functions) will work with the context on the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found