getElementAttribute
To get an element's desired attribute's value, this function first look into the properties of HTMLElement
object which is the parent of every element. If a property with the given name is not found, element's attributes in html markup are searched.
Once an html page is rendered, browser automatically computes the properties of every element and create an instance of HTMLElement
. Whenever the state of element changes, it reflects in this object but not necessarily reflects in the html markup. For instance, if you create a checkbox
using <input id="consent" type="checkbox" name="consent" checked>
and uncheck it after it renders, the html markup doesn't update leaving the checked
attribute as-is even though the checkbox is not checked. The state of checkbox is rather updated in it's corresponding HTMLInputElement
(which implements HTMLElement
) object. You can view the current state using javascript like so document.getElementById('consent').checked
. See this StackOverflow thread to learn more about this concept if you need to.
getElementAttribute(elemId, attribute)
#
ParameterselemId
: The target elementattribute
: The name of attribute
#
Returns- value of the attribute or nothing if doesn't exists
Examples
ariaLabel = getElementAttribute(findElement('fileDetail', by.testId), 'aria-label')# returns the aria-label attribute value of element