GitHub - avajs/ava: 🚀 Testing can be a drag. AVA helps you get it done.
なんかちょっとしたテストにPuppeteer使えるお手軽フレームワーク無いかなと思ってちょっとググってみたらAVAと言うのが良さそうだったのでさらにPuppeteerのレシピまであったのでちょっと使ってみた。
Recipes
helpers/withPage in recipe
なるほど話が早い
The first step is setting up a helper to configure the environment:
helpers/withPage.js
import puppeteer from 'puppeteer';
export default async function withPage(t, run) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
try {
await run(t, page);
} finally {
await page.close();
await browser.close();
}
}
ava/puppeteer.md at master · avajs/ava · GitHub
さっそくサーバー証明書に引っかかるの巻
ありがち、でも困る
main.js
import test from 'ava';
import withPage from './helpers/withPage';
const url = `https://host/test`;
test('page title must be appear in "📓The Narrative"', withPage, async (t, page) => {
await page.goto(url);
t.is('🐥 VigilaNTe', await page.title());
}
);
✖ page title must be appear in "📓 The Narrative" Rejected promise returned by test
1 test failed
page title must be appear in "📓 The Narrative"
/path/to/node_modules/puppeteer/lib/FrameManager.js:107
Rejected promise returned by test. Reason:
Error {
message: 'net::ERR_CERT_AUTHORITY_INVALID at https://host/test',
}
navigate (node_modules/puppeteer/lib/FrameManager.js:107:37)
Frame.<anonymous> (node_modules/puppeteer/lib/helper.js:144:27)
Page.goto (node_modules/puppeteer/lib/Page.js:624:49)
Page.<anonymous> (node_modules/puppeteer/lib/helper.js:145:23)
goto (test/main.js:7:16)
Test.run (test/helpers/withPage.js:7:11)
puppeteer.launch([options]) で対応
とりあえずpuppeteer.launchで対応してしまおう
helpers/withPage.js
import puppeteer from 'puppeteer';
export default async function withPage(t, run, puppeteer_launch_opts = {}) {
const browser = await puppeteer.launch(puppeteer_launch_opts);
const page = await browser.newPage();
try {
await run(t, page);
} finally {
await page.close();
await browser.close();
}
}
main.js
import test from 'ava';
import withPage from './helpers/withPage';
const url = `https://host/test`;
const puppeteer_launch_opts =
{
ignoreHTTPSErrors: true
};
test('page title must be appear in "📓The Narrative"', withPage, async (t, page) => {
await page.goto(url);
t.is('🐥 VigilaNTe', await page.title());
}, puppeteer_launch_opts
);
✔ page title must be appear in "📓 The Narrative" (2.4s)
1 test passed
以上 🐤