e655bb6ca2
The `Puppeteer` class had two concerns: * connect to an existing browser * launch a new browser The first of those concerns is needed in all environments, but the second is only needed in Node. https://github.com/puppeteer/puppeteer/pull/6484 landing enabled us to pull the `Puppeteer` class apart into two: 1. `Puppeteer` which hosts the behaviour for connecting to existing browsers. 2. `PuppeteerNode`, which extends `Puppeteer` and also adds the ability to launch a new browser. This is a non-breaking change, because Node users will still get an instance of a class with all the methods they expect, but it'll be a `PuppeteerNode` rather than `Puppeteer`. I don't expect this to cause people any issues. We also now have new files that are effectively the entry points for Puppeteer: * `node.ts`: the main entry point for Puppeteer on Node. * `web.ts`: the main entry point for Puppeteer on the web. * `node-puppeteer-core.ts`: for those using puppeteer-core (which only exists in Node, not on the web). |
||
---|---|---|
.. | ||
assets | ||
fixtures | ||
golden-chromium | ||
golden-firefox | ||
.eslintrc.js | ||
accessibility.spec.ts | ||
ariaqueryhandler.spec.ts | ||
assert-coverage-test.js | ||
browser.spec.ts | ||
browsercontext.spec.ts | ||
CDPSession.spec.ts | ||
chromiumonly.spec.ts | ||
click.spec.ts | ||
cookies.spec.ts | ||
coverage-utils.js | ||
coverage.spec.ts | ||
defaultbrowsercontext.spec.ts | ||
dialog.spec.ts | ||
diffstyle.css | ||
elementhandle.spec.ts | ||
emulation.spec.ts | ||
evaluation.spec.ts | ||
EventEmitter.spec.ts | ||
fixtures.spec.ts | ||
frame.spec.ts | ||
golden-utils.js | ||
headful.spec.ts | ||
idle_override.spec.ts | ||
ignorehttpserrors.spec.ts | ||
input.spec.ts | ||
jshandle.spec.ts | ||
keyboard.spec.ts | ||
launcher.spec.ts | ||
mocha-ts-require.js | ||
mocha-utils.ts | ||
mouse.spec.ts | ||
navigation.spec.ts | ||
network.spec.ts | ||
oopif.spec.ts | ||
page.spec.ts | ||
queryselector.spec.ts | ||
README.md | ||
requestinterception.spec.ts | ||
run_static_server.js | ||
screenshot.spec.ts | ||
target.spec.ts | ||
touchscreen.spec.ts | ||
tracing.spec.ts | ||
tsconfig.json | ||
tsconfig.test.json | ||
utils.js | ||
waittask.spec.ts | ||
worker.spec.ts |
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 ranrequire('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 (seeutils/testserver
for more).httpsServer
: a dummy test server HTTPS instance (seeutils/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
withit.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
withxit
(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