@Override public void open() { String paramUrl = paramTranslate(getUrl()); engine.openUrl(paramUrl); try { engine.computeToolbarHeight(); } catch(UnhandledAlertException e) { Alert alert = engine.getDriver().switchTo().alert(); if(alert != null) { alert.dismiss(); engine.computeToolbarHeight(); } } }
public List<WebElement> findElements(By arg0) { logger.debug("Finding elements with locator: " + arg0.toString()); try { List<WebElement> elements = driver.findElements(arg0); List<WebElement> returnValue = new ArrayList<WebElement>(); for (WebElement element : elements) { WebElementWrapper wrappedElement = new WebElementWrapper( element, driver); registerAlertHandlerToElement(wrappedElement); returnValue.add(wrappedElement); } return returnValue; } catch (UnhandledAlertException exp) { handleAlert(); } return findElements(arg0); }
public List<WebElement> findElements(By arg0) { try { List<WebElement> elements = element.findElements(arg0); List<WebElement> returnElements = new ArrayList<WebElement>(); for (WebElement ele : elements) { WebElementWrapper wrapper = new WebElementWrapper(ele, driver); registerAlertHandlerToElement(wrapper); returnElements.add(ele); } return returnElements; } catch (UnhandledAlertException exp) { } return element.findElements(arg0); }
/** * Takes screenshot from current page * @param basename filename (below screenshot base directory). * @return location of screenshot. */ public String takeScreenshot(String basename) { try { String screenshotFile = createScreenshot(basename); if (screenshotFile == null) { throw new SlimFixtureException(false, "Unable to take screenshot: does the webdriver support it?"); } else { screenshotFile = getScreenshotLink(screenshotFile); } return screenshotFile; } catch (UnhandledAlertException e) { // standard behavior will stop test, this breaks storyboard that will attempt to take screenshot // after triggering alert, but before the alert can be handled. // so we output a message but no exception. We rely on a next line to actually handle alert // (which may mean either really handle or stop test). return String.format( "<div><strong>Unable to take screenshot</strong>, alert is active. Alert text:<br/>" + "'<span>%s</span>'</div>", StringEscapeUtils.escapeHtml4(alertText())); } }
/** * {@inheritDoc} */ @Override public void onApplicationEvent(@Nullable StepUnexpectedAlertEvent arg0) { if (arg0 == null) throw GlobalUtils .createInternalError("spring application context event"); if (arg0.getThrowable() instanceof UnhandledAlertException) { ElementTestStep step = (ElementTestStep) arg0.getSource(); List<AbstractAlertDialog> alerts = step.getMyWebDriver().getMultiWindowsHandler().getAlerts(); for (AbstractAlertDialog alert:alerts) { alert.accept(); } step.setCorrectedOnTheFly(true); } }
public synchronized void takeScreenshot(final String errorMessage, final String testName) { try { //This probably could be used someday (do not delete) // int jsErrorNumber = JavaScriptError.readErrors(getWebDriver()).size(); // printStrings(image, removeNL(testName, errorMessage, "The following number of JS errors appeared during the test: " + jsErrorNumber)); BufferedImage image = new OurScreenshot().fullPage().getImage(); printStrings(image, removeNL(testName, errorMessage)); final String pathName = getFilenameFor(testName); final File screenShotWithProjectPath = new File(pathName); ImageIO.write(image, "png", screenShotWithProjectPath); addScreenShotPathForTest(getOriginalTestName(testName), screenShotWithProjectPath.getPath(), errorMessage); attachScreenShotToAllure(errorMessage, testName, screenShotWithProjectPath); //VE path below works for TestNG (do not delete) // String pathToImage = "../../ws/test-reports/test-classes/" + screenShotWithProjectPath.getName(); //VE path below works for ReportNG String pathToImage = "../../test-reports/test-classes/" + screenShotWithProjectPath.getName(); Reporter.log("<br/><a href='" + pathToImage + "' target='_blank'> <img src='" + pathToImage + "' height='100' width='100'/> </a><br/>"); } catch (IOException e) { LOGGER.error("IOException occurs", e); } catch (UnhandledAlertException alertException) { Alert alert = getWebDriver().switchTo().alert(); String alertText = alert.getText(); LOGGER.error("*****ERROR***** Unexpected Alert appeared. Alert text " + alertText); alert.dismiss(); takeScreenshot(errorMessage, testName); } }
/** * Called after every screen action, this checks whether the DomUI "waiting" backdrop is present and waits for it * to be gone. */ private static void initializeAfterCommandListener(final @Nonnull WebDriverConnector tu) { tu.setAfterCommandCallback(new IExecute() { @Override public void execute() { try { tu.waitForNoneOfElementsPresent(By.className("ui-io-blk"), By.className("ui-io-blk2")); } catch(UnhandledAlertException e) { //-- If an alert is present then we just ignore and continue. } catch(Exception x) { throw WrappedException.wrap(x); } } }); }
static public void onTestFailure(@Nonnull WebDriverConnector wd, @Nullable Method failedMethod) throws Exception { //-- Make a screenshot System.out.println("@onTestFailure: attempting to create a screenshot"); File tmpf = File.createTempFile("screenshot", ".png"); /* * Selenium aborts if we try to capture a screenshot with an alert on-screen 8-( So if that * abort happens we try to handle the alert, then try again. Since the alert probably * contains the reason why the test failed we try to get it's message since the screenshot * will not contain it - but the message is at least logged. */ try { wd.screenshot(tmpf); } catch(UnhandledAlertException x) { System.err.println("@onTestFailure: alert present when taking screenshot - trying to get rid of it"); //-- Try to handle alert try { String message = wd.alertGetMessage(); System.err.println("@onTestFailure: alert message is:\n" + message); } catch(Exception xx) { System.err.println("@onTestFailure: exception accepting alert"); xx.printStackTrace(); } //-- Try screenshot a 2nd time, die this time if it fails again wd.screenshot(tmpf); } System.out.println("Made screenshot in " + tmpf); if(tmpf.exists()) { IPaterContext context = Pater.context(); // System.out.println("@onTestFailure: context = " + context); context.registerResult("Screenshot at the end of the failed test", "image/png", tmpf); tmpf.delete(); } }
public WebElement findElement(By arg0) { logger.debug("Finding element with locator: " + arg0.toString()); try { WebElementWrapper element = new WebElementWrapper( driver.findElement(arg0), driver); registerAlertHandlerToElement(element); return element; } catch (UnhandledAlertException exp) { handleAlert(); } return findElement(arg0); }
private void handleAlert() { boolean handledAlert = false; Alert alert; try { alert = driver.switchTo().alert(); if (alert == null) { return; } for (IAlertHandler handler : alertHandlers) { handledAlert = handler.handleAlert(alert); handler.logAlert(alert); if (handledAlert) { break; // bail as soon as first handler has handled alert, // alert bubbling } else { throw new UnhandledAlertException( "Modal window present, " + ((alertHandlers.size() > 0) ? "alert handlers were not able to handle alert" : "There were no alert handlers to handle alert")); } } } catch (Exception exp) { // there is no alert false alarm, At least this is what we know as // of now return; } }
public WebElement findElement(By arg0) { try { WebElementWrapper wrapper = new WebElementWrapper( element.findElement(arg0), driver); registerAlertHandlerToElement(wrapper); return wrapper; } catch (UnhandledAlertException exp) { handleAlert();// bail out } return element.findElement(arg0); }
public String getAttribute(String arg0) { try { return element.getAttribute(arg0); } catch (UnhandledAlertException exp) { handleAlert(); } return element.getAttribute(arg0); }
public String getCssValue(String arg0) { try { return element.getCssValue(arg0); } catch (UnhandledAlertException exp) { handleAlert(); } return element.getCssValue(arg0); }
public Point getLocation() { try { return element.getLocation(); } catch (UnhandledAlertException exp) { handleAlert(); } return element.getLocation(); }
public Dimension getSize() { try { return element.getSize(); } catch (UnhandledAlertException exp) { handleAlert(); } return element.getSize(); }
public String getTagName() { try { return element.getTagName(); } catch (UnhandledAlertException exp) { handleAlert(); } return element.getTagName(); }
public String getText() { try { return element.getText(); } catch (UnhandledAlertException exp) { handleAlert(); } return element.getText(); }
public boolean isDisplayed() { try { return element.isDisplayed(); } catch (UnhandledAlertException exp) { handleAlert(); } return element.isDisplayed(); }
public boolean isEnabled() { try { return element.isEnabled(); } catch (UnhandledAlertException exp) { handleAlert(); } return element.isEnabled(); }
public boolean isSelected() { try { return element.isSelected(); } catch (UnhandledAlertException exp) { handleAlert(); } return element.isSelected(); }
public void sendKeys(CharSequence... arg0) { try { element.sendKeys(arg0); return; } catch (UnhandledAlertException exp) { handleAlert(); } element.sendKeys(arg0); }
public void submit() { try { element.submit(); return; } catch (UnhandledAlertException exp) { handleAlert(); } element.submit();; }
public void handleAlert() { boolean handledAlert = false; Alert alert; try { alert = driver.switchTo().alert(); if (alert == null) { return; } for (IAlertHandler handler : alertHandlers) { handledAlert = handler.handleAlert(alert); handler.logAlert(alert); if (handledAlert) { break; // bail as soon as first handler has handled alert, // alert bubbling } else { throw new UnhandledAlertException( "Modal window present, " + ((alertHandlers.size() > 0) ? "alert handlers were not able to handle alert" : "There were no alert handlers to handle alert")); } } } catch (Exception exp) { // there is no alert false alarm, At least this is what we know as // of now return; } }
@Test(expected = UnhandledAlertException.class) public void demonstrateDebugMarking() { String username = page.usernameCredential.getText(); String password = page.passwordCredential.getText(); page.username.setText(username); page.password.setText(password); page.login.click(); getBrowser().acceptAlertIfVisible(); }
protected String getExceptionScreenshotTag(String screenshotBaseName, String messageBase, Throwable t) { String screenshotTag = "(Screenshot not available)"; try { String screenShotFile = createScreenshot(screenshotBaseName, t); screenshotTag = getScreenshotLink(screenShotFile); } catch (UnhandledAlertException e) { // https://code.google.com/p/selenium/issues/detail?id=4412 System.err.println("Unable to take screenshot while alert is present for exception: " + messageBase); } catch (Exception sse) { System.err.println("Unable to take screenshot for exception: " + messageBase); sse.printStackTrace(); } return screenshotTag; }
private T invokeInFrames(WebDriver webDriver) { T result = null; List<WebElement> frames = webDriver.findElements(ConstantBy.frames()); for (WebElement frame : frames) { helper.switchToFrame(frame); try { result = decorated.apply(webDriver); if (isFinished(result)) { break; } else { result = invokeInFrames(webDriver); if (isFinished(result)) { break; } } } finally { // if we already had a problem with alerts at lower level, no need to try to go up again if (helper.getFrameDepthOnLastAlertError() == 0) { int depthOnAlert = helper.getCurrentFrameDepth(); try { helper.switchToParentFrame(); } catch (UnhandledAlertException e) { // we can't go up if there is an alert open. // we store the current depth so we might go back up when the alert is handled helper.storeFrameDepthOnAlertError(depthOnAlert - frameDepthOnStart); break; } } } } return result; }
private void waitUntilAlertIsNotPresent() { try { final FluentWait<WebDriver> wait = new WebDriverWait(webDriver, defaultTimeout().getSeconds()); wait.until(not(alertIsPresent())); } catch (TimeoutException e) { throw new UnhandledAlertException(e.getMessage()); } }
@When("^I accept the alert$") public void i_accept_the_alert() { try { alertPage.acceptAlert(); } catch (UnhandledAlertException e) { context.setAlertPresent(true); } }
@When("^I dismiss the alert$") public void i_dismiss_the_alert() { try { alertPage.dismissAlert(); } catch (UnhandledAlertException e) { context.setAlertPresent(true); } }
public boolean isValid() { if (delegate == null) return false; try { // IE driver keeps providing an empty window handles set even after the browser is closed return delegate.getWindowHandles() != null && delegate.getWindowHandles().size() > 0; } catch (WebDriverException e) { if (e instanceof UnhandledAlertException) { return true; } return false; } }
/** * Click "OK" on a modal dialog box (usually referred to * as a "javascript dialog"). */ public void acceptDialog() { try { logger.info("Accepting dialog."); this.wd.switchTo().alert().accept(); this.waitForAlertDismissal(); } catch(UnhandledAlertException uae) { logger.warning("Unhandled alert exception"); } }
/** * Dismisses a modal dialog box (usually referred to * as a "javascript dialog"). * */ public void dismissDialog() { try { logger.info("Dismissing dialog."); this.wd.switchTo().alert().dismiss(); this.waitForAlertDismissal(); } catch(UnhandledAlertException uae) { logger.warning("Unhandled alert exception"); } }
/** * Returns true if a modal dialog can be switched to * and switched back from; otherwise, returns false. * * @return Boolean true only if a modal dialog can * be switched to, then switched back from. */ public boolean isDialogVisible() { try { this.wd.switchTo().alert(); logger.info("Dialog present?: true."); return true; } catch(UnhandledAlertException uae) { logger.info("(Unhandled alert in FF?) Dialog present?: true. May have ignored dialog..."); return true; } catch(NoAlertPresentException nape) { logger.info("Dialog present?: false."); return false; } }
@Test public void blocksOtherCallsWhenAlertPresent() { driver().executeScript("setTimeout(function(){alert('alert present');}, 100)"); Alert a = new WebDriverWait(driver(), 2).until(ExpectedConditions.alertIsPresent()); Assert.assertEquals("alert present", a.getText()); try { driver().findElement(By.linkText("Foo")); throw new RuntimeException("should have gotten an UnhandledAlertException"); } catch (UnhandledAlertException uae) { // pass } finally { a.accept(); } }
@SuppressWarnings("unchecked") public DefaultOnUnhandledAlertInteractionListener() { super(UnhandledAlertException.class); }