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);
});
}