What is a Playwright `BrowserContext`, and why is it central to test isolation and parallel execution?
Answer
A BrowserContext is an isolated browser session inside one browser process, similar to a fresh incognito profile: cookies, local storage, permissions and other session state are separated from other contexts. Creating a fresh context per test gives strong isolation without launching a full browser each time, while multiple pages in one context intentionally share session state. Use separate contexts for independent users or tests and reuse authentication through explicit storage-state setup rather than leaking state from one test into another.
Strong answer includes
- describes BrowserContext as isolated browser-session state rather than another page
- connects fresh contexts to cheap test isolation and safe parallelism
- uses separate contexts for independent users and explicit storage state for controlled auth reuse
Practical example
A messaging test needs Alice and Bob online at once. It creates two contexts, logs each user in separately and opens one page per context. Their cookies and local storage cannot overwrite each other, while both sessions still run efficiently inside the same browser process.