Multiple Window Handling in Selenium

Today most of the web applications contain multiple navigation links which lead the user to other windows. It is quite possible that at the same time, functionality is performed using two or more windows.  This can be quite a tricky situation although it might sound a simple one. This was something I faced while automating a web application, as working with multiple windows (sometimes even 3-4 windows at the same time) was an essential part of this application).

Selenium WebDriver assigns an alphanumeric id to each window as soon as the WebDriver object is instantiated. This unique alphanumeric id is called window handle. Selenium uses this unique id to switch control among several windows. In simple terms, each unique window has a unique ID, so that Selenium can differentiate when it is switching controls from one window to the other. I will explain it using below example-

public void multipleWindowsHandle() {
        driver.get("http://the-internet.herokuapp.com/windows");
        // Store the first window handle
        String firstWindow = driver.getWindowHandle();
        String newWindow = "";
        driver.findElement(By.cssSelector(".example a")).click();
        //You might need to add wait here to wait for second window to appear
        Set<String> allWindows = driver.getWindowHandles();
        //iterate in window handles and switch to the different window
        for (String window : allWindows) {
            if (!window.equals(firstWindow)) {
                newWindow = window;
            }
        }

        driver.switchTo().window(newWindow);
        if(driver.getTitle().equals("New Window"))){
          System.out.println("You are now on new window");
        }
    }

}

Following is the expected behavior from above code-

  • Visit the page
  • Click to open a new window
  • Switch between the windows
  • Check the page title to make sure the correct window is in focus

First we have stored single window handle and after click operation we are storing all window handle. Once we have all window handle, we are switching to expected window. Important thing to understand here is that method getWindowHandles() returns a Set. A set never store data in sorted manner(its random), so don’t make a mistake of accessing window handles on basis of index.

 

Now when we click on link in above scenario, based on the network speed it might took sometime to open the new window. So following can be some solutions-

  1. Thread.Sleep() could be one solution but we never know the exact time to wait. It also depends on internet speed and processor speed.
  2. Wait till total window handles become two.

Either we can use inbuilt method waitForNumberOfWindowsToBe() of ExpectedConditions class or we can make our own as below-

Code to wait for specific number of windows-

public void waitForNumberOfWindowsToEqual(final int numberOfWindows)               { //Making a new expected condition   
new WebDriverWait(driver,30){}.until(new ExpectedCondition<Boolean>(){   
                   @Override public Boolean apply(WebDriver driver) {

                              return (driver.getWindowHandles().size() == numberOfWindows);}

                          });

}

If you handle this wait in your code, your code become more robust. This is how you can work on multiple windows in Selenium WebDriver.

Happy Learning 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s