Handle windows popups using Selenium Webdriver

import java.util.Set;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class WindowExamples {
static WebDriver driver;

@Test
public void test_CloseAllWindowsExceptMainWindow() {
driver = new FirefoxDriver();
// It will open Naukri website with multiple windows
driver.get(“http://www.naukri.com/”);

// To get the main window handle
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;
}

//To close all the other windows except the main window.
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;
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s