0
0
Selenium Javatesting~10 mins

Window handles (getWindowHandles) in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a webpage that opens a new browser window. It switches control to the new window using window handles and verifies the new window's title.

Test Code - JUnit with Selenium WebDriver
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.Set;
import java.util.Iterator;
import static org.junit.Assert.assertEquals;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class WindowHandlesTest {
    WebDriver driver;

    @Before
    public void setUp() {
        driver = new ChromeDriver();
    }

    @Test
    public void testSwitchToNewWindow() {
        driver.get("https://example.com/page_with_popup");

        // Click button that opens new window
        driver.findElement(By.id("openWindowBtn")).click();

        // Get all window handles
        Set<String> windowHandles = driver.getWindowHandles();
        Iterator<String> iterator = windowHandles.iterator();

        String originalWindow = iterator.next();
        String newWindow = iterator.next();

        // Switch to new window
        driver.switchTo().window(newWindow);

        // Verify new window title
        String expectedTitle = "New Window Title";
        String actualTitle = driver.getTitle();
        assertEquals(expectedTitle, actualTitle);

        // Switch back to original window
        driver.switchTo().window(originalWindow);
    }

    @After
    public void tearDown() {
        driver.quit();
    }
}
Execution Trace - 8 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window is open and ready-PASS
2Navigates to https://example.com/page_with_popupPage with a button that opens a new window is loaded-PASS
3Finds button with id 'openWindowBtn' and clicks itNew browser window opens alongside original window-PASS
4Calls driver.getWindowHandles() to get all window handlesTwo window handles are retrieved: original and newVerify two window handles existPASS
5Switches control to the new window using driver.switchTo().window(newWindow)Browser focus is on the new window-PASS
6Gets the title of the new windowNew window is fully loadedAssert that title equals 'New Window Title'PASS
7Switches back to original window using driver.switchTo().window(originalWindow)Browser focus returns to original window-PASS
8Test ends and browser closesAll browser windows closed-PASS
Failure Scenario
Failing Condition: The new window does not open or window handle is missing
Execution Trace Quiz - 3 Questions
Test your understanding
What does driver.getWindowHandles() return?
AThe title of the current window
BThe URL of the current page
CA set of all open browser window handles
DThe handle of the original window only
Key Result
Always retrieve all window handles after opening a new window and switch to the correct handle before interacting with that window.