Translations: Français
- Puppeteer:
npm install --save-dev puppeteer
The first step is setting up a helper to configure the environment:
./test/_withPage.js
importpuppeteerfrom'puppeteer';module.exports=async(t,run)=>{constbrowser=awaitpuppeteer.launch();constpage=awaitbrowser.newPage();try{awaitrun(t,page);}finally{awaitpage.close();awaitbrowser.close();}}
./test/main.js
importtestfrom'ava';importwithPagefrom'./_withPage';consturl='https://google.com';test('page title should contain "Google"',withPage,async(t,page)=>{awaitpage.goto(url);t.true((awaitpage.title()).includes('Google'));});test('page should contain an element with `#hplogo` selector',withPage,async(t,page)=>{awaitpage.goto(url);t.not(awaitpage.$('#hplogo'),null);});test('search form should match the snapshot',withPage,async(t,page)=>{awaitpage.goto(url);constinnerHTML=awaitpage.evaluate(form=>form.innerHTML,awaitpage.$('#searchform'));t.snapshot(innerHTML);});