Intermediate 15 min 4 scenarios

Tabs & Windows Automation Practice

Practice opening new tabs, switching between windows, reading titles, and closing child windows — key skills for multi-tab automation in Selenium & Playwright.

Scenario 1: Open a link in a new tab

Click the button below — it opens the home page in a new tab. Switch to the new tab and read its title.


Scenario 2: Open multiple windows

Opens additional browser windows. Iterate all handles and print each window's title.


Scenario 3: Switch back to the parent window

Open a child tab using Scenario 1, then switch back to this parent window and assert the page title.

  1. Store the parent handle before clicking
  2. Click "Open Home Page" to open the child tab
  3. Switch to the child tab and read its title
  4. Switch back to parent handle and assert the title

Scenario 4: Close the child window

After switching to the child tab, close it and verify only the parent window remains active.

  1. Open child tab via Scenario 1
  2. Switch to child and call driver.close() / newPage.close()
  3. Switch back to parent handle
  4. Assert only 1 window handle remains

What You'll Learn

Selenium (Java)

  • getWindowHandles()
  • switchTo().window()
  • getWindowHandle()
  • driver.close()
  • Actions.keyDown(Keys.CONTROL)
Tutorial video coming soon

Test Cases

Introduction

Modern web apps frequently open content in new tabs or popup windows. Automation must:

  1. Detect new windows/tabs opened after a click
  2. Switch focus to the new window
  3. Interact with elements in the new window
  4. Switch back to the original window
  5. Close windows when done

Selenium tracks windows via window handles (unique string IDs). Playwright exposes new tabs as Page objects — much simpler to work with.

Key Methods Summary

Action Selenium (Java) Playwright (JS) Playwright (Python)
Get current handle getWindowHandle() page (already have it) page (already have it)
Get all handles getWindowHandles() context.pages() context.pages
Switch to window switchTo().window(handle) use the new Page object use the new Page object
Close current window close() page.close() page.close()
Back to original switchTo().window(original) use original page reference use original page reference

1. Open a new tab and switch to it

Selenium (Java)

String originalWindow = driver.getWindowHandle();
driver.findElement(By.id("newTabBtn")).click();
 
// Wait for the new window to open
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
wait.until(ExpectedConditions.numberOfWindowsToBe(2));
 
// Switch to the new window
for (String handle : driver.getWindowHandles()) {
    if (!handle.equals(originalWindow)) {
        driver.switchTo().window(handle);
        break;
    }
}
 
System.out.println("New tab URL: " + driver.getCurrentUrl());

Playwright (JS)

const [newPage] = await Promise.all([
  page.context().waitForEvent("page"),
  page.locator("#newTabBtn").click(),
]);
 
await newPage.waitForLoadState();
console.log("New tab URL:", newPage.url());

Playwright (Python)

with page.context.expect_page() as new_page_info:
    page.locator("#newTabBtn").click()
 
new_page = new_page_info.value
new_page.wait_for_load_state()
print("New tab URL:", new_page.url)

2. Interact with elements in the new window

Selenium (Java)

// (After switching to new window)
driver.findElement(By.id("newWindowInput")).sendKeys("Hello");
driver.findElement(By.id("newWindowSubmit")).click();

Playwright (JS)

// Use newPage directly
await newPage.locator("#newWindowInput").fill("Hello");
await newPage.locator("#newWindowSubmit").click();

Playwright (Python)

new_page.locator("#newWindowInput").fill("Hello")
new_page.locator("#newWindowSubmit").click()

3. Switch back to the original window

Selenium (Java)

driver.close(); // close new window
driver.switchTo().window(originalWindow); // return to original
 
// Verify we're back on the original page
assertEquals("https://www.qaplayground.com/practice/window", driver.getCurrentUrl());

Playwright (JS)

await newPage.close();
// The original "page" variable still points to the first page — just use it
await expect(page).toHaveURL(/window/);

Playwright (Python)

new_page.close()
# Original page reference is still valid
expect(page).to_have_url(re.compile("window"))

4. Handle a popup window (not a tab)

Selenium (Java)

String mainWindow = driver.getWindowHandle();
driver.findElement(By.id("popupBtn")).click();
 
// Switch to popup
Set<String> handles = driver.getWindowHandles();
for (String handle : handles) {
    if (!handle.equals(mainWindow)) {
        driver.switchTo().window(handle);
    }
}
 
// Interact in popup
String popupTitle = driver.getTitle();
driver.close();
driver.switchTo().window(mainWindow);

Playwright (JS)

const [popup] = await Promise.all([
  page.waitForEvent("popup"),
  page.locator("#popupBtn").click(),
]);
await popup.waitForLoadState();
console.log("Popup title:", await popup.title());
await popup.close();

Playwright (Python)

with page.expect_popup() as popup_info:
    page.locator("#popupBtn").click()
 
popup = popup_info.value
popup.wait_for_load_state()
print("Popup title:", popup.title())
popup.close()

5. Count total open windows/tabs

Selenium (Java)

int windowCount = driver.getWindowHandles().size();
System.out.println("Open windows: " + windowCount);
assertEquals(2, windowCount);

Playwright (JS)

const pages = page.context().pages();
console.log("Open tabs:", pages.length);
expect(pages.length).toBe(2);

Playwright (Python)

pages = page.context.pages
print("Open tabs:", len(pages))
assert len(pages) == 2

6. Open a new tab programmatically

Selenium (Java)

// Open a blank new tab using JavaScript
((JavascriptExecutor) driver).executeScript("window.open('https://www.qaplayground.com', '_blank');");
 
// Switch to new tab
List<String> handles = new ArrayList<>(driver.getWindowHandles());
driver.switchTo().window(handles.get(1));

Playwright (JS)

const newPage = await page.context().newPage();
await newPage.goto("https://www.qaplayground.com");

Playwright (Python)

new_page = page.context.new_page()
new_page.goto("https://www.qaplayground.com")

📄 Also Read: Top 10 Best Automation Practice Website

Frequently Asked Questions