puppeteer/test
2022-11-29 19:50:58 +00:00
..
assets feat: add ability to collect JS code coverage at the function level (#9027) 2022-10-06 23:53:05 +02:00
fixtures chore: use strict typing in tests (#8524) 2022-06-15 12:09:22 +02:00
golden-chromium feat(chromium): roll to Chromium 108.0.5351.0 (r1056772) (#9153) 2022-10-25 12:55:19 +02:00
golden-firefox chore: add missing golden asset for firefox (#9171) 2022-10-27 09:33:38 +00:00
installation feat: use configuration files (#9140) 2022-10-21 15:09:21 +02:00
src chore: upgrade mitt (#9340) 2022-11-29 19:50:58 +00:00
.eslintrc.js chore: use braces in function bodies (#8525) 2022-06-15 12:42:21 +02:00
package.json chore: use test runner for installation tests (#9110) 2022-10-14 13:37:07 +02:00
README.md feat: separate puppeteer and puppeteer-core (#9023) 2022-10-05 14:17:03 +02:00
TestExpectations.json chore: mark a waitForFunction test as flaky in Firefox (#9323) 2022-11-24 13:13:03 +01:00
TestSuites.json chore: enable firefox tests on Mac (#9002) 2022-09-23 19:06:09 +02:00
tsconfig.json chore: refactor utils (#9053) 2022-10-06 10:27:14 +02:00

Puppeteer unit 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

Tests that are not expected to pass in Firefox can be skipped. You can skip an individual test by using itFailsFirefox rather than it. Similarly you can skip a describe block with describeFailsFirefox.

There is also describeChromeOnly and itChromeOnly which will only execute the test if running in Chromium. Note that this is different from describeFailsFirefox: the goal is to get any FailsFirefox calls passing in Firefox, whereas describeChromeOnly should be used to test behaviour that will only ever apply in Chromium.

There are also tests that assume a normal install flow, with browser binaries ending up in .local-<browser>, for example. Such tests are skipped with itOnlyRegularInstall which checks BINARY and PUPPETEER_ALT_INSTALL environment variables.

Running tests

  • To run all tests:
npm test
  • Important: don't forget to first build the code if you're testing local changes:
npm run build:test && npm test
  • To run a specific test, substitute the it with it.only:
  ...
  it.only('should work', async function() {
    const {server, page} = getTestState();
    const response = await page.goto(server.EMPTY_PAGE);
    expect(response.ok).toBe(true);
  });
  • To disable a specific test, substitute the it with xit (mnemonic rule: 'cross it'):
  ...
  // Using "xit" to skip specific test
  xit('should work', async function({server, page}) {
    const {server, page} = 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