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.waitForFunction passes an unexpected first argument to pageFunction since Puppeteer 13 #7836

Closed
timvandermeij opened this issue Dec 12, 2021 · 6 comments
Assignees
Labels

Comments

@timvandermeij
Copy link

timvandermeij commented Dec 12, 2021

Bug description

For Mozilla's PDF.js we use Puppeteer for our integration tests. The upgrade from Puppeteer 12.0.1 to 13.0.0 made one test fail and investigation showed that there is a regression in Puppeteer 13.

Steps to reproduce the problem:

  1. Install Puppeteer 12.0.1 with npm.
  2. Create a file called repro.js with the following minimal reproducer:
const puppeteer = require("puppeteer");
const dns = require("dns");

dns.setDefaultResultOrder("ipv4first");

async function reproduce() {
  const browser = await puppeteer.launch({
    product: "chrome",
    headless: false,
  });
  const page = await browser.newPage();
  await page.goto("https://nodejs.org/en/docs/");

  try {
    const selector = "body";
    await page.waitForFunction(
      (_selector) => document.querySelector(_selector) !== null,
      {},
      selector,
    );
  } catch (ex) {
    console.log(ex);
  }
  await browser.close();
}

reproduce();
  1. Run node repro.js and notice no errors being logged (browser starts and no output is logged). This is expected.
  2. Install Puppeteer 13.0.0 with npm.
  3. Run node repro.js and and notice that it fails all of a sudden, with the following logged exception:
Error: Evaluation failed: DOMException: Failed to execute 'querySelector' on 'Document': '[object HTMLDocument]' is not a valid selector.
    at eval (eval at waitForPredicatePageFunction (:3:23), <anonymous>:3:33)
    at eval (eval at waitForPredicatePageFunction (:3:23), <anonymous>:3:67)
    at onRaf (__puppeteer_evaluation_script__:50:35)
    at pollRaf (__puppeteer_evaluation_script__:43:15)
    at waitForPredicatePageFunction (__puppeteer_evaluation_script__:8:22)
    at ExecutionContext._evaluateInternal (/home/timvandermeij/Documenten/Ontwikkeling/pdf.js/Code/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:221:19)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async WaitTask.rerun (/home/timvandermeij/Documenten/Ontwikkeling/pdf.js/Code/node_modules/puppeteer/lib/cjs/puppeteer/common/DOMWorld.js:547:27)

This is unexpected because nothing in the changelogs indicates a change in behavior here. Moreover, the API documentation at https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#pagewaitforfunctionpagefunction-options-args clearly states, and even includes an example of, that pageFunction only gets the arguments we give it.

However, in Puppeteer 13 I found after debugging all of a sudden the first argument to pageFunction is the document object for some reason, and the other arguments are shifted, so the second argument is what used to be the first one in Puppeteer 12.0.1, et cetera. This is why it fails: the document.querySelector call gets the document object as argument now since Puppeteer 13 put that inside the _selector argument.

Note that this happens in both Chrome and Firefox, so it's not browser-specific.

Puppeteer version

13.0.0

Node.js version

17.1.0

npm version

8.1.3

What operating system are you seeing the problem on?

Linux

Relevant log output

(see above)

timvandermeij added a commit to timvandermeij/pdf.js that referenced this issue Dec 12, 2021
We use string arguments in all other places, so these two places are a
bit inconsistent in that sense. Moreover, it's just one argument now,
which makes it a bit easier to read and see what it does because we
don't have to pass the always-empty options argument anymore. Finally,
doing it like this ensures it works in all Puppeteer versions given
puppeteer/puppeteer#7836.
@simonhammes
Copy link

I am also experiencing a similar issue (trying to upgrade puppeteer to v13): GitHub Action Run

Element .edit-post-more-menu [aria-label="Options"] not found
Evaluation failed: DOMException: Failed to execute 'querySelectorAll' on 'Document': '[object HTMLDocument]' is not a valid selector.

@campersau
Copy link
Contributor

Looks like #7825 is now passing in the root as the first parameter to the predicate e.g. here

const success = await predicate(root, ...args);

const success = await predicate(root, ...args);

const success = await predicate(root, ...args);

const success = await predicate(root, ...args);

@neonidian
Copy link

I am also experiencing a similar issue (trying to upgrade puppeteer to v13): GitHub Action Run

Element .edit-post-more-menu [aria-label="Options"] not found
Evaluation failed: DOMException: Failed to execute 'querySelectorAll' on 'Document': '[object HTMLDocument]' is not a valid selector.

+1
Experiencing the same issue on upgrade to Puppeteer version 13. Had to revert back to version 12.

@OrKoN
Copy link
Collaborator

OrKoN commented Dec 14, 2021

Thanks for the report, I will take a look soon.

@OrKoN OrKoN self-assigned this Dec 14, 2021
OrKoN added a commit that referenced this issue Dec 14, 2021
The same predicate function is used by the waitForFunction
API that does not need the context element.

Issues: #7836
OrKoN added a commit that referenced this issue Dec 15, 2021
The same predicate function is used by the waitForFunction
API that does not need the context element.

Issues: #7836
@timvandermeij
Copy link
Author

Closing since this should be fixed by #7845 now and therefore hopefully part of a new release soon. Thanks @OrKoN for fixing this!

@slawiko
Copy link

slawiko commented Dec 20, 2021

@timvandermeij is there any ETA for release of this fix? By coincidence our project needs both fixes: this one and the one from 13.0.0 :)
UPD:
I see 13.0.1 released. Thanks a lot!

Kafva pushed a commit to Kafva/pdf.js that referenced this issue Jan 20, 2022
We use string arguments in all other places, so these two places are a
bit inconsistent in that sense. Moreover, it's just one argument now,
which makes it a bit easier to read and see what it does because we
don't have to pass the always-empty options argument anymore. Finally,
doing it like this ensures it works in all Puppeteer versions given
puppeteer/puppeteer#7836.
This was referenced May 30, 2022
This was referenced May 30, 2022
bh213 pushed a commit to bh213/pdf.js that referenced this issue Jun 3, 2022
We use string arguments in all other places, so these two places are a
bit inconsistent in that sense. Moreover, it's just one argument now,
which makes it a bit easier to read and see what it does because we
don't have to pass the always-empty options argument anymore. Finally,
doing it like this ensures it works in all Puppeteer versions given
puppeteer/puppeteer#7836.
rousek pushed a commit to signosoft/pdf.js that referenced this issue Aug 10, 2022
We use string arguments in all other places, so these two places are a
bit inconsistent in that sense. Moreover, it's just one argument now,
which makes it a bit easier to read and see what it does because we
don't have to pass the always-empty options argument anymore. Finally,
doing it like this ensures it works in all Puppeteer versions given
puppeteer/puppeteer#7836.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment