2022-12-09 12:57:39 +00:00
# Puppeteer tests
2020-04-09 05:56:25 +00:00
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:
2021-03-23 09:02:34 +00:00
- `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.
2022-09-29 08:08:55 +00:00
- `server` : a dummy test server instance (see `packages/testserver` for more).
- `httpsServer` : a dummy test server HTTPS instance (see `packages/testserver` for more).
2021-03-23 09:02:34 +00:00
- `isFirefox` : true if running in Firefox.
- `isChrome` : true if running Chromium.
- `isHeadless` : true if the test is in headless mode.
2020-04-09 05:56:25 +00:00
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.
2020-06-12 13:55:51 +00:00
## Skipping tests in specific conditions
2020-04-09 05:56:25 +00:00
2023-02-28 11:55:20 +00:00
To skip tests edit the [TestExpectations ](https://github.com/puppeteer/puppeteer/blob/main/test/TestExpectations.json ) file. See [test runner documentation ](https://github.com/puppeteer/puppeteer/tree/main/tools/mochaRunner ) for more details.
2020-04-09 18:12:32 +00:00
## Running tests
2022-12-09 12:57:39 +00:00
- To run all tests applicable for your platform:
2020-04-09 18:12:32 +00:00
```bash
2022-07-01 14:29:21 +00:00
npm test
2020-04-09 18:12:32 +00:00
```
2022-07-01 14:29:21 +00:00
- **Important**: don't forget to first build the code if you're testing local changes:
2020-05-13 08:20:33 +00:00
```bash
2023-01-27 12:20:32 +00:00
npm run build --workspace=@puppeteer-test/test & & npm test
2020-05-13 08:20:33 +00:00
```
2023-01-27 12:20:32 +00:00
### 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
2020-04-09 18:12:32 +00:00
- To run a specific test, substitute the `it` with `it.only` :
2022-07-01 11:52:39 +00:00
```ts
2020-04-09 18:12:32 +00:00
...
it.only('should work', async function() {
2023-06-21 19:41:09 +00:00
const {server, page} = await getTestState();
2020-04-09 18:12:32 +00:00
const response = await page.goto(server.EMPTY_PAGE);
expect(response.ok).toBe(true);
});
```
2022-12-09 12:57:39 +00:00
- To disable a specific test, substitute the `it` with `it.skip` :
2020-04-09 18:12:32 +00:00
2022-07-01 11:52:39 +00:00
```ts
2020-04-09 18:12:32 +00:00
...
2022-12-09 12:57:39 +00:00
it.skip('should work', async function({server, page}) {
2023-06-21 19:41:09 +00:00
const {server, page} = await getTestState();
2020-04-09 18:12:32 +00:00
const response = await page.goto(server.EMPTY_PAGE);
expect(response.ok).toBe(true);
});
```
2022-07-01 14:29:21 +00:00
- To run Chrome headful tests:
2020-04-09 18:12:32 +00:00
```bash
2022-07-01 14:29:21 +00:00
npm run test:chrome:headful
2020-04-09 18:12:32 +00:00
```
- To run tests with custom browser executable:
```bash
2022-08-11 18:45:52 +00:00
BINARY=< path-to-executable > npm run test:chrome:headless # Or npm run test:firefox
2020-04-09 18:12:32 +00:00
```
2022-12-09 12:57:39 +00:00
[mocha]: https://mochajs.org/
[expect]: https://www.npmjs.com/package/expect