Rayrun

How do I create an isolated browser context and page in Playwright?

Answer

Creating an Isolated Browser Context and Page

To fully isolate a new browser context and page from the storage state, you can use Playwright's "incognito" browser contexts. These contexts don't write any browsing data to disk, ensuring complete isolation from other contexts or pages.

Here's how to create an incognito context and a new page inside it:

const { chromium } = require('@playwright/test');

(async () => {
  const browser = await chromium.launch();
  const context = await browser.newContext(); // Create an incognito context
  const page = await context.newPage(); // Create a new page inside the context
  await page.goto('https://ray.run/'); // Navigate to a URL
  // Perform your tests here
  await context.close(); // Dispose of the context when finished
  await browser.close();
})();

By using an incognito browser context, you can test web applications in isolation without interfering with existing user sessions or data. This is a powerful tool for ensuring your tests run efficiently and accurately. For more tips on writing efficient Playwright test scripts, check out Tips for Writing Efficient Playwright Test Scripts.

Thank you!
Was this helpful?
Still have questions?

If you still have questions, please ask a question and I will try to answer it.

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.