List of usage examples for org.openqa.selenium WebDriver getWindowHandles
Set<String> getWindowHandles();
From source file:com.gargoylesoftware.htmlunit.javascript.host.Window2Test.java
License:Apache License
/** * @throws Exception if the test fails//from w ww . j a v a 2 s.c o m */ @Test public void setDocumentLocation() throws Exception { final String firstContent = "<html>\n" + "<head><title>First</title></head>\n" + "<body>\n" + "<form name='form1'>\n" + " <a id='link' onClick='document.location=\"" + URL_SECOND + "\";'>Click me</a>\n" + "</form>\n" + "</body></html>"; final String secondContent = "<html><head><title>Second</title></head><body></body></html>"; getMockWebConnection().setResponse(URL_SECOND, secondContent); final WebDriver driver = loadPage2(firstContent); assertEquals("First", driver.getTitle()); assertEquals(1, driver.getWindowHandles().size()); driver.findElement(By.id("link")).click(); assertEquals("Second", driver.getTitle()); assertEquals(1, driver.getWindowHandles().size()); assertEquals(new String[] { "", "second/" }, getMockWebConnection().getRequestedUrls(URL_FIRST)); assertEquals(URL_SECOND.toString(), driver.getCurrentUrl()); }
From source file:com.gargoylesoftware.htmlunit.WebDriverTestCase.java
License:Apache License
/** * Release resources but DON'T close the browser if we are running with a real browser. * Note that HtmlUnitDriver is not cached by default, but that can be configured by {@link #isWebClientCached()}. *//*from ww w . jav a 2s . c o m*/ @After @Override public void releaseResources() { super.releaseResources(); if (!isWebClientCached()) { if (webDriver_ != null) { webDriver_.quit(); } assertTrue("There are still JS threads running after the test", getJavaScriptThreads().isEmpty()); } if (useRealBrowser()) { synchronized (WEB_DRIVERS_REAL_BROWSERS) { final WebDriver driver = WEB_DRIVERS_REAL_BROWSERS.get(getBrowserVersion()); if (driver != null) { try { final String currentWindow = driver.getWindowHandle(); final Set<String> handles = driver.getWindowHandles(); // close all windows except the current one handles.remove(currentWindow); if (handles.size() > 0) { for (final String handle : handles) { try { driver.switchTo().window(handle); driver.close(); } catch (final NoSuchWindowException e) { LOG.error("Error switching to browser window; quit browser.", e); WEB_DRIVERS_REAL_BROWSERS.remove(getBrowserVersion()); WEB_DRIVERS_REAL_BROWSERS_USAGE_COUNT.remove(getBrowserVersion()); driver.quit(); return; } } // we have to force WebDriver to treat the remaining window // as the one we like to work with from now on // looks like a web driver issue to me (version 2.47.2) driver.switchTo().window(currentWindow); } driver.manage().deleteAllCookies(); // in the remaining window, load a blank page driver.get("about:blank"); } catch (final WebDriverException e) { shutDownRealBrowsers(); } } } } }
From source file:com.hotwire.test.steps.angular.AngularModelTemplate.java
License:Open Source License
@Override public void clickCommentCardLink(String page) { if (page.equals("home")) { new AngularHomePage(getWebdriverInstance()).clickOlabLink(); } else if (page.equals("results")) { new AngularHotelResultsPage(getWebdriverInstance()).clickOlabLink(); } else if (page.equals("details")) { new AngularHotelDetailsPage(getWebdriverInstance()).clickOlabLink(); } else {/* w ww.j a v a 2 s .com*/ throw new UnimplementedTestException(page + ": not valid page id for this method."); } LOGGER.info(">>>>> Switching to comment card window <<<<<"); // Need to switch to comment card window. Looping through set as it is not guaranteed what order will be // returned. Assuming there will only be 2 windows opened as anymore will be problematic. WebDriver webdriverInstance = getWebdriverInstance(); String parentWindow = webdriverInstance.getWindowHandle(); //String parentTitle = webdriverInstance.getTitle(); for (String handle : webdriverInstance.getWindowHandles()) { LOGGER.info("Browser window handle: " + handle); if (!handle.equals(parentWindow)) { webdriverInstance.switchTo().window(handle); // Assume the window that doesn't have the parent title is the comment card. if (webdriverInstance.getTitle().equals("Comment card")) { LOGGER.info("Found comment card window."); break; } } } }
From source file:com.hotwire.test.steps.application.ApplicationModelWebApp.java
License:Open Source License
@Override public void submitCommentCard(String comment, int content, int design, int usability, int overall) { logger.info(">>>>> Switching to comment card window <<<<<"); // Need to switch to comment card window. Looping through set as it is // not guaranteed what order will be // returned. Assuming there will only be 2 windows opened as anymore // will be problematic. WebDriver webdriverInstance = getWebdriverInstance(); String parentWindow = webdriverInstance.getWindowHandle(); // String parentTitle = webdriverInstance.getTitle(); for (String handle : webdriverInstance.getWindowHandles()) { logger.info("Browser window handle: " + handle); if (!handle.equals(parentWindow)) { webdriverInstance.switchTo().window(handle); // Assume the window that doesn't have the parent title is the // comment card. if (webdriverInstance.getTitle().equals("Comment card")) { logger.info("Found comment card window."); break; }// w w w . j a va2 s. c o m } } CommentCardPage commentCardPage = new CommentCardPage(getWebdriverInstance()); commentCardPage.typeComment(comment); commentCardPage.rateContent(content); commentCardPage.rateDesign(design); commentCardPage.rateUsability(usability); commentCardPage.rateOverall(overall); commentCardPage.clickSubmitButton(); logger.info("Comment card sent. Switching to parent browser window."); getWebdriverInstance().switchTo().window(getWebdriverInstance().getWindowHandles().toArray()[0].toString()); }
From source file:com.hotwire.test.steps.customercare.CustomerCareModuleModelWebApp.java
License:Open Source License
private ExpectedCondition<Boolean> waitForUnexpectedPopup() { return new ExpectedCondition<Boolean>() { @Override//from w w w .ja v a 2 s . c o m public Boolean apply(WebDriver driver) { return driver.getWindowHandles().size() > 1; } }; }
From source file:com.hotwire.test.steps.search.hotel.HotelSearchModelTemplate.java
License:Open Source License
@Override public void clickCommentCardLink() { HotelResultsPage resultsPage = new HotelResultsPage(getWebdriverInstance()); resultsPage.clickCommentCardLink();/*from w w w . j a v a2 s . com*/ logger.info(">>>>> Switching to comment card window <<<<<"); // Need to switch to comment card window. Looping through set as it is not guaranteed what order will be // returned. Assuming there will only be 2 windows opened as anymore will be problematic. WebDriver webdriverInstance = getWebdriverInstance(); String parentWindow = webdriverInstance.getWindowHandle(); // String parentTitle = webdriverInstance.getTitle(); for (String handle : webdriverInstance.getWindowHandles()) { logger.info("Browser window handle: " + handle); if (!handle.equals(parentWindow)) { webdriverInstance.switchTo().window(handle); // Assume the window that doesn't have the parent title is the comment card. if (webdriverInstance.getTitle().equals("Comment card")) { logger.info("Found comment card window."); break; } } } }
From source file:com.hp.test.framework.jelly.SwitchToChildBrowserTag.java
@Override public void doTag(XMLOutput arg0) throws MissingAttributeException, JellyTagException { logger.info("Started Execution of SwitchToChildBrowser"); WebDriver driver = getSelenium(); try {//from w w w . j a v a 2s.com //Set <String> set1=driver.getWindowHandles(); //Iterator <String> window=set1.iterator(); //String ParentWindow=window.next(); //String ChildWindow=window.next(); for (String ChildWindow : driver.getWindowHandles()) { driver.switchTo().window(ChildWindow); } setSelenium((WebDriver) driver); } catch (Exception e) { logger.error("Exception switching Child Window" + "\n" + e.getMessage()); } logger.info("Completed Execution of SwitchToChildBrowser"); }
From source file:com.ibm.sbt.automation.core.environment.TestEnvironment.java
License:Open Source License
/** * Return a WebDriver for the window with one of the specified titles *///from w w w. j a v a 2 s .c om public WebDriver findPopup(String[] titles) { if (titles == null || titles.length == 0) return null; WebDriver webDriver = getWebDriver(); WebDriver popup = null; Set<String> windowHandles = webDriver.getWindowHandles(); Iterator<String> windowIterator = windowHandles.iterator(); while (windowIterator.hasNext()) { String windowHandle = windowIterator.next(); popup = webDriver.switchTo().window(windowHandle); String title = popup.getTitle(); for (int i = 0; i < titles.length; i++) { if (title != null && title.contains(titles[i])) { return popup; } } } return null; }
From source file:com.ibm.sbt.automation.core.environment.TestEnvironment.java
License:Open Source License
/** * @param webDriver/*from w w w . ja v a 2s. c om*/ * @param windowHandle */ protected void restoreWindowHandle(WebDriver webDriver, String windowHandle) { Set<String> windowHandles = webDriver.getWindowHandles(); if (!windowHandles.contains(windowHandle)) { // expected window handle doesn't exist Iterator<String> windowIterator = windowHandles.iterator(); if (windowIterator.hasNext()) { windowHandle = windowIterator.next(); } } webDriver.switchTo().window(windowHandle); }
From source file:com.ibm.watson.app.qaclassifier.selenium.CommonFunctions.java
License:Open Source License
public static void switchTabs(WebDriver driver) { String currentHandle = driver.getWindowHandle(); new WebDriverWait(driver, 2).until(new Predicate<WebDriver>() { @Override// w w w . j a v a 2 s . co m public boolean apply(WebDriver input) { return input.getWindowHandles().size() > 1; } @Override public String toString() { return "tab to open"; } }); assertThat("Expected to find two browser tabs", driver.getWindowHandles(), hasSize(2)); for (String handle : driver.getWindowHandles()) { if (!handle.equals(currentHandle)) { driver.switchTo().window(handle); } } }