0
0

More than 3 years have passed since last update.

Jestとpuppeteerで複数ページへの同じテストをすっきりまとめたサンプル

Last updated at Posted at 2020-09-13

Jestとpuppeteerでe2eテストを書いています。大量のページに対してページのtitleをチェックしています。配列に対象ページのURLとtitleをまとめると、すっきり書けたのでメモしておきます。

配列

検査したい要素、titleとurlをまとめて指定しています。

const pages = [
  {
    'title': 'はじめに - Bootstrap 4.5 - 日本語リファレンス',
    'url': 'https://getbootstrap.jp/docs/4.5/getting-started/introduction/',
  },
  {
    'title': 'ダウンロード - Bootstrap 4.5 - 日本語リファレンス',
    'url': 'https://getbootstrap.jp/docs/4.5/getting-started/download/',
  },
  {
    'title': 'ファイル構成 - Bootstrap 4.5 - 日本語リファレンス',
    'url': 'https://getbootstrap.jp/docs/4.5/getting-started/contents/',
  },
];

テスト部分

testを配列のループで囲んでいます。ページを取得してtitleを照合しています。

  for (const i in pages) {
    const title = pages[i].title;
    const url = pages[i].url;
    it('should be titled "' + title + '"', async () => {
      await page.goto(url);
      await expect(page.title()).resolves.toMatch(title);
    });
  }
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