BrowserContext + Persistent Context in Playwright

In Microsoft Playwright, a BrowserContext is an abstraction that represents an independent session of browser activity, similar to an incognito session in a traditional web browser. Each BrowserContext can have its own set of cookies, local storage data, and session storage data, which means that activities performed in one context do not affect or interfere with those in another, providing a clean slate for each test or automation task.


The primary advantage of using BrowserContexts is their ability to simulate multiple users interacting with a web application simultaneously, without the need for multiple browsers to be opened and managed. Additionally, BrowserContexts allow for custom configurations, such as viewport size, geolocation, language, and permissions, enabling us to configure our scrapers differently from one context to another.

Persistent Context

A Persistent Context is a specialized type of BrowserContext that Playwright offers, designed to mimic the behavior of a regular (non-incognito) browser session. Unlike standard BrowserContexts that are ephemeral and lose their data once the session ends, a Persistent Context retains its data across sessions. This means cookies, local storage, and session storage persist even after the browser is closed, saved on a folder on your hard disk.

This allows scenarios such as testing the login sessions that need to remain active across browser restarts or cookie usage in different runs of the scraper.

The key difference between a standard BrowserContext and a Persistent Context lies in their data persistence behavior. While standard BrowserContexts are ideal for isolated, stateless testing scenarios requiring a fresh environment, Persistent Contexts are more useful for scrapers that require data continuity and state persistence across browser sessions.

Source: Tips and tricks for Microsoft Playwright

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.