Why we are using the Set Collection for storing number of child windows ? And why don`t we use any other collection ?
public class Chilld_Window_Close_Using_Set {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
//get the parent window handle name
String parentHandle = driver.getWindowHandle();
System.out.println("Parent Window :" + parentHandle);
// get all the window handles and assign it to a set
// It will include child window and parentwindow
Set<String> windowHandles = driver.getWindowHandles();
System.out.println("No of Window Handles or currently opened browsers: " + windowHandles.size());
// remove the parent handle from the set
windowHandles.remove(parentHandle);
//Because we removed only the parent handle now only child handles will be thier in
// the set
// Now iterate through the child window and close the child window
for (String winHandle : windowHandles) {
driver.switchTo().window(winHandle);
System.out.println("Child Handle : " + winHandle);
driver.close();
}
// switching back to parent window
driver.switchTo().window(parentHandle);
//closing the parent browser
System.out.println("Closing the parent Browser :");
driver.close();
}
}
With Regards,
BALAJI SINGH .Y