Skip to content

Commit 6ab6d35

Browse files
Dmitriy Sharshakov aka. sh7dmsindresorhusnovemberborn
committed
Add recipe for AVA & Puppeteer (#1913)
Co-authored-by: Sindre Sorhus <[email protected]> Co-authored-by: Mark Wubben <[email protected]>
1 parent 1cc1bd5 commit 6ab6d35

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

docs/recipes/puppeteer.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Testing web apps using Puppeteer
2+
3+
## Dependencies
4+
5+
- [Puppeteer](https://github.com/GoogleChrome/puppeteer): `npm install --save-dev puppeteer`
6+
7+
## Setup
8+
9+
The first step is setting up a helper to configure the environment:
10+
11+
`./test/helpers/withPage.js`
12+
13+
```js
14+
import puppeteer from 'puppeteer';
15+
16+
export default async function withPage(t, run) {
17+
const browser = await puppeteer.launch();
18+
const page = await browser.newPage();
19+
try {
20+
await run(t, page);
21+
} finally {
22+
await page.close();
23+
await browser.close();
24+
}
25+
}
26+
```
27+
28+
## Usage example
29+
30+
`./test/main.js`
31+
32+
```js
33+
import test from 'ava';
34+
import withPage from './helpers/withPage';
35+
36+
const url = 'https://google.com';
37+
38+
test('page title should contain "Google"', withPage, async (t, page) => {
39+
await page.goto(url);
40+
t.true((await page.title()).includes('Google'));
41+
});
42+
43+
test('page should contain an element with `#hplogo` selector', withPage, async (t, page) => {
44+
await page.goto(url);
45+
t.not(await page.$('#hplogo'), null);
46+
});
47+
48+
test('search form should match the snapshot', withPage, async (t, page) => {
49+
await page.goto(url);
50+
const innerHTML = await page.evaluate(form => form.innerHTML, await page.$('#searchform'));
51+
t.snapshot(innerHTML);
52+
});
53+
```

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,7 @@ It's the [Andromeda galaxy](https://simple.wikipedia.org/wiki/Andromeda_galaxy).
12191219
- [Debugging tests with WebStorm](docs/recipes/debugging-with-webstorm.md)
12201220
- [Precompiling source files with webpack](docs/recipes/precompiling-with-webpack.md)
12211221
- [Isolated MongoDB integration tests](docs/recipes/isolated-mongodb-integration-tests.md)
1222+
- [Testing web apps using Puppeteer](docs/recipes/puppeteer.md)
12221223

12231224
## Support
12241225

0 commit comments

Comments
 (0)