String parent = driver.getWindowHandle();
Set<String> pops=driver.getWindowHandles();
{
Iterator<String> it =pops.iterator();
while (it.hasNext()) {
String popupHandle=it.next().toString();
if(!popupHandle.contains(parent))
{
driver.switchTo().window(popupHandle);
System.out.println("Popu Up Title: "+ driver.switchTo().window(popupHandle).getTitle());
driver.close();
public class WindowExamples {
static WebDriver driver;
@Test
public void test_CloseAllWindowsExceptMainWindow() {
driver = new FirefoxDriver();
driver.get("http://www.naukri.com/");
String windowTitle= getCurrentWindowTitle();
String mainWindow = getMainWindowHandle(driver);
Assert.assertTrue(closeAllOtherWindows(mainWindow));
Assert.assertTrue(windowTitle.contains("Jobs - Recruitment"), "Main window title is not matching");
}
public String getMainWindowHandle(WebDriver driver) {
return driver.getWindowHandle();
}
public String getCurrentWindowTitle() {
String windowTitle = driver.getTitle();
return windowTitle;
}
public static boolean closeAllOtherWindows(String openWindowHandle) {
Set<String> allWindowHandles = driver.getWindowHandles();
for (String currentWindowHandle : allWindowHandles) {
if (!currentWindowHandle.equals(openWindowHandle)) {
driver.switchTo().window(currentWindowHandle);
driver.close();
}
}
driver.switchTo().window(openWindowHandle);
if (driver.getWindowHandles().size() == 1)
return true;
else
return false;
}
}