How to Apply Wait for New Window using Selenium WebDriver?

SeleniumLogo

As you might have seen the following case while doing automation that when you perform click on a particular link or button, it opens a new window.

Selenium has getWindowHandles() and switchTo() to switch the controls to new window. However, sometime script fails because when script tried to switch the control, the new Window didn’t open at the time.

In order to overcome from this synchronization issue, you need to give some wait before switching the control to new window.

There are 2 options to handle this issue:

  • Apply Hard wait using following command. However, it is not recommended to use Hard wait as it slows down the script without knowing when new window will get opened. It just wait for few seconds and then execute the next line of code.
    Thread.sleep()
  • Apply Dynamic wait for a new window until it gets open and move ahead as soon as it is opened.

Best option is to use Dynamic wait !

In below code, we will see that how to apply Dynamic wait for a new window until it gets open.

There is a custom function created to apply Dynamic Wait and below script is using that function to wait for a new window. You just need to copy this function in your project and call it with required parameters.

waitForNewWindow(WebDriver driver, int timeOut)

Scenario :

1. Launch URL : http://automate-apps.com/contents/
2. Click on “Why Selenium” link.(As soon as you perform click on it, it opens a new window)
3. Wait for new window.
4. Close browser.

Program :

import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;

public class WaitForNewWindowTest {

	@Test
	public void testApp() {

		WebDriver driver = new FirefoxDriver();

		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

		driver.get("http://automate-apps.com/contents/");

		driver.findElement(By.id("why-selenium")).click();

		//Verify new window is opened or not
		Assert.assertTrue(waitForNewWindow(driver,10), "New window is not opened");

		System.out.println("New window has been opened");

		driver.quit();
	}

	public boolean waitForNewWindow(WebDriver driver, int timeout){
	      boolean flag = false;
	      int counter = 0;
	      while(!flag){
	          try {
	              Set<String> winId = driver.getWindowHandles();
	              if(winId.size() > 1){
	            	  flag = true;
	                  return flag;
	              }
	              Thread.sleep(1000);
	              counter++;
	              if(counter > timeout){
	                  return flag;
	              }
	          } catch (Exception e) {
	        	  System.out.println(e.getMessage());
	        	  return false;
	          }
	      }
	      return flag;
	  }

}

If you really like the information provided above, please don’t forget to like us on Facebook, you can also leave the comment.

Leave a Reply

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