Monday, 6 July 2026

How to Handle Multiple Browser Tabs and Windows in Playwright Using Java (Complete Guide)

Modern web applications often open new browser tabs or windows during user interactions. These may appear when users:

  • Download reports
  • Open help documentation
  • Sign in using Google or Microsoft
  • Complete online payments
  • View invoices
  • Open product details in a new tab

As an automation engineer, you need to switch between these tabs and windows seamlessly.

In this tutorial, we'll learn how to handle multiple browser tabs and windows in Playwright using Java.


Understanding Browser Tabs in Playwright

Unlike older automation frameworks, Playwright treats every browser tab as a Page object.

Whenever a new tab or popup opens, Playwright creates a new Page instance.

This makes switching between tabs much simpler.


Example Scenario

Imagine an e-commerce application.

  1. Open the home page.
  2. Click View Product.
  3. Product opens in a new tab.
  4. Verify the product title.
  5. Close the product tab.
  6. Continue testing on the original tab.

Let's automate this workflow.


Waiting for a New Browser Tab

Playwright provides the waitForPopup() method.

Page productPage = page.waitForPopup(() -> {
    page.locator("#viewProduct").click();
});

What happens here?

  • Playwright waits for a new tab.
  • The click action triggers the popup.
  • The new tab is returned as a Page object.

Verify the New Page

Once the new tab opens, you can interact with it just like the original page.

System.out.println(productPage.title());

productPage.locator("#addToCart").click();

No additional switching commands are required.


Getting the URL of the New Tab

System.out.println(productPage.url());

You can also verify it using assertions.

assertThat(productPage)
        .hasURL("https://example.com/product");

Closing the Popup Window

Once you're done with the popup:

productPage.close();

After closing, your original page remains active.


Working with Multiple Tabs

Suppose your application opens several tabs.

Example:

Page reportsPage = page.waitForPopup(() -> {
    page.locator("#reports").click();
});

Page invoicePage = page.waitForPopup(() -> {
    page.locator("#invoice").click();
});

Now you have two separate Page objects.

You can interact with each independently.


Managing All Open Pages

Playwright stores all open pages inside the browser context.

BrowserContext context = browser.newContext();

List<Page> pages = context.pages();

System.out.println(pages.size());

This returns every open browser tab.


Switching Between Tabs

Switching is simple because each tab has its own Page reference.

Page homePage = pages.get(0);

Page reportsPage = pages.get(1);

Page invoicePage = pages.get(2);

Simply use the desired Page object.


Example: Google Login Popup

Many websites use OAuth authentication.

Example:

  1. Click Sign in with Google
  2. Google login opens in a new window.
  3. Enter credentials.
  4. Return to application.
Page googleLogin = page.waitForPopup(() -> {

    page.locator("#googleLogin").click();

});

googleLogin.locator("#identifierId")
           .fill("user@example.com");

This approach works for any external authentication window.


Handling Multiple Browser Windows

Playwright treats browser windows exactly like browser tabs.

No special API is required.

Whether the application opens:

  • A new tab
  • A new browser window

Playwright returns a Page object.


Best Practices

Always Use waitForPopup()

Avoid:

page.locator("#openReport").click();

Then trying to search for the new page afterwards.

Instead:

Page reportPage =
    page.waitForPopup(() -> {

        page.locator("#openReport").click();

    });

This prevents timing issues.


Store Page References

Use meaningful variable names.

Good:

loginPage

paymentPage

reportPage

invoicePage

Avoid:

page1

page2

page3

Close Unused Tabs

Leaving unnecessary tabs open can:

  • Consume memory
  • Slow down execution
  • Make debugging harder

Always close tabs when they are no longer needed.


Verify Navigation

After switching tabs, verify:

  • URL
  • Page title
  • Key elements

Example:

assertThat(productPage)
        .hasTitle("Laptop");

Common Mistakes

Mistake 1

Using the original page after opening a popup.

Incorrect:

page.locator("#checkout").click();

Correct:

checkoutPage.locator("#checkout").click();

Mistake 2

Using hard waits.

Avoid:

Thread.sleep(5000);

Playwright automatically waits for the popup.


Complete Example

BrowserContext context =
        browser.newContext();

Page page = context.newPage();

page.navigate("https://example.com");

Page popup =
        page.waitForPopup(() -> {

            page.locator("#openWindow").click();

        });

System.out.println(popup.title());

popup.locator("#continue")
     .click();

popup.close();

System.out.println(page.title());

Interview Questions

How does Playwright represent a browser tab?

Every browser tab or browser window is represented by a Page object.


Which method waits for a new browser tab?

waitForPopup()

How do you retrieve all open tabs?

context.pages()

Can Playwright automate multiple browser windows?

Yes.

Browser windows and tabs are handled in the same way using separate Page objects.


Conclusion

Handling multiple browser tabs and windows is an essential skill for automation engineers working on enterprise applications.

Playwright makes this process simple by representing each browser tab as a Page object and providing APIs like waitForPopup() and context.pages().

In this tutorial, you learned:

  • How to wait for new tabs
  • How to switch between browser windows
  • How to verify popup content
  • How to close windows
  • Best practices for multi-tab automation

With these techniques, you'll be able to automate payment gateways, report downloads, external authentication flows, and many other real-world scenarios confidently.

No comments:

Post a Comment