As sometimes during web automation, when you perform click on a particular hyper link, it opens up a new window. In this post I will explain how to apply wait for a new window until it gets open as-
public class WaitForNewWindow {
@Test
public void testApp() {
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("
https://www.edureka.co/blog/what-is-selenium/");
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;
}
}