×
☰ See All Chapters

Puppeteer Browser

  • A Browser is created when Puppeteer connects to a Chromium instance, either through puppeteer.launch or puppeteer.connect. 

  • An example of using a Browser to create a Page: 

const puppeteer = require('puppeteer');

async function example() {

    const browser = await puppeteer.launch();

    const page = await browser.newPage();

    await page.goto('https://tools4testing.com/');

    await browser.close();

}

example();

 

puppeteer-browser-0
 
  • The browser instance created by puppeteer will be headless by default. To make it non-headless, puppeteer should connect /launch with configuration argument headless=false 

  • There are Operating Systems without GUI like Linux. We can do everything by executing commands on command line interface or terminal. Like this there are browsers without GUI and can be done everything without using GUI. Such browsers are called as Headless browsers. 

  • Below is the list of functions/methods available from Browser class. 

Method/Function

Return Type

Description

browserContexts()

BrowserContext[]

Returns an array of all open browser contexts. If it is newly created browser, array will have single instance of BrowserContext.

close()

Promise<void>

Closes browser completely.

createIncognitoBrowserContext()

Promise<BrowserContext>

Creates a new incognito browser context. Does not store and share user data like cookies/cache with other browser contexts.

defaultBrowserContext()

BrowserContext

Returns the default browser context. The default browser context cannot be closed.

disconnect()

void

Disconnects Puppeteer from the browser instance, Browser process will be still running. Cannot reconnect to same process again. Connecting again using puppeteer.connect will connect to new process but the same browser instance.

isConnected()

boolean

This can be used to check if browser is connected.

newPage()

Promise<Page>

Returns a Promise with a new Page object.

pages()

Promise<Page[]>;

Returns a Promise with array of all open page objects

process()

ChildProcess

Returns browser process if instance is created by puppeteer.launch. Returns null if the browser instance was created with puppeteer.connect

target()

Target

Returns target associated with the browser. Target can be page, background_page, service_worker, shared_worker, browser, other.

targets()

Promise<Target[]>

Returns a Promise which with array of all active targets.

userAgent()

Promise<string>

Returns a Promise which with original browser user agent.

waitForTarget(predicate: (target: Target) => boolean, options?: Timeoutable)

Promise<Target>

Wait for the target by applying the provided predicate function.

waitForTarget(predicate: (target: Target) => boolean, options?: Timeoutable): Promise<Target>;

await page.evaluate(() => window.open('https://www.tools4testing.com/'));

const newWindowTarget = await browser.waitForTarget(target => target.url() === 'https://www.tools4testing.com/');

}

We can set timeout, Maximum wait time in milliseconds. Set 0 to disable the timeout. Defaults is 30 seconds.

wsEndpoint()

string

Returns browser websocket endpoint which can be used as an argument to puppeteer.connect.

 

 


All Chapters
Author