Working with Multiple Windows in Selenium WebDriver

I normally have at least three or four windows open at a time. Some of my workflows rely on being able to smoothly switch between my strategically placed windows.

I recently had to solve a very interesting window-related testing problem.

We were testing a SaaS desktop application in Selenium and some of its workflows presumed that the user would have three or four application windows open at the same time.

So how do you automate switching between windows in Selenium?

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. So Selenium has following methods to get window handles.

To get the window handle of the current window:

[code lang=”Java”]String handle= driver.getWindowHandle();//Return a string of alphanumeric window handle[/code]

To get the window handle of all windows:

[code lang=”Java”]Set handles = driver.getWindowHandles();//Return a set of window handles[/code]

As soon as a link within the application is clicked, the user is presented with a new window. Selenium, however, does not automatically switch control to the new window. This has to be coded manually.

To tackle this issue, I created the following keyword.

Code for Switching to Second window:

[code lang=”Java”]public String firstWinHandle;//It store the handle of main window of application
//This method Click on a link/Button in the main window and then switch control to second window
public void clickAndSwitchToSecondWindow()
{
//Perform the click operation that opens new window

driver.findElement(By.id(“id of the link/button which opens new window”)).click();
//wait till two windows are not opened
waitForNumberofWindowsToEqual(2);//this method is for wait

Set handles = driver.getWindowHandles();
firstWinHandle = driver.getWindowHandle(); handles.remove(firstWinHandle);
String winHandle=handles.iterator().next();
if (winHandle!=firstWinHandle){
//To retrieve the handle of second window, extracting the handle which does not match to first window handle
secondWinHandle=winHandle; //Storing handle of second window handle

//Switch control to new window
driver.switchTo().window(secondWinHandle);
//Control is switched now
}[/code]

In the above code you can see there is a method waitForNumberOfWindowsToEqual().

So we also need to write this method to execute above code. Let me explain the approach I took.

We don’t know how long it will take to open the new window after clicking a link that opens a new window, so we need to hold up code execution until the window is ready. We could use Thread.Sleep()  but we don’t know how long we should wait for the window which depends on things like Internet speed and processor speed.

So instead of a time-based solution, we count the number of windows. Once the number of open windows reaches the expected number, we can start testing again.

Here’s the code for wait:

[code] public void waitForNumberOfWindowsToEqual(final int numberOfWindows)
{

//Making a new expected condition
new WebDriverWait(driver,30){}.until(new ExpectedCondition()
{
@Override public Boolean apply(WebDriver driver)
{
return (driver.getWindowHandles().size() == numberOfWindows);
}
});

}[/code]

This is how I implemented automated testing across multiple windows in Selenium WebDriver.

Please share your interesting and valuable thoughts in the comments.

Happy Learning.


4 replies
  1. Avatar
    Vibhor Jain says:

    Why did you remove first handle from Set? you could have iterated to second element in set and then you could have assigned it to secondHandler.

    Reply
  2. Avatar
    Ganesa Murthy says:

    Hi, here is one way.

    //to get the mainhandle
    String mainhandle = driver.getWindowHandle();
    //to get all the handles
    String handles = driver.getWindowHandles();
    for(String subhandle : handles ) {
    if(!subhandle.equals(mainhandle)) {
    driver.switchTo.window(subhandle);
    }
    }

    Hope this helps.

    Reply
    • Avatar
      Ganesa Murthy says:

      Updated code

      //to get the mainhandle
      String mainhandle = driver.getWindowHandle();
      //to get all the handles
      Set handles = driver.getWindowHandles();
      for(String subhandle : handles ) {
      if(!subhandle.equals(mainhandle)) {
      driver.switchTo.window(subhandle);
      }
      }

      Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *