Selenium

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:

String  handle= driver.getWindowHandle();//Return a string of alphanumeric window handle

To get the window handle of all windows:

Set handles = driver.getWindowHandles();//Return a set of window handles

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:

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
 }

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:

	 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);
	                          }
	                    });

	    }

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.

The post Working with Multiple Windows in Selenium WebDriver appeared first on OptimusQA.