/** * Closes the active Pop Up, assumes only 1 Pop Up is open * * @return */ public boolean closePopUp() { try { se.log().logSeStep("Closing PopUp:"); if (currentPopUp != null) { se.driver().switchTo().window(currentPopUp).close(); se.driver().switchTo().window(prevWindowHandle); se.util().sleep(1000); currentPopUp = null; prevWindowHandle = null; return true; } else { String currentWindow = se.driver().getWindowHandle(); Set<String> windows = se.driver().getWindowHandles(); for (Iterator<String> iterator = windows.iterator(); iterator.hasNext(); ) { String string = (String) iterator.next(); if (!currentWindow.equals(string)) { se.driver().switchTo().window(string).close(); se.driver().switchTo().window(currentWindow); se.util().sleep(1000); return true; } } } return false; } catch (Exception e) { se.log().logSeStep("Un-handled Exception in closePopUp:"); se.log().logSeStep(e.getMessage()); return false; } }
/** * Switch to a window containing a certain url * * @param url of window to switch to * @return */ public String switchToWindowByUrl(String url) { try { return getHandleByUrl(url); } catch (Exception e) { System.out.println("Un-handled Exception in switchToWindowByUrl:"); System.out.println(e.getMessage()); } return null; }
/** * Get the current url of the browser. * * @return The url as a string. */ public String getCurrentUrl() { try { return se.driver().getCurrentUrl(); } catch (Exception e) { System.out.println("Unhandled Exception in getCurrentUrl"); System.out.println(e.getMessage()); return null; } }
/** * executes javascript on the document and returns the result as a java Object * * @param javascript * @return */ public Object executeJavascriptReturnsObject(String javascript, boolean log) { if (log) se.log().trace("Executing Javascript:" + javascript); try { Object result = ((JavascriptExecutor) se.driver()).executeScript(javascript); if (result != null) return result; else return null; } catch (Exception e) { System.out.println(e.getMessage()); return null; } }
public boolean browse(WebLocator el) { try { el.focus(); Actions builder = new Actions(driver); builder.moveToElement(el.currentElement).perform(); builder.click().perform(); return true; } catch (Exception e) { LOGGER.error(e.getMessage(), e); return false; } }
/** * Executes javascript on an element and returns the result as a java String * * @param javascript snippit of javascript that returns a string * @param webElement Element to execute the javascript on, reference in snippit as "arguments[0]" * @return Returned value of the snippit */ public String executeJavascriptOnWebElement(String javascript, WebElement webElement) { se.log() .logSeStep( "Executing Javascript on WebElement:" + javascript + ", " + webElement.toString()); try { Object result = ((JavascriptExecutor) se.driver()).executeScript(javascript, webElement); if (result != null) return result.toString(); else return ""; } catch (Exception e) { System.out.println(e.getMessage()); return ""; } }
/** * Takes a screenshot and returns as a base64 encoded string. * * <p>Doesn't add to report directly. * * @return base64 encoded string of screenshot */ public String takeScreenShot() { if (se.driver() == null) return null; WebDriver driver = se.driver(); if (!(driver instanceof TakesScreenshot)) driver = (new Augmenter()).augment(driver); try { return ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64); } catch (Exception e) { se.log().trace(e.getMessage()); se.log().trace(e.getStackTrace().toString()); } se.util().sleep(500); return null; }
/** * Closes a window containing a url * * @param url of window to close (matched using string.contains) * @return true if successful */ public boolean closeWindowByUrl(String url) { try { se.log().logSeStep("Closing Window With Url:" + url); String windowHandle = getHandleByUrl(url); se.driver().switchTo().window(windowHandle); // return returnToPrevWindow(); return closeCurrentWindow(); } catch (Exception e) { se.log().logSeStep("Un-handled Exception in closeWindowByUrl:"); se.log().logSeStep(e.getMessage()); return false; } }
/** * Returns True of an Alert Pop Up is present * * @return */ public boolean hasAlert() { // Get a handle to the open alert, prompt or confirmation try { se.driver().switchTo().alert(); se.log().logSeStep("Has Alert"); return true; } catch (Exception e) { // no alert se.log().logSeStep("No Alert"); se.log().logSeStep(e.getMessage()); return false; } }
/** * Closes a window by the window title * * @param windowTitle * @return true if successful */ public boolean closeWindowByTitle(String windowTitle) { try { se.log().logSeStep("Closing Window With Title:" + windowTitle); String windowHandle = getHandleByTitle(windowTitle); se.driver().switchTo().window(windowHandle); // ((JavascriptExecutor) myDriver).executeScript("window.close()"); // return returnToPrevWindow(); return closeCurrentWindow(); } catch (Exception e) { se.log().logSeStep("Un-handled Exception in closeWindowByTitle:"); se.log().logSeStep(e.getMessage()); return false; } }
/** * Waits for the window title to contain windowTitle * * @param windowTitle Text for the window title to contain * @param timeOut Seconds to wait for the title to contain * @return True if WindowTitle found in title before timeOut */ public boolean waitForWindowTitleToContain(String windowTitle, int timeOut) { se.log() .logSeStep("Waiting for Window Title to contain " + windowTitle + " (" + timeOut + " sec)"); WebDriverWait wait = new WebDriverWait(se.driver(), timeOut); try { wait.until(windowTitleContains(windowTitle)); return true; } catch (TimeoutException e) { se.log().logSeStep("Timed out waiting for Window Title to contain " + windowTitle); return false; } catch (Exception e) { String errorName = "Un-handled Exception in waitForWindowTitleToContain:"; se.log().logSeStep(errorName + e.getMessage()); return false; } }
/** * Switch to a window using the title * * @param title of window * @return true of successful */ public String switchToWindow(String title) { try { if (prevWindow == null || prevWindow != title) { prevWindow = se.driver().getTitle(); prevWindowHandle = se.driver().getWindowHandle(); se.log().logSeStep("Saved Prev Window Title:" + prevWindow); } se.log().logSeStep("Switching To Window Title:" + title); return getHandleByTitle(title); } catch (Exception e) { se.log().logSeStep("Un-handled Exception in switchToWindow:"); se.log().logSeStep(e.getMessage()); } return null; }
/** * Used for initial get of first page Log.setTcStartTime(); is called. use get(url) to get pages * without overwriting the start time. * * @param url */ public boolean openUrl(String url) { se.log().logSeStep("Open URL: " + url); try { se.driver().get(url); deleteCookies(); se.log().setTcStartTime(); se.driver().get(url); } catch (Exception e) { se.log() .logTcError( String.format( "Unhandled Exception in openUrl %s: %s: %s: %s", url, e, e.getMessage(), ExceptionUtils.getFullStackTrace(e)), takeScreenShot()); return false; } return se.driver().getTitle() != null; }
/** * Load a new url in the browser. Does not log the test case start time. Use openUrl() instead, if * this is the first url loaded for the test. * * @param url */ public boolean get(String url) { se.log().logSeStep("Get URL: " + url); if (url == null) { se.log().logSeStep("url is null, nothing to get"); return false; } try { se.driver().get(url); } catch (Exception e) { System.out.println("Unhandled Exception in Browser.get " + url); System.out.println(e.getMessage()); return false; } if (se.driver().getTitle() == null || se.driver().getTitle().equals("Problem loading page") || se.driver().getTitle().contains("Oops! Google Chrome could not connect to") || se.driver().getTitle().equals("Internet Explorer cannot display the webpage")) { return false; } else return true; }
/** * Returns if Popup is present and switches context to the popup window if present. * * @return true if popup is present, and switches test context to the popup. */ public boolean isPopup() { try { se.log().logSeStep("Checking for PopUp:"); String currentWindow = se.driver().getWindowHandle(); Set<String> windows = se.driver().getWindowHandles(); for (Iterator<String> iterator = windows.iterator(); iterator.hasNext(); ) { String string = (String) iterator.next(); if (!currentWindow.equals(string)) { se.log().logSeStep("PopUp Found"); currentPopUp = string; prevWindowHandle = currentWindow; se.driver().switchTo().window(currentPopUp); se.log().logSeStep("Switching Context to PopUp window: " + se.driver().getTitle()); return true; } } return false; } catch (Exception e) { se.log().logSeStep("Un-handled Exception in isPopup:"); se.log().logSeStep(e.getMessage()); return false; } }