Showing posts with label BrowserContext in Playwright. Show all posts
Showing posts with label BrowserContext in Playwright. Show all posts

BrowserContext in Playwright

 



In Playwright, a BrowserContext is like an isolated, independent session within a browser. We can think of it as a separate browser profile that does not share cookies, cache, or storage with other contexts, even though they run within the same browser instance. 

This makes it very useful for testing scenarios where you want multiple users to interact with the same application without interfering with each other—for example, logging in as two different users simultaneously. 

Each context can have its own set of pages (tabs) and can be configured with settings such as viewport size, geolocation, user agent, or permissions. Unlike launching multiple browsers, creating multiple contexts is lightweight and much faster, because contexts share the same underlying browser process but remain completely isolated. 

This improves performance, reduces resource usage, and allows for parallel test execution. In short, BrowserContext enables efficient, scalable, and realistic testing by simulating multiple independent browser environments within a single browser instance.











Why BrowserContext is useful in Playwright:

1. Test Isolation

  • Each BrowserContext has its own cookies, cache, and local storage.

  • This means multiple tests can run in parallel without sharing session data.
  • Example: One context can simulate a logged-in user, while another can simulate a guest user.

2. Faster Execution
  • Instead of launching a full new browser instance for every test, you can just create a new BrowserContext.
  • This is much faster and lighter on resources.

3. Multi-User Scenarios

  • You can easily test scenarios involving multiple users at the same time. Below is the example.
        Context A = Seller account
        Context B = Buyer account
        Both interact with the same application in parallel, just like in real life.


4. Parallel Testing
  • Multiple contexts allow parallel execution inside a single browser.
  • This speeds up test suites significantly.

5. Clean-up Simplicity
  • When you close a BrowserContext, all related data (cookies, storage, sessions) is automatically cleared.
  • This prevents data leakage between tests.


Important Points:

FeatureDescription
IsolationEach context has its own cookies, local storage, and session data.
Multiple tabsYou can open multiple pages (tabs) inside one context.
Faster than launching new browserYou don’t need to open a new browser each time, just create a new context.


Java Example with Playwright

Let's create two BrowserContext objects to simulate two users logging in separately.


Maven Dependencies:

<dependency>
    <groupId>com.microsoft.playwright</groupId>
    <artifactId>playwright</artifactId>
    <version>1.43.0</version> <!-- use latest -->
</dependency>


Java Code: BrowserContext Example








(a) In above code, we are creating two BrowserContexts by using code:
BrowserContext userAContext = browser.newContext();

(b) Once we create BrowserContext object, we are creating Page object by:
Page userAPage = userAContext.newPage();

(c) Then, we are launching browser by using navigate()
userAPage.navigate("https://example.com");

(d) Then, we are printing web page title by title()
System.out.println("User A title: " + userAPage.title());


import com.microsoft.playwright.*;

public class BrowserContextExample {
    public static void main(String[] args) {
        try (Playwright playwright = Playwright.create()) {
            // Launch browser
            Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));

            // Create first context (User A)
            BrowserContext userAContext = browser.newContext();
            Page userAPage = userAContext.newPage();
            userAPage.navigate("https://example.com");
            System.out.println("User A title: " + userAPage.title());

            // Create second context (User B)
            BrowserContext userBContext = browser.newContext();
            Page userBPage = userBContext.newPage();
            userBPage.navigate("https://example.com");
            System.out.println("User B title: " + userBPage.title());

            // Perform user-specific actions
            // Example: log in as two different users, store cookies separately

            // Cleanup
            userAContext.close();
            userBContext.close();
            browser.close();
        }
    }
}


Below are some use case example

In test automation:

  • We can simulate User A logs in and books a ticket, while User B cancels it.

  • Run in the same test thread efficiently using isolated sessions.


Suggested Posts:

1. Handle Alerts in Playwright
2. Handle Shadowdom in Playwright
3. Handle Dropdowns in Playwright
4. Handle IFrames in Playwright
5. Thread Local in Playwright