Skip to main content

Element State

ZWL provides several easy to use functions to get element's current state (such as checked, visibility, attributes).

Checking whether element is selected#

Works for html elements that keep a selected state such as checkbox, radio.

isSelected = isElementSelected(elemId) # Tells whether the given element is selected
anySelected = anyElementSelected([elemId, elemId...])# Tells whether any of the given element is selected. Useful for testing elements that# allows only one of the element to be selected from a group such as `radio` group. It takes# list of elements which can be retrieved using `findElements`.
allSelected = allElementsSelected([elemId, elemId...])# Tells whether all the given elements are selected. Useful for testing group of elements that# all need to be selected.

Get element attributes#

Gets any attribute of an element such as title, role, aria-label, alt.

attributeValue = getElementAttribute(elemId, attributeName)# Gets value of the attributeName for the given element.

Get element text#

Gets the visible text of an element.

text = getElementText(elemId) # get element's visible text

To get the visible text of elements that stores it in value attribute, use function getElementValue(elemId). Some of such elements are input[type='text'], hidden, input[type='button'].

Get element coordinates and dimensions#

  • Get element's x, y coordinates relative to viewport and dimensions:

    getElementRect(elemId)# returns a value similar to {x: 800, y: 2000, width: 600, height: 200}
  • Scroll element to viewport (if it's not in viewport) and get it's coordinates only:

    getElementViewportCoordinates(elemId)# returns a value similar to {x: 400, y: 600}

Checking whether element is stale#

Sometimes when you're not sure whether an element is stale, use isStale to make sure so that the test doesn't throw an error.

if (!isStale(elemId)) {  // do stuff}

Checking element exists#

To check whether an element is no more on the page, we can use noWait parameter of findElements and check that the returned list is empty. A shortcut to that is elementExists. This function doesn't wait if no element is found and returns a boolean indicating the result.

isExists = elementExists(using, by)
# Or
isExists = elementExists(fromElement, using, by)

Capturing element screenshot#

You can capture screenshot of a specific element (for example to see how it looked on page). Once the build completes, you can see the screenshot from build details.

captureElementScreenshot(elemId, fileName)# An example fileName could be 'small-select.png'

See Api Reference for all available functions.