• 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

README.mdH A D30-Mar-20223.5 KiB8860

accessibility.spec.tsH A D30-Mar-202215.9 KiB501456

ariaqueryhandler.spec.tsH A D30-Mar-202221.4 KiB595523

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.1 KiB353300

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

coverage.spec.tsH A D30-Mar-202211.2 KiB281235

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

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

diffstyle.cssH A D30-Mar-2022166 1411

elementhandle.spec.tsH A D30-Mar-202216.3 KiB454382

emulation.spec.tsH A D30-Mar-202214 KiB412352

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 KiB271225

headful.spec.tsH A D30-Mar-202210.1 KiB296238

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-20229.7 KiB301252

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

launcher.spec.tsH A D30-Mar-202229.2 KiB719634

mocha-utils.tsH A D30-Mar-20228.4 KiB310248

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

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

network.spec.tsH A D30-Mar-202221.6 KiB611508

oopif.spec.tsH A D30-Mar-20222.2 KiB7546

page.spec.tsH A D30-Mar-202259.7 KiB1,7491,508

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

requestinterception.spec.tsH A D30-Mar-202226.5 KiB747651

screenshot.spec.tsH A D30-Mar-202211 KiB330286

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.2 KiB13499

tsconfig.jsonH A D30-Mar-2022114 87

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

waittask.spec.tsH A D30-Mar-202227 KiB774668

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