0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AVA + Puppeteer のサンプルレシピを元に ignoreHTTPSErrors を指定する

Last updated at Posted at 2018-12-14

ava/header.png at master · avajs/ava · GitHub

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で対応してしまおう :sweat_smile:

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

以上 🐤:star2:

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?