SynchronisationService extends BaseServiceHub { public void waitAndSwitchToNewBrowserWindow(int timeout) { final Set<String> handles = SenBotContext.getSeleniumDriver().getWindowHandles(); mainWindowHandle.set(SenBotContext.getSeleniumDriver().getWindowHandles().iterator().next()); if (getWebDriver() instanceof InternetExplorerDriver) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } String newWindow = (new WebDriverWait(getWebDriver(), timeout)).until(new ExpectedCondition<String>() { public String apply(WebDriver input) { if (input.getWindowHandles().size() > 1) { Iterator<String> iterator = input.getWindowHandles().iterator(); String found = iterator.next(); found = iterator.next(); return found; } return null; } }); assertTrue(!newWindow.equals(mainWindowHandle.get())); assertTrue(!newWindow.isEmpty()); popupWindowHandle.set(newWindow); SenBotContext.getSeleniumDriver().switchTo().window(newWindow); }  boolean waitForExpectedCondition(ExpectedCondition<?> condition, int timeout); boolean waitForExpectedCondition(ExpectedCondition<?> condition); void waitAndAssertForExpectedCondition(ExpectedCondition<?> condition, int timeout); void waitAndAssertForExpectedCondition(ExpectedCondition<?> condition); boolean checkForExpectedCondition(ExpectedCondition<?> condition); void checkAndAssertForExpectedCondition(ExpectedCondition<?> condition); void waitAndSwitchToNewBrowserWindow(int timeout); void waitAndSwitchToNewBrowserWindow(); void switchToMainWindow(); void switchToLatestPopupWindow();  }
@Test public void testWaitAndSwitchToNewBrowserWindow() throws Exception { seleniumNavigationService.navigate_to_url(TEST_PAGE_URL); seleniumSynchronisationService.checkAndAssertForExpectedCondition(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input[id='textField']"))); driver.findElement(By.cssSelector("input[id='textField']")).sendKeys("file: seleniumElementService.findExpectedElement(By.cssSelector("button[id='openBrowserWindow']")).click(); seleniumSynchronisationService.waitAndSwitchToNewBrowserWindow(seleniumManager.getTimeout()); assertEquals(1, driver.findElements(By.xpath(" driver.close(); seleniumSynchronisationService.switchToMainWindow(); }