Skip to content

UI.getAttributes()

Gets all HTML attributes from an element and returns them as a key–value map.

Usage

js
var attr = UI.getAttributes(element: string)

Parameters

ParameterTypeDescription
elementstringTarget element — by CSS or XPath selector

Returns

A map where:

  • each key is the attribute name
  • each value is the attribute value as a string

Example

Take the following checkbox as an example:


Demo
UI
HTML
<!--[--><label>
    <input id="notify-stock-checkbox" type="checkbox" class="pretty-checkbox" data-product-id="PRD_3820" checked=""> Notify me when it is in stock
</label>
<!--]-->

We can retrieve all attributes of the checkbox using:

js
var attr = UI.getAttributes("#notify-stock-checkbox")

This returns a map with the following contents:

javascript
{
    "id": "notify-stock-checkbox",
    "type": "checkbox",
    "class": "pretty-checkbox",
    "data-product-id": "PRD_3820",
    "checked": ""
}

We can then read individual attribute values from the map and perform custom logging or assertions. Here is a complete example:

javascript
var attr = UI.getAttributes("#notify-stock-checkbox") 
var productID = attr["data-product-id"]
if (!productID) {
    TEST.log.fail("Missing 'data-product-id' attribute")
} else {
    TEST.log.info("Product ID: ", productID)
}