/** * Gets the browser name * * @return */ public String getBrowserName() { if (se.currentBrowser() == Browsers.HtmlUnit) return "HtmlUnit"; else { Capabilities browserCapabilities = ((RemoteWebDriver) se.driver()).getCapabilities(); return browserCapabilities.getBrowserName(); } }
/** * Get the window handle by iterating all windows searching for the url. * * @param url * @return Window handle of window with the specified url */ private String getHandleByUrl(String url) { String currentHandle = se.driver().getWindowHandle(); for (String handle : se.driver().getWindowHandles()) { if ((se.driver().switchTo().window(handle).getCurrentUrl()).contains(url)) { return handle; } } return currentHandle; }
/** * 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; } }
/** * Get the window handle by iterating all windows searching for the title. * * @param title * @return Window handle of window with the specified title. */ private String getHandleByTitle(String title) { String currentHandle = se.driver().getWindowHandle(); for (String handle : se.driver().getWindowHandles()) { if (title.equals(se.driver().switchTo().window(handle).getTitle())) { se.log().trace("Found new window handle by title: " + handle); return handle; } } se.log().trace("Could not find new window handle by title"); return currentHandle; }
/** * Return to the previous window, previous window was saved by swithToWindow() * * @return true of successful */ public boolean returnToPrevWindow() { if (prevWindowHandle == null) { se.log().logSeStep("No Prev Window Handle Stored"); se.driver().switchTo().defaultContent(); return false; } else { se.log().logSeStep("Switching to Prev Window Handle " + prevWindowHandle); se.driver().switchTo().window(prevWindowHandle); prevWindow = null; return true; } }
/** * 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; } }
/** * Close browser and browser session. * * <p>!! Should be called after all tests !! */ public void quit() { try { if (se.driver() instanceof RemoteWebDriver && ((RemoteWebDriver) se.driver()).getSessionId() != null) { se.driver().close(); se.driver().quit(); } else { se.driver().quit(); } } catch (Exception e) { // chrome drive quit throws errors when window is already closed } }
/** * 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; } }
/** * 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 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; } }
/** * 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; } }
/** * Close an Alert Pop Up * * @return */ public boolean closeAlert() { // Get a handle to the open alert, prompt or confirmation try { Alert alert = se.driver().switchTo().alert(); // And acknowledge the alert (equivalent to clicking "OK") alert.accept(); se.log().logSeStep("Closing Alert"); return true; } catch (Exception e) { // no alert se.log().logSeStep("Cannot Close Alert"); e.printStackTrace(); 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; }
/** * 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; } }
/** * 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; } }
public boolean isMobileDevice() { Capabilities browserCapabilities = ((RemoteWebDriver) se.driver()).getCapabilities(); String browserName = browserCapabilities.getBrowserName(); Platform platform = browserCapabilities.getPlatform(); if (browserName.equals("android") && platform.equals(Platform.ANDROID)) return true; else if ((browserName.equals("iOS") || browserName.equals("iPhone")) && platform.equals(Platform.MAC)) return true; else return false; }
public String switchToWindow(int windowIndex) { prevWindow = se.driver().getTitle(); prevWindowHandle = se.driver().getWindowHandle(); se.log().logSeStep("Switching To Window Index:" + windowIndex); try { Set<String> windows = se.driver().getWindowHandles(); windows.remove(prevWindowHandle); List<String> listOfWindows = new ArrayList<String>(windows); if (windowIndex <= windows.size()) ; se.driver().switchTo().window(listOfWindows.get(windowIndex)); return se.driver().getWindowHandle(); } catch (Exception e) { return null; } }
/** * 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; } }
/** * 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; }
/** * Get current browser type * * @return */ public Browsers getBrowserType() { return se.currentBrowser(); }
/** * Get a cookie named cookieName * * @param cookieName Name of cookie to get * @return Cookie object */ public Cookie getCookie(String cookieName) { se.log().logSeStep("Get Cookie"); return se.driver().manage().getCookieNamed(cookieName); }
/** * Adds a Cookie to the current domain * * @param name Cookie Name * @param value Cookie Value * @param domain Cookie Domain * @param path Cookie path * @param expiry Date of expiration * @param isSecure True if secure */ public void addCookie( String name, String value, String domain, String path, Date expiry, boolean isSecure) { Cookie cookie = new Cookie(name, value, domain, path, expiry, isSecure); se.log().logSeStep("Creating Cookie:" + cookie); se.driver().manage().addCookie(cookie); }
/** Closes all open windows */ public void closeAllWindows() { for (String handle : se.driver().getWindowHandles()) { se.driver().switchTo().window(handle).close(); } }
/** * Closes current window and returns to the previous window, if this is the last window the * browser will quit * * @return true if able to return to previous window, false of no more windows are open */ public boolean closeCurrentWindow() { se.driver().close(); return returnToPrevWindow(); }
/** * Gets the window Title * * @return */ public String getWindowTitle() { return se.driver().getTitle(); }
/** Given the frame ID it will break out frame to the current window */ public void breakOutFrame(int iFrameNum) { get(se.element().getAttribute(By.tagName("iframe"), "src", iFrameNum)); }
/** Given the frame ID it will break out frame to the current window */ public void breakOutFrame(String iFrameID) { get(se.element().getAttribute(By.id(iFrameID), "src")); }
/** Prints the title and the window handle of the current window to the console */ public void printWindowInfo() { System.out.println(se.driver().getTitle()); System.out.println(se.driver().getWindowHandles().size()); System.out.println(se.driver().getWindowHandle()); }