Пример #1
0
 /**
  * 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;
   }
 }
Пример #2
0
 /**
  * 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;
 }
Пример #3
0
 /**
  * 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;
   }
 }
Пример #4
0
 /**
  * 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;
   }
 }
Пример #5
0
 /**
  * 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;
   }
 }
Пример #6
0
 /**
  * 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;
   }
 }
Пример #7
0
 /**
  * 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;
   }
 }
Пример #8
0
  /**
   * 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;
    }
  }
Пример #9
0
  /**
   * 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;
  }
Пример #10
0
 /**
  * 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;
 }
Пример #11
0
 /**
  * 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;
   }
 }
Пример #12
0
 /**
  * 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;
 }
Пример #13
0
 /**
  * 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 "";
   }
 }
Пример #14
0
 /**
  * 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;
 }
Пример #15
0
 /**
  * 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;
   }
 }
Пример #16
0
 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;
   }
 }
Пример #17
0
 /**
  * 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);
 }
Пример #18
0
 /**
  * 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);
 }
Пример #19
0
 /** Refresh current page */
 public void refresh() {
   se.log().logSeStep("Refreshing Page");
   se.driver().navigate().refresh();
 }