Sobes.tech
Middle

At what level is test isolation in Playwright? Why create a new BrowserContext?

sobes.tech AI

Answer from AI

In Playwright, test isolation is achieved at the BrowserContext level. Each BrowserContext is a separate, independent browser environment with its own cookies, local storage, and sessions.

Creating a new BrowserContext for each test ensures that the state of one test does not affect another, providing cleanliness and predictability in testing.

If the same BrowserContext is used for multiple tests, session data and cookies will be preserved between tests, which can lead to unwanted side effects.

Example:

const browser = await playwright.chromium.launch();
const context = await browser.newContext(); // new context for isolation
const page = await context.newPage();
// ... test actions
await context.close();

Thus, a new BrowserContext is a way to ensure test isolation and independence in Playwright.

At what level is test isolation in Playwright? Why… - sobes.tech