Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REGRESSION]: page.evaluate with a string no longer works unless wrapped in an IIFE #10819

Closed
trusktr opened this issue Dec 9, 2021 · 2 comments

Comments

@trusktr
Copy link

trusktr commented Dec 9, 2021

Context:

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

Code Snippet

    function add(...addends) {
      return addends.reduce(
        (accumulator, currentValue) => accumulator + currentValue
      );
    }

    const functionResult = await page.evaluate(`
      ${add.toString()}
      var result = add(1, 2, 3);
      Promise.resolve(result);
    `);

Describe the bug

I get the error:

    page.evaluate: SyntaxError: Unexpected token 'var'

I am trying to migrate from an older jest-playwright-preset setup to @playwright/test, in the process updating playwright. This code worked before.

@trusktr
Copy link
Author

trusktr commented Dec 9, 2021

What other info can I provide?

@dgozman
Copy link
Contributor

dgozman commented Dec 9, 2021

@trusktr I can repro this. I guess something has changed, but I am not sure we are going to fix this. I'd suggest to wrap your code in the IIFE, to have a scope to declare variables. Sorry for the inconvenience.

const functionResult = await page.evaluate(`(() => {
    ${add.toString()}
    var result = add(1, 2, 3);
    return Promise.resolve(result);
})()`);

@dgozman dgozman closed this as completed Dec 9, 2021
@trusktr trusktr changed the title [REGRESSION]: [REGRESSION]: page.evaluate with a string no longer works unless wrapped in an IIFE Jan 22, 2022
trusktr added a commit to docsifyjs/docsify that referenced this issue Jan 23, 2022
Playwright had some in-range breaking changes (regressions): microsoft/playwright#10819 and microsoft/playwright#11570

Playwright tests need to `await onceRendered()` after each route navigation to wait for render to finish before testing the state of the rendered DOM. Tests were flaky because they were looking for DOM before render finished (sometimes).

Additionally, this fixes one test that was failing only locally, but not in CI, due to a RegExp check against page.url() (not sure why it would differ on CI vs local, but now the URL is explicit).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants