/** * Wait for the window handle to disappear. * @throws Exception */ public void waitForHandleNotPresent() throws Exception { try { timed(getWaitTimeout(), getWaitInterval(), new Action<Boolean>() { @Override @Nullable public Boolean execute() throws Exception { if(!driver().getWindowHandles().contains(driver().getWindowHandle())) { return null; } return Boolean.FALSE; } }); } catch(NoSuchWindowException e) { //Already gone, fine! } }
protected void closeAndQuitWebDriver() { if (driver != null) { if (WebDriverUtils.dontTearDownPropertyNotSet() && WebDriverUtils.dontTearDownOnFailure(isPassed())) { try { acceptAlertIfPresent(); driver.close(); } catch (NoSuchWindowException nswe) { System.out.println("NoSuchWindowException closing WebDriver " + nswe.getMessage()); } finally { if (driver != null) { driver.quit(); } } } } else { System.out.println("WebDriver is null for " + this.getClass().toString() + " if using a remote hub did you include the port?"); } }
private void closeAndQuitWebDriver() { if (driver != null) { if (WebDriverUtils.dontTearDownPropertyNotSet() && WebDriverUtils.dontTearDownOnFailure(passed)) { try { driver.close(); } catch (NoSuchWindowException nswe) { System.out.println("NoSuchWindowException closing WebDriver " + nswe.getMessage()); } finally { if (driver != null) { driver.quit(); } } } } else { System.out.println("WebDriver is null for " + this.getClass().toString()); } }
protected void closeAndQuitWebDriver() { if (driver != null) { if (WebDriverUtils.dontTearDownPropertyNotSet() && WebDriverUtils.dontTearDownOnFailure(passed)) { try { driver.close(); } catch (NoSuchWindowException nswe) { System.out.println("NoSuchWindowException closing WebDriver " + nswe.getMessage()); } finally { if (driver != null) { driver.quit(); } } } } else { System.out.println("WebDriver is null for " + this.getClass().toString()); } }
/** * Tear down. */ @AfterMethod(alwaysRun = true) public void tearDown() { try { if (null != mainDriver) { mainDriver.quit(); } if (null != context) { Map<String, IMyWebDriver> myWebDrivers = context .getBeansOfType(IMyWebDriver.class); for (IMyWebDriver myWebDriver2 : myWebDrivers.values()) { WebDriver weD = myWebDriver2.getWebDriver(); if (null != weD) weD.quit(); } } } catch (UnreachableBrowserException | NoSuchWindowException e) { // NOPMD // browser has been closed, no action needs to be done here. } if (null != context) { ((ConfigurableApplicationContext) context).close(); } }
private void checkCloseWindowAlert(String winh) throws NoAlertPresentException { try { Alert alt = getDriver().switchTo().alert(); if (alt == null) throw GlobalUtils.createInternalError("web driver"); PopupPromptDialog aDialog = new PopupPromptDialog(getDriver(), alt, alerts.size()); aDialog.setClosingWindowHandle(winh); alerts.add(aDialog); } catch (UnreachableBrowserException error) { if (getNumberOfOpenWindows() > 0) throw GlobalUtils .createInternalError("ATE multi windows handler", error); } catch (UnsupportedOperationException e) { throw GlobalUtils .createInternalError("Driver doesn't support alert handling yet", e); } catch (NoSuchWindowException windClosedAlready) { //do nothing if window closed without alert dialog intervention. for example in Chrome. throw new NoAlertPresentException(windClosedAlready); } }
@SuppressWarnings("unchecked") @Test public void shouldThrowFindableNotPresentExceptionIfDriverIsNotPresent() throws IOException { TargetedWebDriver driver = mock(TargetedWebDriver.class); OutputStream outputStream = mock(OutputStream.class); Browser browser = new WebDriverBrowser(driver, new StubWebDriverParentContext(), new StubWebDriverElementContext()); when(driver.getScreenshotAs(OutputType.BYTES)) .thenThrow(NoSuchWindowException.class); try { browser.takeScreenshot(outputStream); fail("Expected FindableNotPresentException to be thrown"); } catch (Exception e) { assertThat("Expected FindableNotPresentException to be thrown", e.getClass(), equalTo(FindableNotPresentException.class)); } verify(outputStream).close(); }
protected static void selectMatching(WebDriver webDriver, Matcher matcher, String error) { String startingHandle = null; try { startingHandle = webDriver.getWindowHandle(); } catch (NoSuchWindowException e) { // Window of current WebDriver instance is already closed } for (String handle : webDriver.getWindowHandles()) { webDriver.switchTo().window(handle); if (matcher.match(getCurrentWindowInfo(webDriver))) { return; } } if (startingHandle != null) { webDriver.switchTo().window(startingHandle); } throw new SeleniumLibraryNonFatalException(error); }
public static List<List<String>> getWindowInfos(WebDriver webDriver) { String startingHandle = null; try { startingHandle = webDriver.getWindowHandle(); } catch (NoSuchWindowException e) { // Window of current WebDriver instance is already closed } List<List<String>> windowInfos = new ArrayList<List<String>>(); try { for (String handle : webDriver.getWindowHandles()) { webDriver.switchTo().window(handle); windowInfos.add(getCurrentWindowInfo(webDriver)); } } finally { if (startingHandle != null) { webDriver.switchTo().window(startingHandle); } } return windowInfos; }
protected static void selectMatching(WebDriver webDriver, Matcher matcher, String error) { String startingHandle = null; try { startingHandle = webDriver.getWindowHandle(); } catch (NoSuchWindowException e) { // Window of current WebDriver instance is already closed } for (String handle : webDriver.getWindowHandles()) { webDriver.switchTo().window(handle); if (matcher.match(getCurrentWindowInfo(webDriver))) { return; } } if (startingHandle != null) { webDriver.switchTo().window(startingHandle); } throw new Selenium2LibraryNonFatalException(error); }
public void windowThrowsExceptionWhenNotExisting() throws Throwable { driver = new JavaDriver(); SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { frame.setVisible(true); } }); try { driver.switchTo().window("My Dialog1"); throw new MissingException(NoSuchWindowException.class); } catch (NoSuchWindowException e) { } }
@Test void switchingToWindowByUnknownNameOrHandleThrowsException() { NoSuchWindowException exception = mock(NoSuchWindowException.class); when(webDriver.switchTo().window("fooBar")).thenThrow(exception); assertThrows(NoSuchWindowException.class, () -> { cut.onWindow("fooBar"); }); verify(events).fireExceptionEvent(exception); verifyNoMoreInteractions(events); }
@Test(expected = NoSuchWindowException.class) public void testExceptionHandlingInCaseAWindowIsNotFoundForTheGivenNameOrHandle() { TargetLocator locator = mockTargetLocator(); NoSuchWindowException exception = createSeleniumExceptionOfClass(NoSuchWindowException.class); doThrow(exception).when(locator).window(NAME_OR_HANDLE); try { cut.setFocusOnWindow(NAME_OR_HANDLE); } finally { verifyLastEventFiredWasExceptionEventOf(exception); } }
public int getCurrentTabIndex(List<String> tabHandles) { try { String currentHandle = driver().getWindowHandle(); return tabHandles.indexOf(currentHandle); } catch (NoSuchWindowException e) { return -1; } }
private WebDriver nth(final int zeroBasedIndex) { try { final List<String> windowHandles = webDriver.getWindowHandles().stream().collect(Collectors.toList()); return webDriver.switchTo().window(windowHandles.get(zeroBasedIndex)); } catch (IndexOutOfBoundsException e) { throw new NoSuchWindowException(e.getMessage(), e); } }
@Override public WebDriver byTitle(final String title) { try { final FluentWait<WebDriver> wait = new WebDriverWait(webDriver, defaultTimeout().getSeconds()); return wait.until(windowToBeAvailableAndSwitchToIt(title)); } catch (TimeoutException e) { throw new NoSuchWindowException(String.format("Cannot find the window with title %s.", title)); } }
@Override public WebDriver byPartialTitle(final String partialTitle) { try { final FluentWait<WebDriver> wait = new WebDriverWait(webDriver, defaultTimeout().getSeconds()); return wait.until(windowToBeAvailableAndSwitchToIt(partialTitle, true)); } catch (TimeoutException e) { throw new NoSuchWindowException(String.format("Cannot find the window with partial title %s.", partialTitle)); } }
private void removeClosedWindows() { boolean winRemoved = false;// NOPMD String currentWinHandle = ""; try { currentWinHandle = this.getDriver().getWindowHandle(); } catch (NoSuchWindowException noWinEx) { winRemoved = true; } // for (int i = 0; i < windows.size(); i++) { // if (windows.get(i).isClosed()) { // windows.remove(i); // winRemoved = true;// NOPMD // } // } for (Iterator<BrowserWindow> winItr=windows.iterator(); winItr.hasNext();) { BrowserWindow bw = winItr.next(); if (bw.isClosed()) { winItr.remove(); winRemoved = true; } else { try { this.getDriver().switchTo().window(bw.getWindowHandle()); } catch (NoSuchWindowException noWinE) { winItr.remove(); winRemoved = true; } } } if (winRemoved) { this.getDriver().switchTo() .window(windows.get(windows.size() - 1).getWindowHandle()); } else { this.getDriver().switchTo().window(currentWinHandle); } }
/** * Close. */ public void close() { try { obtainWindowFocus(); myWd.getWebDriverInstance().close(); this.setClosed(true); } catch (NoSuchWindowException noWinE) { this.setClosed(true); } }
private String findWindow(TargetLocator targetLocator) { for (String windowHandle : targetLocator.defaultContent().getWindowHandles()) { Browser forWindowHandle = By.id(windowHandle).find(Browser.class, parentContext); view.setContext(forWindowHandle); if (view.isLoaded()) { return windowHandle; } } throw new NoSuchWindowException("No window in driver found which has " + view + " " + "currently loaded."); }
private String findWindow(TargetLocator targetLocator) { for (String windowHandle : targetLocator.defaultContent().getWindowHandles()) { if (targetLocator.window(windowHandle).getTitle().equals(title)) { return windowHandle; } } throw new NoSuchWindowException("No window in driver found which has title: " + title); }
private String findWindow(TargetLocator targetLocator) { for (String windowHandle : targetLocator.defaultContent().getWindowHandles()) { if (urlMatcher.matches(targetLocator.window(windowHandle).getCurrentUrl())) { return windowHandle; } } throw new NoSuchWindowException("No window in driver found which has url matching: " + urlMatcher); }
/** * Wrapper for interacting with a targeted driver that may or may not actually be present. */ private void attempt(Runnable action) { try { action.run(); } catch (NoSuchFrameException | NoSuchWindowException | NoSuchSessionException e) { throw new FindableNotPresentException(this, e); } }
/** * Wrapper for interacting with a targeted driver that may or may not actually be present. * Returns a result. */ private <T> T attemptAndGet(Supplier<T> action) { try { return action.get(); } catch (NoSuchFrameException | NoSuchWindowException | NoSuchSessionException e) { throw new FindableNotPresentException(this, e); } }
@Test(expected = NoSuchWindowException.class) public void shouldThrowNoSuchWindowExceptionIfViewIsNotLoadedInAnyWindow() { View view = new NeverLoadedView(); WebDriverTarget target = WebDriverTargets.withViewLoaded(view, context); target.switchTo(locator); }
public void switchWindow() throws NoSuchWindowException { WebDriver drv = getDriver(); Set<String> handles = drv.getWindowHandles(); String current = drv.getWindowHandle(); if (handles.size() > 1) { handles.remove(current); } String newTab = handles.iterator().next(); drv.switchTo().window(newTab); }
@Test(expected = NoSuchWindowException.class) public void testSwitchToInvalidWindow() { assertEquals(1, driver.getWindowHandles().size()); driver.switchTo().window("42"); }
@Test(expected = NoSuchWindowException.class) public void shouldThrowNoSuchWindowExceptionIfNoWindowsCanBeFoundMatchingTheUrlMatcher() { WebDriverTarget target = WebDriverTargets.windowByUrl(equalTo("awesome window")); target.switchTo(locator); }
@Test(expected = NoSuchWindowException.class) public void shouldThrowNoSuchWindowExceptionIfNoWindowsCanBeFoundWithTitle() { WebDriverTarget target = WebDriverTargets.windowByTitle("awesome window"); target.switchTo(locator); }
@Test(expected = NoSuchWindowException.class) public void shouldThrowExceptionIfContextNotFound() { openMultipleWebViewActivity(); SelendroidDriver driver = driver(); driver.context("BANANA"); }
/** * Sets the browser's focus to the window with the given name or handle. * <p> * <b>Tip:</b> A handle for the current window can be got by using the {@link CurrentWindow#getHandle()} method. * * @param nameOrHandle the name or handle of the window to focus on * @throws NoSuchWindowException in case there is no window with the given name or handle * @see Browser#currentWindow() * @see CurrentWindow#getHandle() * @see WebDriver.TargetLocator#window(String) * @since 2.0 */ public void onWindow(String nameOrHandle) throws NoSuchWindowException { ActionTemplate.browser(browser()) .execute(browser -> doOnWindow(browser, nameOrHandle)) .fireEvent(browser -> new SwitchedToWindowEvent(nameOrHandle)); log.debug("focused on window with name or handle: {}", nameOrHandle); }