puppeteer/test
2024-06-06 19:12:11 +00:00
..
assets test: use trace instead of log (#12504) 2024-05-29 18:37:52 +02:00
fixtures build: fix EsLint rule and add fixer (#11826) 2024-02-05 10:26:37 +01:00
golden-chrome fix: support clip in ElementHandle.screenshot (#12115) 2024-03-21 08:16:24 +00:00
golden-firefox fix: support clip in ElementHandle.screenshot (#12115) 2024-03-21 08:16:24 +00:00
installation chore(deps): Bump glob from 10.3.15 to 10.4.1 in the dependencies group (#12487) 2024-05-27 07:22:00 +00:00
src test: ignore favicon requests in the network test for redirects (#12541) 2024-06-06 19:12:11 +00:00
.eslintrc.js chore: fix interception tests (#12105) 2024-03-19 20:29:42 +01:00
CanaryTestExpectations.json test: update canary expectations (#12386) 2024-05-04 09:17:53 +00:00
package.json chore(deps-dev): Bump the dev-dependencies group across 1 directory with 12 updates (#12432) 2024-05-14 05:23:37 +02:00
README.md chore: rename mocha-runner (#10984) 2023-09-22 10:57:18 +02:00
TestExpectations.json test: ignore favicon requests in the network test for redirects (#12541) 2024-06-06 19:12:11 +00:00
TestSuites.json test: switch Chrome bidi to new headless (#12120) 2024-03-21 14:08:20 +01:00
tsconfig.json chore(deps-dev): Bump the dev-dependencies group with 13 updates (#11908) 2024-02-14 18:20:12 +00:00
tsdoc.json chore: update license headers (#11563) 2024-01-03 10:11:33 +00:00

Puppeteer tests

Unit tests in Puppeteer are written using Mocha as the test runner and Expect as the assertions library.

Test state

We have some common setup that runs before each test and is defined in mocha-utils.js.

You can use the getTestState function to read state. It exposes the following that you can use in your tests. These will be reset/tidied between tests automatically for you:

  • puppeteer: an instance of the Puppeteer library. This is exactly what you'd get if you ran require('puppeteer').
  • puppeteerPath: the path to the root source file for Puppeteer.
  • defaultBrowserOptions: the default options the Puppeteer browser is launched from in test mode, so tests can use them and override if required.
  • server: a dummy test server instance (see packages/testserver for more).
  • httpsServer: a dummy test server HTTPS instance (see packages/testserver for more).
  • isFirefox: true if running in Firefox.
  • isChrome: true if running Chromium.
  • isHeadless: true if the test is in headless mode.

If your test needs a browser instance, you can use the setupTestBrowserHooks() function which will automatically configure a browser that will be cleaned between each test suite run. You access this via getTestState().

If your test needs a Puppeteer page and context, you can use the setupTestPageAndContextHooks() function which will configure these. You can access page and context from getTestState() once you have done this.

The best place to look is an existing test to see how they use the helpers.

Skipping tests in specific conditions

To skip tests edit the TestExpectations file. See test runner documentation for more details.

Running tests

  • To run all tests applicable for your platform:
npm test
  • Important: don't forget to first build the code if you're testing local changes:
npm run build --workspace=@puppeteer-test/test && npm test

CLI options

Description Option Type
Do not generate coverage report --no-coverage boolean
Do not generate suggestion for updating TestExpectation.json file --no-suggestions boolean
Specify a file to which to save run data --save-stats-to string
Specify a file with a custom Mocha reporter --reporter string
Number of times to retry failed tests. --retries number
Timeout threshold value. --timeout number
Tell Mocha to not run test files in parallel --no-parallel boolean
Generate full stacktrace upon failure --fullTrace boolean
Name of the Test suit defined in TestSuites.json --test-suite string

Helpful information

  • To run a specific test, substitute the it with it.only:
  ...
  it.only('should work', async function() {
    const {server, page} = await getTestState();
    const response = await page.goto(server.EMPTY_PAGE);
    expect(response.ok).toBe(true);
  });
  • To disable a specific test, substitute the it with it.skip:
  ...
  it.skip('should work', async function({server, page}) {
    const {server, page} = await getTestState();
    const response = await page.goto(server.EMPTY_PAGE);
    expect(response.ok).toBe(true);
  });
  • To run Chrome headful tests:
npm run test:chrome:headful
  • To run tests with custom browser executable:
BINARY=<path-to-executable> npm run test:chrome:headless # Or npm run test:firefox