JS Testing Academy · QA Demo Site

Browser Windows & Tabs

New tabs and new windows. The test has to move its focus to the new handle, do its work, close it and return.

The opened page has the title Sample page and a heading with the id sample-heading.

You are still on the main page.

Practice tasks

  1. Click Open a new tab, switch to the new handle, assert its heading, close it and switch back.
  2. Assert that the main page's handle count goes from 1 to 2 and back to 1.
  3. Open the message window and read the text inside it.
  4. Assert the main window title is unchanged after you come back.
Sample automation code
String parent = driver.getWindowHandle();
driver.findElement(By.id("tab-button")).click();

for (String h : driver.getWindowHandles()) {
    if (!h.equals(parent)) { driver.switchTo().window(h); break; }
}
Assert.assertEquals(driver.findElement(By.id("sample-heading")).getText(), "This is a sample page");
driver.close();
driver.switchTo().window(parent);
Assert.assertEquals(driver.getWindowHandles().size(), 1);