Exemplo n.º 1
0
 /**
  * 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();
   }
 }
Exemplo n.º 2
0
 /**
  * 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;
 }
Exemplo n.º 3
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;
   }
 }
Exemplo n.º 4
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;
 }
Exemplo n.º 5
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;
   }
 }
Exemplo n.º 6
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;
   }
 }
Exemplo n.º 7
0
 /**
  * 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
   }
 }
Exemplo n.º 8
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;
   }
 }
Exemplo n.º 9
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 "";
   }
 }
Exemplo n.º 10
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;
 }
Exemplo n.º 11
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;
   }
 }
Exemplo n.º 12
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;
   }
 }
Exemplo n.º 13
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;
   }
 }
Exemplo n.º 14
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;
  }
Exemplo n.º 15
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;
    }
  }
Exemplo n.º 16
0
  /**
   * 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;
    }
  }
Exemplo n.º 17
0
  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;
  }
Exemplo n.º 18
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;
   }
 }
Exemplo n.º 19
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;
   }
 }
Exemplo n.º 20
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;
 }
Exemplo n.º 21
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;
 }
Exemplo n.º 22
0
 /**
  * Get current browser type
  *
  * @return
  */
 public Browsers getBrowserType() {
   return se.currentBrowser();
 }
Exemplo n.º 23
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);
 }
Exemplo n.º 24
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);
 }
Exemplo n.º 25
0
  /** Closes all open windows */
  public void closeAllWindows() {

    for (String handle : se.driver().getWindowHandles()) {
      se.driver().switchTo().window(handle).close();
    }
  }
Exemplo n.º 26
0
 /**
  * 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();
 }
Exemplo n.º 27
0
 /**
  * Gets the window Title
  *
  * @return
  */
 public String getWindowTitle() {
   return se.driver().getTitle();
 }
Exemplo n.º 28
0
 /** 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));
 }
Exemplo n.º 29
0
 /** 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"));
 }
Exemplo n.º 30
0
 /** 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());
 }