Does Puppeteer run JavaScript on the page?

Viewed 141

I'm using Puppeteer for some testing tasks, and am a bit confused:
Does Puppeteer run the JavaScript it encounters? Specifically, our app fires of a bunch of requests (eg. loading Stripe, Facebook, etc), and I want to ensure that code is being run during a Puppeteer request.

1 Answers

Does Puppeteer run JavaScript on the page?

Yes, it does.

Does Puppeteer run the JavaScript it encounters?

Yes, it does.

JavaScript is enabled by default:

While Puppeteer's documentation does not explicitly say that JavaScript is enabled by default, their tutorial documentation does say that it launches Chrome in headless mode by default, and Chrome's headless mode has JavaScript enabled by default, therefore Puppeteer has JavaScript enabled by default).

You can enable or disable it with page.setJavaScriptEnabled(boolean): Promise<void>

https://pptr.dev/#?product=Puppeteer&version=v5.2.1&show=api-pagesetjavascriptenabledenabled

You can check if it is enabled with page.isJavaScriptEnabled(): boolean


Specifically, our app fires of a bunch of requests (eg. loading Stripe, Facebook, etc), and I want to ensure that code is being run during a Puppeteer request.

Use Puppeteer's Code Coverage feature to test that your JavaScript is actually executed, including the lines and functions you want to run.

This (unrelated) blog page has an example.

Related