Skip to content

Latest commit

 

History

History
73 lines (55 loc) · 1.69 KB

puppeteer.md

File metadata and controls

73 lines (55 loc) · 1.69 KB

Puppeteer

Browser Automation Library. Puppeteer = Node.js + Chrome. Open pages, navigate to websites, evaluate Javascript

Installation

npm install puppeteer

Usage

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://joel.tools/merch');
  const price = await page.$eval('.price', div => div.textContent);
  console.log(price);
  await browser.close();
})();

Puppeteer for Firefox

npm install puppeteer-firefox

Browser context to speed up tests

const browser = await puppeteer.launch();

it('should have a pay button', async () => {
  const context = await browser.createIncognitoBrowserContext();
  const page = await context.newPage();
  await page.goto('https://joel.tools/merch');
  expect(await page.$('button.gpay-button')).toBeTruthy();
  await context.close();
})

Flaky tests (tests that sometimes pass sometimes don't)

it('should pay', async () =>{
  const page = await context.newPage();
  await page.goto('https://joel.tools/merch/');
  await page.waitForSelector('button.gpay-button');

  const response = page.waitForResponse(res => res.url().endsWith('/pay'));
  await page.click('button.gpay-button');
  await response;
  assert(await page.$('.successful-purchase'));
});

Extract data

await page.waitForXPath("/html/body/div/a/@title", {timeout: 1000});
const aTags = await page.$x('/html/body/div/a');
for (const aTag of aTags) {
  let title = await page.evaluate(aTag => aTag.title, aTag);
  console.log(title);
}

Source

Modern Web Testing and Automation with Puppeteer