• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

assets/H03-May-2022-6150

fixtures/H03-May-2022-

golden-chromium/H03-May-2022-

golden-firefox/H03-May-2022-

CDPSession.spec.tsH A D30-Mar-20223.8 KiB11480

EventEmitter.spec.tsH A D30-Mar-20225.3 KiB171130

NetworkManager.spec.tsH A D30-Mar-202219.2 KiB547506

README.mdH A D30-Mar-20223.5 KiB8860

accessibility.spec.tsH A D30-Mar-202216 KiB502456

ariaqueryhandler.spec.tsH A D30-Mar-202222 KiB613540

browser.spec.tsH A D30-Mar-20222.8 KiB8253

browsercontext.spec.tsH A D30-Mar-20227.2 KiB208165

chromiumonly.spec.tsH A D30-Mar-20225.8 KiB160128

click.spec.tsH A D30-Mar-202212 KiB347295

cookies.spec.tsH A D30-Mar-202216.8 KiB565521

coverage.spec.tsH A D30-Mar-202212.6 KiB316268

defaultbrowsercontext.spec.tsH A D30-Mar-20223 KiB11595

dialog.spec.tsH A D30-Mar-20222.2 KiB7547

diffstyle.cssH A D30-Mar-2022166 1411

drag-and-drop.spec.tsH A D30-Mar-20225.4 KiB138106

elementhandle.spec.tsH A D30-Mar-202218.9 KiB530443

emulation.spec.tsH A D30-Mar-202214.3 KiB421359

evaluation.spec.tsH A D30-Mar-202215.7 KiB477402

fixtures.spec.tsH A D30-Mar-20223.3 KiB9469

frame.spec.tsH A D30-Mar-202210.5 KiB282232

headful.spec.tsH A D30-Mar-202211.1 KiB328267

idle_override.spec.tsH A D30-Mar-20222.9 KiB9559

ignorehttpserrors.spec.tsH A D30-Mar-20224.6 KiB136100

input.spec.tsH A D30-Mar-202211.3 KiB344295

jshandle.spec.tsH A D30-Mar-202213.1 KiB402344

keyboard.spec.tsH A D30-Mar-202213.6 KiB409351

launcher.spec.tsH A D30-Mar-202232.1 KiB792694

mocha-utils.tsH A D30-Mar-20229 KiB336271

mouse.spec.tsH A D30-Mar-20227.8 KiB240205

navigation.spec.tsH A D30-Mar-202227.6 KiB777651

network.spec.tsH A D30-Mar-202227.6 KiB777650

oopif.spec.tsH A D30-Mar-20228.7 KiB281232

page.spec.tsH A D30-Mar-202266.6 KiB1,9481,683

queryselector.spec.tsH A D30-Mar-202217 KiB508441

requestinterception-experimental.spec.tsH A D30-Mar-202231.2 KiB874772

requestinterception.spec.tsH A D30-Mar-202227.8 KiB791692

screenshot.spec.tsH A D30-Mar-202211.3 KiB341295

target.spec.tsH A D30-Mar-202210.1 KiB295244

touchscreen.spec.tsH A D30-Mar-20221.7 KiB5031

tracing.spec.tsH A D30-Mar-20224.8 KiB157120

tsconfig.jsonH A D30-Mar-2022114 87

tsconfig.test.jsonH A D30-Mar-202294 76

waittask.spec.tsH A D30-Mar-202227.6 KiB790684

worker.spec.tsH A D30-Mar-20224.2 KiB127101

README.md

1# Puppeteer unit tests
2
3Unit tests in Puppeteer are written using [Mocha] as the test runner and [Expect] as the assertions library.
4
5## Test state
6
7We have some common setup that runs before each test and is defined in `mocha-utils.js`.
8
9You 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:
10
11- `puppeteer`: an instance of the Puppeteer library. This is exactly what you'd get if you ran `require('puppeteer')`.
12- `puppeteerPath`: the path to the root source file for Puppeteer.
13- `defaultBrowserOptions`: the default options the Puppeteer browser is launched from in test mode, so tests can use them and override if required.
14- `server`: a dummy test server instance (see `utils/testserver` for more).
15- `httpsServer`: a dummy test server HTTPS instance (see `utils/testserver` for more).
16- `isFirefox`: true if running in Firefox.
17- `isChrome`: true if running Chromium.
18- `isHeadless`: true if the test is in headless mode.
19
20If 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()`.
21
22If 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.
23
24The best place to look is an existing test to see how they use the helpers.
25
26## Skipping tests in specific conditions
27
28Tests 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`.
29
30There 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.
31
32There are also tests that assume a normal install flow, with browser binaries ending up in `.local-<browser>`, for example. Such tests are skipped with
33`itOnlyRegularInstall` which checks `BINARY` and `PUPPETEER_ALT_INSTALL` environment variables.
34
35[mocha]: https://mochajs.org/
36[expect]: https://www.npmjs.com/package/expect
37
38## Running tests
39
40Despite being named 'unit', these are integration tests, making sure public API methods and events work as expected.
41
42- To run all tests:
43
44```bash
45npm run unit
46```
47
48- **Important**: don't forget to first run TypeScript if you're testing local changes:
49
50```bash
51npm run tsc && npm run unit
52```
53
54- To run a specific test, substitute the `it` with `it.only`:
55
56```js
57  ...
58  it.only('should work', async function() {
59    const {server, page} = getTestState();
60    const response = await page.goto(server.EMPTY_PAGE);
61    expect(response.ok).toBe(true);
62  });
63```
64
65- To disable a specific test, substitute the `it` with `xit` (mnemonic rule: '_cross it_'):
66
67```js
68  ...
69  // Using "xit" to skip specific test
70  xit('should work', async function({server, page}) {
71    const {server, page} = getTestState();
72    const response = await page.goto(server.EMPTY_PAGE);
73    expect(response.ok).toBe(true);
74  });
75```
76
77- To run tests in non-headless mode:
78
79```bash
80HEADLESS=false npm run unit
81```
82
83- To run tests with custom browser executable:
84
85```bash
86BINARY=<path-to-executable> npm run unit
87```
88