puppeteer/test
Jack Franklin a4d12a2b21 chore: remove helper.promisify (#6100)
It was just re-exporting the built-in Node module so let's just import
from that directly.
2020-06-25 13:01:46 +02:00
..
assets chore: migrate tests to TypeScript (#6075) 2020-06-23 07:18:46 +02:00
fixtures chore: add Prettier (#5825) 2020-05-07 12:54:55 +02:00
golden-chromium feat(api): add page.emulateVisionDeficiency(type) (#5901) 2020-05-26 17:14:20 +02:00
golden-firefox test: add page.screenshot viewport clipping test (#5079) 2019-10-24 14:05:13 +02:00
accessibility.spec.ts chore: migrate tests to TypeScript (#6075) 2020-06-23 07:18:46 +02:00
assert-coverage-test.js chore: add Prettier (#5825) 2020-05-07 12:54:55 +02:00
browser.spec.ts chore: migrate tests to TypeScript (#6075) 2020-06-23 07:18:46 +02:00
browsercontext.spec.ts chore: migrate tests to TypeScript (#6075) 2020-06-23 07:18:46 +02:00
CDPSession.spec.ts chore: migrate tests to TypeScript (#6075) 2020-06-23 07:18:46 +02:00
chromiumonly.spec.ts chore: migrate tests to TypeScript (#6075) 2020-06-23 07:18:46 +02:00
click.spec.ts chore: migrate tests to TypeScript (#6075) 2020-06-23 07:18:46 +02:00
cookies.spec.ts chore: migrate tests to TypeScript (#6075) 2020-06-23 07:18:46 +02:00
coverage-utils.js chore: fix ESLint violation and lessen rule strictness (#6081) 2020-06-23 12:55:42 +01:00
coverage.spec.ts chore: declare toBeGolden to TypeScript (#6080) 2020-06-23 15:02:09 +01:00
defaultbrowsercontext.spec.ts chore: migrate tests to TypeScript (#6075) 2020-06-23 07:18:46 +02:00
dialog.spec.ts chore: migrate tests to TypeScript (#6075) 2020-06-23 07:18:46 +02:00
diffstyle.css Implement FrameManager 2017-06-21 14:11:52 -07:00
elementhandle.spec.ts chore: migrate tests to TypeScript (#6075) 2020-06-23 07:18:46 +02:00
emulation.spec.ts feat(api): remove emulateMedia method (#6084) 2020-06-23 16:27:37 +01:00
evaluation.spec.ts chore: migrate tests to TypeScript (#6075) 2020-06-23 07:18:46 +02:00
EventEmitter.spec.ts chore: migrate tests to TypeScript (#6075) 2020-06-23 07:18:46 +02:00
fixtures.spec.ts chore: migrate tests to TypeScript (#6075) 2020-06-23 07:18:46 +02:00
frame.spec.ts chore: migrate tests to TypeScript (#6075) 2020-06-23 07:18:46 +02:00
golden-utils.js chore: migrate tests to TypeScript (#6075) 2020-06-23 07:18:46 +02:00
headful.spec.ts chore: migrate tests to TypeScript (#6075) 2020-06-23 07:18:46 +02:00
ignorehttpserrors.spec.ts chore: migrate tests to TypeScript (#6075) 2020-06-23 07:18:46 +02:00
input.spec.ts chore: migrate tests to TypeScript (#6075) 2020-06-23 07:18:46 +02:00
jshandle.spec.ts chore: tidy up TODO from TS tests migration (#6090) 2020-06-25 13:01:36 +02:00
keyboard.spec.ts chore: migrate tests to TypeScript (#6075) 2020-06-23 07:18:46 +02:00
launcher.spec.ts chore: remove helper.promisify (#6100) 2020-06-25 13:01:46 +02:00
mocha-utils.ts chore: declare toBeGolden to TypeScript (#6080) 2020-06-23 15:02:09 +01:00
mouse.spec.ts chore: migrate tests to TypeScript (#6075) 2020-06-23 07:18:46 +02:00
navigation.spec.ts chore: fix ESLint violation and lessen rule strictness (#6081) 2020-06-23 12:55:42 +01:00
network.spec.ts chore: migrate tests to TypeScript (#6075) 2020-06-23 07:18:46 +02:00
oopif.spec.ts chore: migrate tests to TypeScript (#6075) 2020-06-23 07:18:46 +02:00
page.spec.ts chore: migrate tests to TypeScript (#6075) 2020-06-23 07:18:46 +02:00
queryselector.spec.ts chore: migrate tests to TypeScript (#6075) 2020-06-23 07:18:46 +02:00
README.md chore: add test configuration options for running tests against multiple products (#5964) 2020-06-12 15:56:11 +02:00
requestinterception.spec.ts chore: declare toBeGolden to TypeScript (#6080) 2020-06-23 15:02:09 +01:00
run_static_server.js chore: add Prettier (#5825) 2020-05-07 12:54:55 +02:00
screenshot.spec.ts chore: declare toBeGolden to TypeScript (#6080) 2020-06-23 15:02:09 +01:00
target.spec.ts chore: migrate tests to TypeScript (#6075) 2020-06-23 07:18:46 +02:00
touchscreen.spec.ts chore: migrate tests to TypeScript (#6075) 2020-06-23 07:18:46 +02:00
tracing.spec.ts chore: migrate tests to TypeScript (#6075) 2020-06-23 07:18:46 +02:00
tsconfig.json chore: migrate tests to TypeScript (#6075) 2020-06-23 07:18:46 +02:00
utils.js chore: migrate tests to TypeScript (#6075) 2020-06-23 07:18:46 +02:00
waittask.spec.ts chore: migrate tests to TypeScript (#6075) 2020-06-23 07:18:46 +02:00
worker.spec.ts chore: migrate tests to TypeScript (#6075) 2020-06-23 07:18:46 +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 utils/testserver for more).
  • httpsServer: a dummy test server HTTPS instance (see utils/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

Despite being named 'unit', these are integration tests, making sure public API methods and events work as expected.

  • To run all tests:
npm run unit
  • Important: don't forget to first run TypeScript if you're testing local changes:
npm run tsc && npm run unit
  • 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 tests in non-headless mode:
HEADLESS=false npm run unit
  • To run tests with custom browser executable:
BINARY=<path-to-executable> npm run unit