Class AbstractUIContext

java.lang.Object
com.onenetwork.platform.tools.test.ui.AbstractUIContext
Direct Known Subclasses:
NeoUIContext, UIContext

public class AbstractUIContext
extends java.lang.Object
  • Field Summary

    Fields 
    Modifier and Type Field Description
    static java.lang.String CHROME_BROWSER_METHOD  
    protected org.openqa.selenium.WebDriver driver  
    static java.lang.String EDGE_BROWSER_METHOD  
    static java.lang.String EDGECHROMIUM_BROWSER_METHOD  
    static java.lang.String FIREFOX_BROWSER_METHOD  
    static java.lang.String IE_BROWSER_METHOD  
    protected long implicitWaitInSeconds
    Controls the timeout amount used by driver when searching for an element.
    protected long loginPageWait
    Controls the timeout amount used by login Page.
    protected long sleepInMilliseconds
    Controls the amount of time to sleep in between polls made by WebDriverWait.
    protected long timeoutInSeconds
    Controls the timeout amount used by WebDriverWait.
  • Constructor Summary

    Constructors 
    Constructor Description
    AbstractUIContext​(org.openqa.selenium.WebDriver driver)  
  • Method Summary

    Modifier and Type Method Description
    static void clearWebDriver()  
    void clickByJavascriptExecutor​(org.openqa.selenium.WebElement webElement)
    Utility if WebElement click() is not working.
    static void closeAllBrowserWindows()
    Close all open browser windows.
    protected void createDriver()
    For Chrome WebDriver the existing automated scripts may fail due to following reasons.
    void executeAutoIt​(java.lang.String scriptName, java.lang.String... arguments)
    Excutor for AutoIt Scripts AutoIt v3 is a freeware BASIC-like scripting language designed for automating the Windows GUI and general scripting.
    void export​(java.lang.String filePath)
    This method will download exported file at the given filePath.
    java.util.List<org.openqa.selenium.WebElement> findElementsWithoutImplicitWait​(org.openqa.selenium.WebElement webElement, org.openqa.selenium.By by)
    Find all elements within the current context using the given mechanism.
    void focusByJavascriptExecutor​(org.openqa.selenium.WebElement webElement)
    Utility to focus on element using javascript.
    double getBrowserVersion()
    Returns browser version from driver capabilities
    org.openqa.selenium.WebDriver getDriver()
    Returns the underlying WebDriver instance.
    java.lang.String getHiddenWebElementText​(org.openqa.selenium.WebElement webElement)
    If WebElement is hidden or not in view port getText() returns blank text.
    long getImplicitWaitInSeconds()
    Returns the value of implicitWaitInSeconds.
    long getLoginPageWait()
    Returns the value of loginPageWait.
    long getSleepInMilliseconds()
    Returns the value of sleepInMilliseconds.
    long getTimeoutInSeconds()
    Returns the value of timeoutInSeconds.
    static org.openqa.selenium.WebDriver getWebDriver()  
    boolean isChrome()
    Returns true if browser is Chrome
    boolean isEdge()
    Returns true if browser is Edge
    boolean isFirefox()
    Returns true if browser is Firefox
    boolean isIE()
    Returns true if browser is InternetExplorer
    boolean isSeleniumGrid()
    Returns true if current execution is using selenium grid
    boolean isTestNg()  
    boolean isWebElementExists​(org.openqa.selenium.SearchContext context, org.openqa.selenium.By by)
    Returns true if WebElement exists in SearchContext
    protected void logNodeDetails​(org.openqa.selenium.Capabilities capabilities)
    Capture the nodeDetails on which current driver is running.
    void mouseDownByJavascriptExecutor​(org.openqa.selenium.WebElement webElement)
    Utility to fire mouse down on element using javascript.
    protected void setDriverPath​(java.lang.String driverPath, java.lang.String driverProperty, java.lang.String driverExe)  
    void setImplcitWaitInSeconds​(long implicitWaitInSeconds)
    Sets the value of implicitWaitInSeconds, the driver should wait when searching for an element if it is not immediately present.
    void setImplicitWait​(long timeInSeconds)
    Specifies the amount of time timeoutInSeconds the driver should wait when searching for an element if it is not immediately present.
    void setLoginPageWait​(long loginPageWait)
    Sets the value of loginPageWait Which is used by the login page only.
    void setSleepInMilliseconds​(long sleepInMilliseconds)
    Sets the value of sleepInMilliseconds, which is used by the waitUntil* methods.
    void setTestNg​(boolean isTestNg)  
    void setTimeoutInSeconds​(long timeoutInSeconds)
    Sets the value of timeoutInSeconds, which is used by the waitUntil* methods.
    static void setWebDriver​(org.openqa.selenium.WebDriver driver)  
    void waitAndAcceptAlert()
    Waits for alert and accept it.
    static void waitFor​(java.lang.Long timeInMilliseconds)
    Waits for the specified amount of [timeInMilliseconds].
    <T> T waitUntil​(org.openqa.selenium.support.ui.ExpectedCondition<T> condition)
    Waits until the specified condition occurs, or until timeout.
    <T> T waitUntil​(org.openqa.selenium.support.ui.ExpectedCondition<T> condition, long timeOutInSeconds, long sleepInMilliseconds)
    Waits until the specified condition occurs, or until specified timeout.
    void waitUntilElementDisplayed​(org.openqa.selenium.SearchContext context, org.openqa.selenium.By elToFind)
    Given a search context and element query, waits until the element is displayed.
    org.openqa.selenium.WebElement waitUntilElementDisplayedAndReturn​(org.openqa.selenium.SearchContext context, org.openqa.selenium.By elToFind)
    Given a search context and element query, waits until the element is displayed and returns it.
    org.openqa.selenium.WebElement waitUntilElementExists​(org.openqa.selenium.By elToFind)
    Convenience function which calls waitUntilElementExists(SearchContext, By), passing the WebDriver instance as the SearchContext.
    org.openqa.selenium.WebElement waitUntilElementExists​(org.openqa.selenium.SearchContext context, org.openqa.selenium.By elToFind)
    Waits until a WebElement can be found with the properties provided by [elToFind], within the SearchContext provided by [context].
    org.openqa.selenium.WebElement waitUntilElementExists​(org.openqa.selenium.SearchContext context, org.openqa.selenium.By elToFind, long minWaitInMilliseconds)
    Similar to waitUntilElementExists(SearchContext, By), except it allows you to specify a minimum wait time; in other words, you can specify that this function should not return before X number of milliseconds has passed.
    org.openqa.selenium.WebElement waitUntilElementExistsWithTimeout​(org.openqa.selenium.SearchContext context, org.openqa.selenium.By elToFind, long paramTimeOutInSeconds)
    Similar to waitUntilElementExists(SearchContext, By), except it allows you to specify the timeout in seconds when an expectation is called.
    void waitUntilElementNotDisplayed​(org.openqa.selenium.SearchContext context, org.openqa.selenium.By elToFind)
    Given a search context and element query, waits until the no element is found, or the element is found but is not displayed.
    java.util.List<org.openqa.selenium.WebElement> waitUntilElementsExist​(org.openqa.selenium.SearchContext context, org.openqa.selenium.By elToFind)
    Similar to waitUntilElementExists(SearchContext, By), but returns all matching elements as soon as 1 or more are available.
    java.util.List<org.openqa.selenium.WebElement> waitUntilElementsExist​(org.openqa.selenium.SearchContext context, org.openqa.selenium.By elToFind, long minWaitInMilliseconds)
    Similar to waitUntilElementsExist(SearchContext, By), except it allows you to specify a minimum wait time; in other words, you can specify that this function should not return before X number of milliseconds has passed.
    void withNestedDriver​(UITask task)
    Given a WebDriver, executes the given UITask; whatever the original Window which was associated to the WebDriver at the beginning of the method is guaranteed to be restored before this method returns, regardless of any calls to switchTo()
    static java.lang.String xPathContainsClass​(java.lang.String className)
    Utility method which returns an XPath condition which will correctly search for the provided [className] within the element.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • AbstractUIContext

      public AbstractUIContext​(org.openqa.selenium.WebDriver driver)
  • Method Details

    • xPathContainsClass

      public static java.lang.String xPathContainsClass​(java.lang.String className)
      Utility method which returns an XPath condition which will correctly search for the provided [className] within the element.

      Usage: String xpath = "//div[" + xPathContainsClass("my-class") + "]";

      Parameters:
      className -
      Returns:
    • setTimeoutInSeconds

      public void setTimeoutInSeconds​(long timeoutInSeconds)
      Sets the value of timeoutInSeconds, which is used by the waitUntil* methods. The default value for the field is 15L.
      Parameters:
      timeoutInSeconds -
    • getTimeoutInSeconds

      public long getTimeoutInSeconds()
      Returns the value of timeoutInSeconds.
      Returns:
    • getImplicitWaitInSeconds

      public long getImplicitWaitInSeconds()
      Returns the value of implicitWaitInSeconds.
      Returns:
    • isTestNg

      public boolean isTestNg()
      Returns:
      the isTestNg
    • setTestNg

      public void setTestNg​(boolean isTestNg)
      Parameters:
      isTestNg - the isTestNg to set
    • createDriver

      protected void createDriver()
      For Chrome WebDriver the existing automated scripts may fail due to following reasons. - Compared to IE driver, Chrome driver is faster and hence we may need to provide explicit waits in automation scripts. - In some scenarios, Chrome driver does not support sendKeys() on elements like div and span or containers with multiple input elements. As a workaround sendKeys() can be replaced with Actions API of Selenium.
    • setDriverPath

      protected void setDriverPath​(java.lang.String driverPath, java.lang.String driverProperty, java.lang.String driverExe)
    • logNodeDetails

      protected void logNodeDetails​(org.openqa.selenium.Capabilities capabilities)
      Capture the nodeDetails on which current driver is running. Includes information such as Node IP, Browser Name and Browser Version
    • setImplcitWaitInSeconds

      public void setImplcitWaitInSeconds​(long implicitWaitInSeconds)
      Sets the value of implicitWaitInSeconds, the driver should wait when searching for an element if it is not immediately present. Default value is 0L. Not intented to override by client.
      Parameters:
      implicitWaitInSeconds -
    • getDriver

      public org.openqa.selenium.WebDriver getDriver()
      Returns the underlying WebDriver instance.
      Returns:
    • withNestedDriver

      public void withNestedDriver​(UITask task) throws java.lang.Exception
      Given a WebDriver, executes the given UITask; whatever the original Window which was associated to the WebDriver at the beginning of the method is guaranteed to be restored before this method returns, regardless of any calls to switchTo()
      Throws:
      java.lang.Exception
    • setImplicitWait

      public void setImplicitWait​(long timeInSeconds)
      Specifies the amount of time timeoutInSeconds the driver should wait when searching for an element if it is not immediately present.

      When searching for a single element, the driver should poll the page until the element has been found, or this timeout expires before throwing a NoSuchElementException. When searching for multiple elements, the driver should poll the page until at least one element has been found or this timeout has expired.

      The default value for the field is 0L. Increasing the implicit wait timeout should be used judiciously as it will have an adverse effect on test run time, especially when used with slower location strategies like XPath.

      Parameters:
      timeInSeconds - The amount of time to wait in seconds.
      timeInSeconds -
    • setSleepInMilliseconds

      public void setSleepInMilliseconds​(long sleepInMilliseconds)
      Sets the value of sleepInMilliseconds, which is used by the waitUntil* methods. The default value for the field is 20L.
      Parameters:
      sleepWaitInMilliseconds -
    • getSleepInMilliseconds

      public long getSleepInMilliseconds()
      Returns the value of sleepInMilliseconds.
      Returns:
    • closeAllBrowserWindows

      public static void closeAllBrowserWindows()
      Close all open browser windows. Generally use before running setup for a test.
    • waitFor

      public static void waitFor​(java.lang.Long timeInMilliseconds)
      Waits for the specified amount of [timeInMilliseconds].
      Parameters:
      timeInMilliseconds -
    • getLoginPageWait

      public long getLoginPageWait()
      Returns the value of loginPageWait.
      Returns:
    • setLoginPageWait

      public void setLoginPageWait​(long loginPageWait)
      Sets the value of loginPageWait Which is used by the login page only. waitUntil* method ignore explicitWait value and use loginPageWait if it's value is grater than 0. The default value for the field is 0L.
      Parameters:
      loginPageWaitInMilliseconds -
    • waitUntil

      public <T> T waitUntil​(org.openqa.selenium.support.ui.ExpectedCondition<T> condition)
      Waits until the specified condition occurs, or until timeout.
      Type Parameters:
      T -
      Parameters:
      condition -
      Returns:
    • waitUntil

      public <T> T waitUntil​(org.openqa.selenium.support.ui.ExpectedCondition<T> condition, long timeOutInSeconds, long sleepInMilliseconds)
      Waits until the specified condition occurs, or until specified timeout.
      Type Parameters:
      T -
      Parameters:
      condition -
      paramTimeOutInSeconds -
      Returns:
    • waitUntilElementExists

      public org.openqa.selenium.WebElement waitUntilElementExists​(org.openqa.selenium.By elToFind)
      Convenience function which calls waitUntilElementExists(SearchContext, By), passing the WebDriver instance as the SearchContext.
      Parameters:
      elToFind -
      Returns:
    • waitUntilElementExists

      public org.openqa.selenium.WebElement waitUntilElementExists​(org.openqa.selenium.SearchContext context, org.openqa.selenium.By elToFind)
      Waits until a WebElement can be found with the properties provided by [elToFind], within the SearchContext provided by [context].
    • waitUntilElementExists

      public org.openqa.selenium.WebElement waitUntilElementExists​(org.openqa.selenium.SearchContext context, org.openqa.selenium.By elToFind, long minWaitInMilliseconds)
      Similar to waitUntilElementExists(SearchContext, By), except it allows you to specify a minimum wait time; in other words, you can specify that this function should not return before X number of milliseconds has passed. This is useful when running tests in a browser such as Chrome or Firefox, where sometimes things get executed a little too fast causing the test to fail.
      Parameters:
      context -
      elToFind -
      Returns:
    • waitUntilElementExistsWithTimeout

      public org.openqa.selenium.WebElement waitUntilElementExistsWithTimeout​(org.openqa.selenium.SearchContext context, org.openqa.selenium.By elToFind, long paramTimeOutInSeconds)
      Similar to waitUntilElementExists(SearchContext, By), except it allows you to specify the timeout in seconds when an expectation is called. This is useful when running tests with slower response from server for specific WebElement
      Parameters:
      context -
      elToFind -
      paramTimeOutInSeconds -
      Returns:
    • waitUntilElementNotDisplayed

      public void waitUntilElementNotDisplayed​(org.openqa.selenium.SearchContext context, org.openqa.selenium.By elToFind)
      Given a search context and element query, waits until the no element is found, or the element is found but is not displayed.
      Parameters:
      context - search context
      elToFind - pattern for finding the element
    • waitUntilElementDisplayed

      public void waitUntilElementDisplayed​(org.openqa.selenium.SearchContext context, org.openqa.selenium.By elToFind)
      Given a search context and element query, waits until the element is displayed.
      Parameters:
      context - search context
      elToFind - pattern for finding the element
    • waitUntilElementDisplayedAndReturn

      public org.openqa.selenium.WebElement waitUntilElementDisplayedAndReturn​(org.openqa.selenium.SearchContext context, org.openqa.selenium.By elToFind)
      Given a search context and element query, waits until the element is displayed and returns it.
      Parameters:
      context - search context
      elToFind - pattern for finding the element
    • waitUntilElementsExist

      public java.util.List<org.openqa.selenium.WebElement> waitUntilElementsExist​(org.openqa.selenium.SearchContext context, org.openqa.selenium.By elToFind)
      Similar to waitUntilElementExists(SearchContext, By), but returns all matching elements as soon as 1 or more are available.
    • waitUntilElementsExist

      public java.util.List<org.openqa.selenium.WebElement> waitUntilElementsExist​(org.openqa.selenium.SearchContext context, org.openqa.selenium.By elToFind, long minWaitInMilliseconds)
      Similar to waitUntilElementsExist(SearchContext, By), except it allows you to specify a minimum wait time; in other words, you can specify that this function should not return before X number of milliseconds has passed. This is useful when running tests in a browser such as Chrome or Firefox, where sometimes things get executed a little too fast causing the test to fail.
    • findElementsWithoutImplicitWait

      public java.util.List<org.openqa.selenium.WebElement> findElementsWithoutImplicitWait​(org.openqa.selenium.WebElement webElement, org.openqa.selenium.By by)
      Find all elements within the current context using the given mechanism. When using xpath be aware that webdriver follows standard conventions: a search prefixed with "//" will search the entire document, not just the children of this current node. Use ".//" to limit your search to the children of this WebElement.
      Parameters:
      webElement - WebElement
      by - The locating mechanism to use
      Returns:
      A list of all WebElements, or an empty list if nothing matches.
    • clickByJavascriptExecutor

      public void clickByJavascriptExecutor​(org.openqa.selenium.WebElement webElement)
      Utility if WebElement click() is not working.
      Parameters:
      webElement -
    • focusByJavascriptExecutor

      public void focusByJavascriptExecutor​(org.openqa.selenium.WebElement webElement)
      Utility to focus on element using javascript.
      Parameters:
      webElement -
    • mouseDownByJavascriptExecutor

      public void mouseDownByJavascriptExecutor​(org.openqa.selenium.WebElement webElement)
      Utility to fire mouse down on element using javascript.
      Parameters:
      webElement -
    • getHiddenWebElementText

      public java.lang.String getHiddenWebElementText​(org.openqa.selenium.WebElement webElement)
      If WebElement is hidden or not in view port getText() returns blank text. Work around utility to work in this case.
      Parameters:
      webElement -
      Returns:
    • waitAndAcceptAlert

      public void waitAndAcceptAlert()
      Waits for alert and accept it.
    • export

      public void export​(java.lang.String filePath)
      This method will download exported file at the given filePath. It will work for Chrome, Internet Explorer and Microsoft Edge browsers only. AutoIt export will handle native save file window for Internet Explorer and Edge browser. For Chrome file will be downloaded to a default location (can be set in commancenter.properties as 'chrome.download.dir') and later moved to given filePath.
      Parameters:
      filePath - Path where file will be exported
      Throws:
      java.lang.Exception
    • executeAutoIt

      public void executeAutoIt​(java.lang.String scriptName, java.lang.String... arguments)
      Excutor for AutoIt Scripts AutoIt v3 is a freeware BASIC-like scripting language designed for automating the Windows GUI and general scripting. It has functions which waits until Native window opens so we don't need to put hard coded wait. We just need to write AutoIT script which on compilation converted into a stand-alone executable which can be run on computers that do not have the AutoIt interpreter installed. (for more info - http://www.autoitscript.com/site/autoit/)
      Parameters:
      scriptName - Name of script without extension
      arguments - Optional variable arguments for script
    • getBrowserVersion

      public double getBrowserVersion()
      Returns browser version from driver capabilities
      Returns:
    • isIE

      public boolean isIE()
      Returns true if browser is InternetExplorer
      Returns:
    • isChrome

      public boolean isChrome()
      Returns true if browser is Chrome
      Returns:
    • isEdge

      public boolean isEdge()
      Returns true if browser is Edge
      Returns:
    • isFirefox

      public boolean isFirefox()
      Returns true if browser is Firefox
      Returns:
    • isWebElementExists

      public boolean isWebElementExists​(org.openqa.selenium.SearchContext context, org.openqa.selenium.By by)
      Returns true if WebElement exists in SearchContext
      Returns:
    • isSeleniumGrid

      public boolean isSeleniumGrid()
      Returns true if current execution is using selenium grid
      Returns:
    • setWebDriver

      public static void setWebDriver​(org.openqa.selenium.WebDriver driver)
    • clearWebDriver

      public static void clearWebDriver()
    • getWebDriver

      public static org.openqa.selenium.WebDriver getWebDriver()