Puppeteer - Browser Operations



The browser operations can be done by Puppeteer with the help of below given methods −

launch()

It is used to open new browsers and connect with an instance of Chromium. It has some optional parameters which are as follows −

Product − This is of String type and is used to point to the browser to be launched.

Syntax

The syntax is as follows −

let l = await puppeteer.launch({product : "chrome" })

headless − This is of Boolean type(default value is true) and it has to be set with false value inorder to execute the tests in headed mode.

Syntax

The syntax is as follows −

let l = await puppeteer.launch({headless : false})

devtools − This is of Boolean type. If it is set to true, then DevTools shall open automatically in each browser tab. Also, the headless parameter should be set to false, if devtools is set to true.

Syntax

The syntax is as follows −

let l = await puppeteer.launch({devtools: true})

defaultViewport − This is of type object. It provides a persistent viewport for a page(default value of viewport is 800*600). We can modify the size of the viewport by mentioning integer values in width and height for pixels.

Syntax

The syntax is as follows −

let l = await puppeteer.launch({defaultViewport: { width: 500, height: 459}})

slowMo − This is of type number. This parameter is used to slow down the Puppeteer execution for some time, provided in milliseconds.

Syntax

The syntax is as follows −

let l = await puppeteer.launch({slowMo: 500})

goTo()

It is used to navigate to a webpage. The URL of the page to be navigated is passed as a parameter.

Syntax

The syntax is as follows −

await page.goto('https://www.tutorialspoint.com/index.htm')

close()

It is used to close an opened browser.

Syntax

The syntax is as follows −

await browser.close()

browserContexts()

This yields an array of all opened browser contexts.

createIncognitoBrowserContext()

It opens a new browser in incognito context.

defaultBrowserContext()

It yields a default browser context.

disconnect()

It is used to disconnect Puppeteer from the browser instance.

isConnected()

It is used to verify whether a browser is connected.

newPage()

It yields a Promise with a new page object.

pages()

It yields a Promise with an array of all open page objects.

process()

It yields a browser process if the instance is created with the launch method. Furthermore, it yields a null value if the instance is created with the connect method.

target()

It yields the target for a browser.

targets()

It yields a Promise containing the array of all targets which are active.

Advertisements