Daniel Viglione
Daniel Viglione

Reputation: 9457

What is a BrowserContext in the Node library Puppeteer?

I am reading over the documentation of Puppeteer. It states the following:

BrowserContexts provide a way to operate multiple independent browser sessions. When a browser is launched, it has a single BrowserContext used by default. The method browser.newPage() creates a page in the default browser context.

It is interesting it uses the terminology "session". My interpretation of a session is data that we store server-side to store information that is not appropriate to store client-side. Cookies are used on the client to persist information across requests. However, sessions are used in server languages to persist data that is not appropriate to store in cookies. Yet, Puppeteer is a client-side framework to crawl web pages via Chromium. So what does the documentation mean by browser session and browser context?

Upvotes: 5

Views: 9916

Answers (2)

Grant Miller
Grant Miller

Reputation: 29037

The Official Documentation defines the BrowserContext class as:

class: BrowserContext

BrowserContexts provide a way to operate multiple independent browser sessions. When a browser is launched, it has a single BrowserContext used by default. The method browser.newPage() creates a page in the default browser context.

If a page opens another page, e.g. with a window.open call, the popup will belong to the parent page's browser context.

Puppeteer allows creation of "incognito" browser contexts with browser.createIncognitoBrowserContext() method. "Incognito" browser contexts don't write any browsing data to disk.

// Create a new incognito browser context
const context = await browser.createIncognitoBrowserContext();
// Create a new page inside context.
const page = await context.newPage();
// ... do stuff with page ...
await page.goto('https://example.com');
// Dispose context once it's no longer needed.
await context.close();

When Puppeteer refers to a browser session, it is not in reference to session variables.

The term session means "a period devoted to a particular activity." In this case, the activity is browsing.

So the session begins on puppeteer.launch() and ends on browser.close().

The phrase browser context refers to the active or specified browser session.

There can be multiple browser sessions, so the BrowserContext class allows you to focus on a particular session.

Upvotes: 2

Vaviloff
Vaviloff

Reputation: 16856

As far as I understand this:

BrowserContext is a usual browser instance. Currently there can be two BrowserContexts: the default one, which is created upen start, and an Incognito.

Browser session is not a strictly technical term, it is a definition of a work session performed in a given browser. It contains all of the data and metadata that can be present in a working browser: open tabs and pages, accumulated cookies, history, windows position, size, etc.

In case of puppeteer a session lasts from browser launch to browser close. The important thing about sessions is that the default and Incognito sessions are separate (like in a real Chrome/ium) and cannot access each others data, like cookies. Whereas different tabs in one browser session (BrowserContext) can do that.

Upvotes: 3

Related Questions