Class Element

java.lang.Object
com.dougnoel.sentinel.elements.Element
Direct Known Subclasses:
SelectElement, Table, Textbox, WindowsElement

public class Element extends Object
Base element class that handles creation of element types and allows late binding. It takes a selector type, a selector value and a driver, and uses that to exercise Selenium WebDriver functionality. Classes that extend this are wrappers to allow clearer coding conventions. For example a Button element just allows the specific declaration of a button on a web page for functionality.

Selenium already has a WebElement class, but using it requires you to be able to see the elements on the page as soon as you declare the page object. This doesn't work, since we want to define the page before run time. The solution to the problem is late binding, or declaring the objects as they are used. Every time we use an element on the page, we look for it at the time we are using it.

Note: Renamed from WebElement to Element to avoid name space conflicts with selenium's WebElement object when Selenium's WebElement type was needed as a return type for the element() function.

To Do:

  • Remove the need to pass in a driver class instance for the creation of every element and delay that binding to a singleton class that can be changed, allowing the use of multiple drivers through a testing session.
  • Look into creating a Selenium WebElement member variable to store the element after the first time it is used. This could increase the speed of calling objects that get called often.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final String
     
    protected static final org.apache.logging.log4j.Logger
     
    protected String
     
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Element(String elementType, String elementName, Map<String,String> selectors)
     
    Element(String elementName, Map<String,String> selectors)
    The constructor for a WebElement to initialize how an element is going to be found when it is worked on by the WebDriver class.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    attributeContains(String attribute, String value)
    Returns true if the element has an attribute containing to the value passed; otherwise returns false.
    boolean
    attributeEquals(String attribute, String value)
    Returns true if the element has an attribute equal to the value passed; otherwise returns false.
    boolean
    Returns true if the element class contains the value passed; otherwise returns false.
    Clear a Element.
    Click an Element.
    private boolean
    Loops through all the ways to click an element.
    private void
    clickPositionAction(org.openqa.selenium.Point point, org.openqa.selenium.WebElement element)
    Helper function to move the mouse to a point on an element and clicks there.
    clickPositionOnChildElement(org.openqa.selenium.Point point, org.openqa.selenium.By elementBy)
    Clicks a point relative to the center of the element.
    clickPositionOnElement(org.openqa.selenium.Point point)
    Clicks a point relative to the center of the element.
    private org.openqa.selenium.support.ui.FluentWait<org.openqa.selenium.WebDriver>
    Creates a FluentWait Webdriver object for searching for elements.
    Context clicks (right click) on an element.
    protected org.openqa.selenium.By
    createByLocator(SelectorType selectorType, String selectorValue)
    Takes a Sentinel SelectorType and string value and returns a Selenium By locator.
    boolean
    Returns true if an element is neither found nor displayed otherwise false.
    boolean
    Returns true if the attribute does not exist for the element; otherwise returns false.
    Drags the current element on top of the target element.
    private org.openqa.selenium.WebDriver
     
    protected org.openqa.selenium.WebElement
    Returns the Selenium WebElement if it can be found on the current page.
    protected org.openqa.selenium.WebElement
    element(org.openqa.selenium.By locator)
    Returns a Selenium WebElement using the current element as the beginning search point for the passed By locator.
    boolean
    Performs exactly 1 sweep across the DOM for the element (includes all selectors).
    protected org.openqa.selenium.WebElement
    Searches for the current element within the current frame context.
    protected org.openqa.selenium.WebElement
    Searches recursively through any iFrames on the page for the element.
    getAttribute(String attribute)
    From selenium's javadoc:
    Get the value of the given attribute of the element.
    Returns the background color of the element, or it's inherited background color from a parent if transparent.
    private Color
    getBackgroundColor(org.openqa.selenium.WebElement element)
    Returns the background color of the element, or it's inherited background color from a parent if transparent.
    org.openqa.selenium.By
    Returns the first stored selector for this element.
    private org.openqa.selenium.WebElement
    getElement(org.openqa.selenium.By locator)
    Searches for an element using the given locator and executes immediately
    org.openqa.selenium.Point
    Returns a Point object that represents the top-left corner of the element.
    Returns the most prevalent color present in the element.
    Returns the name of the element as it is stored in the yaml file.
    Returns a screenshot File of the current element.
    Returns the text of the page element as a String.
    Returns the tooltip value of the element.
    boolean
    hasAttribute(String attribute)
    Returns true if the attribute exists for the element; otherwise returns false.
    Hovers over an element using Actions.
    boolean
    Returns true if the element is disabled or readonly; false otherwise Expects the element to be disabled, and if it is not, this method will check every 10 milliseconds until it is up to the configured timeout time (10 second default).
    boolean
    Returns true if the element is displayed, otherwise returns false if it is hidden/invisible.
    boolean
    Returns true if the element is enabled and doesn't have the attribute readonly; false otherwise.
    boolean
    Returns true if the element is hidden, otherwise returns false if it is visible/displayed.
    boolean
    Returns true if the element is not selected; false if it is.
    boolean
    Returns true if the element is selected; false if it is not.
    sendFilePaths(List<String> fileLocators)
    Sends a constructed collection of absolute paths of a given file locator string list to an element for use with file upload elements.
    Enter text into a Element.
    private boolean
    Loops through all the ways to send text to an element.
    sendSpecialKey(org.openqa.selenium.Keys key)
    Sends a unicode character to the element directly.
    waitForText(String text, boolean present)
    Waits until the text contains a certain value, and returns if it was found

    Methods inherited from class java.lang.Object

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

    • log

      protected static final org.apache.logging.log4j.Logger log
    • selectors

      protected Map<SelectorType,String> selectors
    • name

      protected String name
    • elementType

      protected final String elementType
  • Constructor Details

    • Element

      public Element(String elementName, Map<String,String> selectors)
      The constructor for a WebElement to initialize how an element is going to be found when it is worked on by the WebDriver class.
      Parameters:
      elementName - String the element name
      selectors - Map the various selectors to iterate through to find the element
    • Element

      public Element(String elementType, String elementName, Map<String,String> selectors)
  • Method Details

    • driver

      private org.openqa.selenium.WebDriver driver()
    • getName

      public String getName()
      Returns the name of the element as it is stored in the yaml file.
      Returns:
      String the name of the element
    • getElement

      private org.openqa.selenium.WebElement getElement(org.openqa.selenium.By locator)
      Searches for an element using the given locator and executes immediately
      Parameters:
      locator - By Selenium By locator
      Returns:
      org.openqa.selenium.WebElement the Selenium WebElement if found, otherwise null
    • createByLocator

      protected org.openqa.selenium.By createByLocator(SelectorType selectorType, String selectorValue)
      Takes a Sentinel SelectorType and string value and returns a Selenium By locator.
      Parameters:
      selectorType - SelectorType the ENUM value indicating how we search for the element
      selectorValue - String the value being pulled from the config file for the selector
      Returns:
      org.openqa.selenium.By returns a Selenium By selector for locating an element.
    • element

      protected org.openqa.selenium.WebElement element()
      Returns the Selenium WebElement if it can be found on the current page. Provides late binding for elements so that the driver does not look for them until they are called, at which point the driver should be on the correct page.
      Returns:
      org.openqa.selenium.WebElement the Selenium WebElement object type that can be acted upon
    • element

      protected org.openqa.selenium.WebElement element(org.openqa.selenium.By locator)
      Returns a Selenium WebElement using the current element as the beginning search point for the passed By locator.
      Parameters:
      locator - org.openqa.selenium.By the locator to use to find the element inside of this one
      Returns:
      org.openqa.selenium.WebElement the Selenium WebElement object type that can be acted upon
    • findElementInIFrame

      protected org.openqa.selenium.WebElement findElementInIFrame()
      Searches recursively through any iFrames on the page for the element. Returns null if the element is not found, or if there are no iFrames on the page. This method traverses through iFrames but returns to the default root context upon returning.
      Returns:
      WebElement the element if it is found, otherwise null
    • findElementInCurrentFrame

      protected org.openqa.selenium.WebElement findElementInCurrentFrame()
      Searches for the current element within the current frame context. Searches each selector.
      Returns:
      WebElement the element if it is found, otherwise null
    • sendFilePaths

      public Element sendFilePaths(List<String> fileLocators)
      Sends a constructed collection of absolute paths of a given file locator string list to an element for use with file upload elements. Supports both single and multiple file sending for input elements. This method will throw an IOException if the files at the given paths cannot be found or the provided file is a directory.
      Parameters:
      fileLocators - String the file locators or paths to upload
      Returns:
      Element (for chaining)
    • sendKeys

      public Element sendKeys(String text)
      Enter text into a Element. Typically used for text boxes. This method will throw an ElementDisabledException() if the text box is disabled.
      Parameters:
      text - String the text to enter
      Returns:
      Element (for chaining)
    • sendSpecialKey

      public Element sendSpecialKey(org.openqa.selenium.Keys key)
      Sends a unicode character to the element directly. Does not clear element before sending. Does not perform any checks to confirm that the element received the key. For use with special characters, such as BACKSPACE.
      Parameters:
      key - Keys the key or character to send
      Returns:
      Element (for chaining)
    • sendKeysLoop

      private boolean sendKeysLoop(String text)
      Loops through all the ways to send text to an element.
      Parameters:
      text - String the text to send
      Returns:
      boolean true if the text was sent, false otherwise
    • click

      public Element click()
      Click an Element.
      Returns:
      Element (for chaining)
    • clickLoop

      private boolean clickLoop()
      Loops through all the ways to click an element.
      Returns:
      boolean true if the element was clicked, false otherwise
    • contextClick

      public Element contextClick()
      Context clicks (right click) on an element.
      Returns:
      Element (for chaining)
    • constructElementWait

      private org.openqa.selenium.support.ui.FluentWait<org.openqa.selenium.WebDriver> constructElementWait(Duration timeout)
      Creates a FluentWait Webdriver object for searching for elements.
      Parameters:
      timeout - Duration how long to search
      Returns:
      FluentWait the wait object that can be invoked
    • clear

      public Element clear()
      Clear a Element. Clears text in a text box. Un-checks check boxes. Clears radio button choices.
      Returns:
      Element (for chaining)
    • dragAndDrop

      public Element dragAndDrop(Element target) throws IOException
      Drags the current element on top of the target element.
      Parameters:
      target - Element the element the target is being dragged and dropped onto
      Returns:
      Element (for chaining)
      Throws:
      IOException - if the drag and drop javascript file cannot be loaded
    • isDisplayed

      public boolean isDisplayed()
      Returns true if the element is displayed, otherwise returns false if it is hidden/invisible.

      NOTE: Use isInvisible() for the fastest processing time if you expect the element to be hidden/invisible.

      Returns:
      boolean true if the element is displayed; false if it is hidden.
    • isHidden

      public boolean isHidden()
      Returns true if the element is hidden, otherwise returns false if it is visible/displayed.

      NOTE: Use isDisplayed() for the fastest processing time if you expect the element to be visible/displayed.

      Returns:
      boolean true if the element is hidden; false if it is displayed.
    • isEnabled

      public boolean isEnabled()
      Returns true if the element is enabled and doesn't have the attribute readonly; false otherwise. Expects the element to be enabled, and if it is not, this method will check every 10 milliseconds until it is up to the configured timeout time (10 second default).

      NOTE: Use isDisabled() for the fastest processing time if you expect the element to be disabled.

      Returns:
      boolean true if the element is enabled; false if it is disabled
    • isDisabled

      public boolean isDisabled()
      Returns true if the element is disabled or readonly; false otherwise Expects the element to be disabled, and if it is not, this method will check every 10 milliseconds until it is up to the configured timeout time (10 second default).

      NOTE: Use isEnabled() for the fastest processing time if you expect the element to be enabled.

      Returns:
      boolean true if the element is disabled; false if it is enabled
    • isSelected

      public boolean isSelected()
      Returns true if the element is selected; false if it is not.

      NOTE: Use isNotSelected() for the fastest processing time if you expect the element to not be selected.

      Returns:
      boolean true if the element is selected, false if it is not
    • isNotSelected

      public boolean isNotSelected()
      Returns true if the element is not selected; false if it is.

      NOTE: Use isSelected() for the fastest processing time if you expect the element to be selected.

      Returns:
      boolean true if the element is not selected, false if it is
    • doesNotExist

      public boolean doesNotExist()
      Returns true if an element is neither found nor displayed otherwise false. Will poll every selector on the page object in a loop until the timeout is reached. This should be used when you expect an element to not be present and do not want to slow down your tests waiting for the normal timeout time to expire.
      Returns:
      boolean true if the element cannot be found, false if it is found
    • getText

      public String getText()
      Returns the text of the page element as a String.
      Returns:
      String the text value stored in the element
    • waitForText

      public Boolean waitForText(String text, boolean present)
      Waits until the text contains a certain value, and returns if it was found
      Returns:
      Boolean If the text value was found in the element.
    • hasAttribute

      public boolean hasAttribute(String attribute)
      Returns true if the attribute exists for the element; otherwise returns false. Expects the attribute to exist, and if it does not, this method will check every 10 milliseconds up until to the configured timeout time (10 second default).

      NOTE: Use doesNotHaveAttribute() for the fastest processing time if you expect the element to not have the attribute.

      Parameters:
      attribute - String the attribute for which to test
      Returns:
      true if the attribute exists for the element; otherwise false
    • doesNotHaveAttribute

      public boolean doesNotHaveAttribute(String attribute)
      Returns true if the attribute does not exist for the element; otherwise returns false. Expects the attribute to not exist, and if it does, this method will check every 10 milliseconds up until to the configured timeout time (10 second default).

      NOTE: Use hasAttribute() for the fastest processing time if you expect the element to have the attribute.

      Parameters:
      attribute - String the attribute for which to test
      Returns:
      true if the attribute does not exist for the element; otherwise false
    • attributeEquals

      public boolean attributeEquals(String attribute, String value)
      Returns true if the element has an attribute equal to the value passed; otherwise returns false.

      Examples:

      • Determine if an element has a style="display:none" attribute set.
      Parameters:
      attribute - String the attribute to look for
      value - String the value to which attribute should be set
      Returns:
      boolean true if the element as an attribute equal to the value passed; otherwise returns false
    • attributeContains

      public boolean attributeContains(String attribute, String value)
      Returns true if the element has an attribute containing to the value passed; otherwise returns false.

      Examples:

      • Determine if "display: block;" is within an element that has a style="display: block; color: blue;" attribute set.
      Parameters:
      attribute - String the attribute to look for
      value - String the value to which attribute should be set
      Returns:
      boolean true if the element as an attribute containing the value passed; otherwise returns false
    • classContains

      public boolean classContains(String value)
      Returns true if the element class contains the value passed; otherwise returns false.

      Examples:

      • Determine if "blob-code-context" is within an element that has a class="blob-code blob-code-context js-file-line" attribute set.
      Parameters:
      value - String the value to which attribute should be set
      Returns:
      boolean true if the element as an attribute containing the value passed; otherwise returns false
    • clickPositionOnElement

      public Element clickPositionOnElement(org.openqa.selenium.Point point)
      Clicks a point relative to the center of the element. To click the top right corner of an element of a width of 20 and a height of 10, you would need to pass Point(10, 5)
      Parameters:
      point - The offset from the center of the element to click on
      Returns:
      Element (for chaining)
    • clickPositionOnChildElement

      public Element clickPositionOnChildElement(org.openqa.selenium.Point point, org.openqa.selenium.By elementBy)
      Clicks a point relative to the center of the element. To click the top right corner of an element of a width of 20 and a height of 10, you would need to pass Point(10, 5)
      Parameters:
      point - The offset from the center of the element to click on
      Returns:
      Element (for chaining)
    • clickPositionAction

      private void clickPositionAction(org.openqa.selenium.Point point, org.openqa.selenium.WebElement element)
      Helper function to move the mouse to a point on an element and clicks there.
      Parameters:
      point - Point the point to click on
      element - WebElement the element to click
    • hover

      public Element hover()
      Hovers over an element using Actions.
      Returns:
      Element for chaining
    • getTooltipText

      public String getTooltipText()
      Returns the tooltip value of the element.
      Returns:
      String the value of the tooltip text
    • getMostPrevalentColor

      public Color getMostPrevalentColor() throws IOException
      Returns the most prevalent color present in the element.
      Returns:
      java.awt.Color the most common color in the element
      Throws:
      IOException
    • getScreenshot

      public File getScreenshot()
      Returns a screenshot File of the current element.
      Returns:
      File a screenshot of the current element
    • getBackgroundColor

      public Color getBackgroundColor()
      Returns the background color of the element, or it's inherited background color from a parent if transparent.
      Returns:
      java.awt.Color the background color of the element or first parent with background color. White if only transparency is found
    • getBackgroundColor

      private Color getBackgroundColor(org.openqa.selenium.WebElement element)
      Returns the background color of the element, or it's inherited background color from a parent if transparent.
      Parameters:
      element - WebElement the web element to get the background color of
      Returns:
      java.awt.Color the background color of the element or first parent with background color. White if only transparency is found
    • getAttribute

      public String getAttribute(String attribute)

      From selenium's javadoc:
      Get the value of the given attribute of the element. Will return the current value, even if this has been modified after the page has been loaded.
      More exactly, this method will return the value of the property with the given name, if it exists. If it does not, then the value of the attribute with the given name is returned. If neither exists, null is returned.
      The "style" attribute is converted as best can be to a text representation with a trailingsemi-colon.
      The following are deemed to be "boolean" attributes, and will return either "true" or null: async, autofocus, autoplay, checked, compact, complete, controls, declare, defaultchecked, defaultselected, defer, disabled, draggable, ended, formnovalidate, hidden, indeterminate, iscontenteditable, ismap, itemscope, loop, multiple, muted, nohref, noresize, noshade, novalidate, nowrap, open, paused, pubdate, readonly, required, reversed, scoped, seamless, seeking, selected, truespeed, willvalidate
      Finally, the following commonly mis-capitalized attribute/property names are evaluated as expected:
      •If the given name is "class", the "className" property is returned.
      •If the given name is "readonly", the "readOnly" property is returned. Note: The reason for this behavior is that users frequently confuse attributes andproperties. If you need to do something more precise, e.g., refer to an attribute even when aproperty of the same name exists, then you should evaluate Javascript to obtain the resultyou desire.

      Gets the value of the given attribute on this element.
      Parameters:
      attribute - String name of the attribute to get the value of
      Returns:
      String the value of the given attribute, or null if it is not set.
    • getLocation

      public org.openqa.selenium.Point getLocation()
      Returns a Point object that represents the top-left corner of the element.
      Returns:
      Point the coordinates of the top-left corner of the element
    • getBy

      public org.openqa.selenium.By getBy()
      Returns the first stored selector for this element.
      Returns:
      By the selector for the element listed in the page object
    • existsAtThisInstant

      public boolean existsAtThisInstant()
      Performs exactly 1 sweep across the DOM for the element (includes all selectors). Does not loop for the full timeout.
      Returns:
      boolean true if the element is found. false otherwise.