Playwright · Reliable end-to-end tests
What is a browser context?
Short answer
A browser context is an isolated, incognito-like session inside a browser. It has its own cookies, localStorage, cache and permissions, and each Playwright test gets a fresh one.
A browser context is the unit of isolation. Opening a page always happens inside a context, whether you create it explicitly or let the test fixture do it. Two contexts in the same browser cannot see each other's cookies, localStorage or cache.
const context = await browser.newContext({
viewport: { width: 412, height: 915 },
locale: "en-IN",
timezoneId: "Asia/Kolkata",
permissions: ["geolocation"],
});
const page = await context.newPage();
await context.close();What a context isolates
- Cookies and localStorage
- Cache and service workers
- Viewport, locale, timezone and permissions
- Extra HTTP headers and authentication credentials
- Network routing registered on that context
Contexts are much cheaper than browser processes. That is why Playwright can give every test a clean context while reusing a browser per worker. Creating a new browser for every test would be slower and provides little additional isolation for normal web tests.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Why is a context cheaper than a browser?
- 2How do you set a locale or timezone for a context?
- 3Does a context isolate server-side data?
- 4How does the page fixture create a context?
Say it in the interview
A browser context is an isolated session inside a browser, roughly like an incognito profile. It has its own cookies, localStorage, cache and permissions, and Playwright gives each test a fresh one. Contexts are cheap, so we reuse a browser per worker but not the session state. They do not isolate backend data, so I still need unique test data when tests share a server.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Playwright mock interview and get scored feedback on depth, clarity and confidence.