writeToFile(webdriver.getPageSource(), pageSource); Alert alert = webdriver.switchTo().alert(); log.severe(e + ": " + alert.getText()); alert.accept(); savePageSourceToFile(config, fileName, webdriver, false);
@Override public WebDriver apply(WebDriver driver) { try { return driver.switchTo().frame(driver.findElement(locator)); } catch (NoSuchFrameException e) { return null; } catch (WebDriverException e) { return null; } }
/** * Switch to window/tab by name/handle/title except some windows handles * @param title title of window/tab */ protected static WebDriver windowByTitle(WebDriver driver, String title) { Set<String> windowHandles = driver.getWindowHandles(); for (String windowHandle : windowHandles) { driver.switchTo().window(windowHandle); if (title.equals(driver.getTitle())) { return driver; } } throw new NoSuchWindowException("Window with title not found: " + title); }
private void closeNewWindows(WebDriver webDriver, String currentWindowHandle, Set<String> currentWindows) { Set<String> windowHandles = webDriver.getWindowHandles(); if (windowHandles.size() != currentWindows.size()) { Set<String> newWindows = new HashSet<>(windowHandles); newWindows.removeAll(currentWindows); log.info("File has been opened in a new window, let's close " + newWindows.size() + " new windows"); for (String newWindow : newWindows) { log.info(" Let's close " + newWindow); try { webDriver.switchTo().window(newWindow); webDriver.close(); } catch (NoSuchWindowException windowHasBeenClosedMeanwhile) { log.info(" Failed to close " + newWindow + ": " + Cleanup.of.webdriverExceptionMessage(windowHasBeenClosedMeanwhile)); } catch (Exception e) { log.warning(" Failed to close " + newWindow + ": " + e); } } webDriver.switchTo().window(currentWindowHandle); } }
/** * Switches to new browser window for browsing. */ public void switchToNewWindow() { String curWin = driver.getWindowHandle(); for (String handle : driver.getWindowHandles()) { if (!handle.equals(curWin) && !windowHandles.contains(curWin)) { windowHandles.push(curWin); driver.switchTo().window(handle); break; } } }
private void selectWindowWithTitle(String title) { String current = driver.getWindowHandle(); for (String handle : driver.getWindowHandles()) { driver.switchTo().window(handle); if (title.equals(driver.getTitle())) { return; } } driver.switchTo().window(current); throw new SeleniumException("Unable to select window with title: " + title); }
@Override public WebDriver apply(WebDriver driver) { try { List<String> windowHandles = new ArrayList<>(driver.getWindowHandles()); return driver.switchTo().window(windowHandles.get(index)); } catch (IndexOutOfBoundsException windowWithIndexNotFound) { return null; } }
private static Boolean isSwithedOn(final WebDriver from, String handle) { from.switchTo().window(handle); if (from.getWindowHandle().equals(handle)) return true; else return null; }
@Step("DismissAlert with message \"([^\"]*)\"") public void dismissAlertWithMessage(final String message) { // Get a handle to the open alert, prompt or confirmation final Alert alert = webDriverContext().getWebDriver().switchTo().alert(); // this will throw a org.openqa.selenium.NoAlertPresentException if no // alert is present Assert.assertThat(alert.getText(), is(message)); // And acknowledge the alert (equivalent to clicking "OK") alert.accept(); } }
protected boolean assertThatAlertListenerWorks(EmptyWebDriver driver, TestListener listener, String prefix) { try { Alert alert = driver.switchTo().alert(); alert.accept(); alert.dismiss(); alert.sendKeys("Keys"); assertThat(listener.messages, contains(prefix + "Attempt to accept alert", prefix + "The alert was accepted", prefix + "Attempt to dismiss alert", prefix + "The alert was dismissed", prefix + "Attempt to send keys to alert", prefix + "Keys were sent to alert")); return true; } finally { listener.messages.clear(); } }