Jestとpuppeteerでe2eテストを書いています。大量のページに対してページのtitleをチェックしています。配列に対象ページのURLとtitleをまとめると、すっきり書けたのでメモしておきます。
配列
検査したい要素、titleとurlをまとめて指定しています。
constpages=[{'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(constiinpages){consttitle=pages[i].title;consturl=pages[i].url;it('should be titled "'+title+'"',async()=>{awaitpage.goto(url);awaitexpect(page.title()).resolves.toMatch(title);});}
コード全体
loop.test.js
constpages=[{'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/',},];describe('LOOP',()=>{beforeAll(async()=>{awaitpage.setDefaultNavigationTimeout(0);});for(constiinpages){consttitle=pages[i].title;consturl=pages[i].url;it('should be titled "'+title+'"',async()=>{awaitpage.goto(url);awaitexpect(page.title()).resolves.toMatch(title);});}});
実行結果
% jest loop.test.js
PASS ./loop.test.js
LOOP
✓ should be titled "はじめに - Bootstrap 4.5 - 日本語リファレンス" (696 ms)
✓ should be titled "ダウンロード - Bootstrap 4.5 - 日本語リファレンス" (331 ms)
✓ should be titled "ファイル構成 - Bootstrap 4.5 - 日本語リファレンス" (515 ms)
Test Suites: 1 passed, 1 total
Tests: 3 passed, 3 total
Snapshots: 0 total
Time: 2.251 s, estimated 9 s
Ran all test suites matching /loop.test.js/i.