Class Element
- Direct Known Subclasses:
SelectElement
,Table
,Textbox
,WindowsElement
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
FieldsModifier and TypeFieldDescriptionprotected final String
protected static final org.apache.logging.log4j.Logger
protected String
protected Map<SelectorType,
String> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
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
classContains
(String value) Returns true if the element class contains the value passed; otherwise returns false.clear()
Clear a Element.click()
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>
constructElementWait
(Duration timeout) 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
doesNotHaveAttribute
(String attribute) Returns true if the attribute does not exist for the element; otherwise returns false.dragAndDrop
(Element target) Drags the current element on top of the target element.private org.openqa.selenium.WebDriver
driver()
protected org.openqa.selenium.WebElement
element()
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
getBy()
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 immediatelyorg.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.getName()
Returns the name of the element as it is stored in the yaml file.Returns a screenshot File of the current element.getText()
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.hover()
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
isHidden()
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
sendKeysLoop
(String text) 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
-
Field Details
-
log
protected static final org.apache.logging.log4j.Logger log -
selectors
-
name
-
elementType
-
-
Constructor Details
-
Element
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 nameselectors
- Map the various selectors to iterate through to find the element
-
Element
-
-
Method Details
-
driver
private org.openqa.selenium.WebDriver driver() -
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
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 elementselectorValue
- 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
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
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
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
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
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
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
Clear a Element. Clears text in a text box. Un-checks check boxes. Clears radio button choices.- Returns:
- Element (for chaining)
-
dragAndDrop
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
Returns the text of the page element as a String.- Returns:
- String the text value stored in the element
-
waitForText
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
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
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
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 forvalue
- 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
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 forvalue
- 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
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
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 onelement
- WebElement the element to click
-
hover
Hovers over an element using Actions.- Returns:
- Element for chaining
-
getTooltipText
Returns the tooltip value of the element.- Returns:
- String the value of the tooltip text
-
getMostPrevalentColor
Returns the most prevalent color present in the element.- Returns:
- java.awt.Color the most common color in the element
- Throws:
IOException
-
getScreenshot
Returns a screenshot File of the current element.- Returns:
- File a screenshot of the current element
-
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
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
From selenium's javadoc:
Gets the value of the given attribute on this element.
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.- 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.
-