Site icon Vinsguru

Puppeteer – Getting Started

Overview:

We all like chrome browser. Whether you like it not, It is the mostly used browser supported by the tech giant Google. I am huge fan of Chrome DevTools which helps me to play with locators, changing element style etc very quickly. Chrome supported headless mode testing from its version 59. Headless mode would be very useful in some cases – particularly in linux/server environments for automated testing.

Puppeteer is a node library with a high-level API to control chrome headless. It uses the DevTools api to interact with chrome. Aim of this article is to introduce puppeteer to you in case you are not aware already!

Installation:

node -v
v8.11.2
npm -v
5.6.0
npm init -y
Wrote to testautomationguru@remotehost/package.json:
{
  "name": "node-playground",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}
npm install --save puppeteer

Launching Browser Instance:

const puppeteer = require('puppeteer');

let config = {
    launchOptions: {
        headless: false
    }
}

puppeteer.launch(config.launchOptions).then(async browser => {
  const page = await browser.newPage();
  await page.goto('https://www.google.com');
  await browser.close();    
});
"scripts": {
    "test": "node test.js"
  }
npm run test
> node-playground@1.0.0 test testautomationguru/node-playground
> node test.js

Devices Emulation/View Port:

const devices = require('puppeteer/DeviceDescriptors');
console.log(devices)
const puppeteer = require('puppeteer');
const devices = require('puppeteer/DeviceDescriptors');
const iPhone = devices['iPhone 6'];

const config = {
    launchOptions: {
        headless: false
    }
}

puppeteer.launch(config.launchOptions).then(async browser => {
  const page = await browser.newPage();
  await page.emulate(iPhone)
  await page.goto('https://www.google.com');
  await browser.close();    
});
const config = {
    launchOptions: {
        headless: false
    },
    viewport:{width:1920, height:1080}
}

puppeteer.launch(config.launchOptions).then(async browser => {
  const page = await browser.newPage();
  await page.setViewport(config.viewport)
  await page.goto('https://www.google.com');
  await browser.close();    
});

Interacting With Elements:

page.waitFor(selector)
page.type(selector, text)
page.click(selector)
page.select(selector, value)
page.$$eval('input[type="checkbox"]', checkboxes => {
   checkboxes.forEach(chbox => chbox.click())
});
let txt = await page.$eval('span.form-checkbox-item', el => el.innerText );
console.log(txt);
const puppeteer = require('puppeteer');

const config = {
    launchOptions: {
        headless: false
    },
    viewport:{width:1920, height:1080}
}

//locators
const registrationPage = {
    firstname: 'input[name="firstName"]',
    lastname: 'input[name="lastName"]',
    username: 'input[name="email"]',
    password:  'input[name="password"]',
    confirmPassword:  'input[name="confirmPassword"]',
    submit: 'input[name="register"]'
}

const registrationConfirmation = {
    sigin: 'a[href="mercurysignon.php"]'
}

puppeteer.launch(config.launchOptions).then(async browser => {
  const page = await browser.newPage();
  await page.setViewport(config.viewport)
  
  //mercury tours registration page
  await page.goto('http://newtours.demoaut.com/mercuryregister.php');

  //wait for the firstname to appear
  await page.waitFor(registrationPage.firstname);

  //enter the details
  await page.type(registrationPage.firstname, 'automation');
  await page.type(registrationPage.lastname, 'guru');
  await page.type(registrationPage.username, 'guru');
  await page.type(registrationPage.password, 'guru');
  await page.type(registrationPage.confirmPassword, 'guru');
  await page.click(registrationPage.submit);

  //after page submission check for the sign-in confirmation
  await page.waitFor(registrationConfirmation.sigin);
  await page.click(registrationConfirmation.sigin);

  await browser.close();    
});

Screenshots:

page.screenshot({ path:'test.png'})
page.screenshot({ path:'test.png', fullPage: true})
const pos = await page.evaluate(selector => {
    const element = document.querySelector(selector);
    const {x, y, width, height} = element.getBoundingClientRect();
    return {x, y, width, height};
}, 'input#username');


await page.screenshot({ path:'test.png', clip: pos})
page.pdf({ path:'test.pdf'});

Summary:

Hope you got some idea from this article. As I had mentioned, aim of this article is just to get started with puppeteer. By using puppeteer in headless mode, we can run the tests in server environments without any GUI / docker containers etc. By making use of the Chrome DevTools, we should be able to achieve things like getting HTTP response code, downloading dynamically generated files etc which is not possible in selenium directly. We can take a look at those areas in the upcoming articles.

Happy Testing & Subscribe 🙂

 

Share This:

Exit mobile version