☰ See All Chapters |
Puppeteer Class
➠ Puppeteer module provides a method to launch a Chromium instance. The following is a typical example of using Puppeteer to drive automation:
const puppeteer = require('puppeteer');
async function example() {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://tools4testing.com/');
await page.screenshot({
path: 'example.png'
});
await browser.close();
}
example();
➠ Puppeteer provides two methods to connect to chromium instance. launch() and connect()
➠ Both launch() and connect() takes optional array of configuration arguments. Below are the important arguments:
launch() | ||
Argument | Type | Description |
product | String | Browser to launch. At this time, this is either chrome or firefox. const browser = await puppeteer.launch({product : "chrome" }); |
headless | boolean | By default puppeteer runs is headless mode, to run in non-headless mode this can be set to false const browser = await puppeteer.launch({headless : false}); |
devtools | boolean | Whether to auto-open a DevTools panel for each tab. If this option is true, the headless option will be set false. const browser = await puppeteer.launch({devtools: true}); |
defaultViewport | Object | Sets a consistent viewport for each page. Defaults to an 800x600 viewport. We can change the viewport size by specifying width and height options. ➠ width: Integer value for page width in pixels. ➠ height : Integer value for page height in pixels.. const browser = await puppeteer.launch({defaultViewport: { width: 1366, height: 768}}); |
slowMo | number | Slows down Puppeteer operations by the specified amount of milliseconds. We can keep track of execution when in non-headless mode. const browser = await puppeteer.launch({slowMo: 1000}); |
connect() | ||
Argument | Type | Description |
product | String | Browser to launch. At this time, this is either chrome or firefox. const browser = await puppeteer.connect({product : "chrome" }); |
browserURL | String | Application URL to connect. const browser = await puppeteer.connect({browserURL: "www.tools4testing.com" }); |
defaultViewport | Object | Sets a consistent viewport for each page. Defaults to an 800x600 viewport. We can change the viewport size by specifying width and height options. ➠ width: Integer value for page width in pixels. ➠ height : Integer value for page height in pixels.. const browser = await puppeteer.connect({defaultViewport: { width: 1366, height: 768}}); |
slowMo | number | Slows down Puppeteer operations by the specified amount of milliseconds. We can keep track of execution when in non-headless mode. const browser = await puppeteer.connect({slowMo: 1000}); |
All Chapters