driver.switchTo().window(<windowName>);
public static final boolean switchToWindowByTitle(final String windowTitle) {
String currentHandle = Config.driver().getWindowHandle();
Set<String> handles = Config.driver().getWindowHandles();
for (String h : handles) {
if (!h.equals(currentHandle)) {
Config.driver().switchTo().window(h);
if (Config.driver().getTitle().contains(windowTitle)) {
return true;
}
}
}
return false;
}
public static final boolean switchToWindowByPageSource(final String windowContent) {
Set<String> allWindows = Config.driver().getWindowHandles();
for (String windowID : allWindows) {
Config.driver().switchTo().window(windowID);
if (Config.driver().getPageSource().contains(windowContent)) {
Config.driver().switchTo().window(windowID);
SeleniumUtils.log.info("Switch to winsdow: " + windowID);
return true;
}
}
return false;
}
/**
* When two windows are opened, switch to another window
*
*/
public static final void switchToAnotherWindow() {
final String parentWindowHandle = Config.driver().getWindowHandle();
SeleniumUtils.log.info("parent Window: " + parentWindowHandle);
new WebDriverWait(Config.driver(), 5, 100).until(new Function<WebDriver, Boolean>() {
public Boolean apply(final WebDriver driver) {
String[] windowHandles = driver.getWindowHandles().toArray(new String[0]);
SeleniumUtils.log.info("Window Size: " + windowHandles.length );
SeleniumUtils.log.info("Window 0: " + windowHandles[0]);
SeleniumUtils.log.info("Window 1: " + windowHandles[1]);
if (windowHandles.length != 2) {
return false;
}
if (windowHandles[0].equals(parentWindowHandle)) {
SeleniumUtils.log.info("Switch to Window: " + windowHandles[1]);
driver.switchTo().window(windowHandles[1]);
//Config.driver().manage().window().maximize();
} else {
driver.switchTo().window(windowHandles[0]);
SeleniumUtils.log.info("Switch to Window: " + windowHandles[0]);
//Config.driver().manage().window().maximize();
}
return true;
}
});
}
/**
*
* Get parent window
*
* @return
*/
public String getParentWindow(){
String parentWindowHandle = driver.getWindowHandle();
log.info("Get Window : " +parentWindowHandle);
return parentWindowHandle;
}
/**
*
* Back Parent Window
*
* @param window
*/
public void backToParentWindow(String parentWindowHandle){
//log.info("Window before switch: " +driver.getWindowHandle() );
driver.switchTo().window(parentWindowHandle);
log.info("Window after switch: " +driver.getWindowHandle() );
}
public void testSwitchWindow(){
Helper review = new Helper ();
String parentWindow = review.getParentWindow();
review.clickAdd(add);
SeleniumUtils.switchToAnotherWindow();
//do action on new window
review.backToParentWindow(parentWindow);
Config.driver().navigate().refresh();
}