UI.hasAttribute

Assert the presense or the expected value of an attribute on an element.

Usage

// assert the presense of the attribute on the target element
UI.hasAttribute(target, attribute)
// assert the value of the attribute on the target element
UI.hasAttribute(target, attribute, value)
Parameter Type Remarks
target string Expression to target an element. This can be a text, CSS selector, or XPATH selector.
attribute string Name of the attribute.
value string (Optional) Value expected for the attribute. Case-insensitive. If not provided, this command will simply assert that the attribute exists on the element.

# Validate that an element should be disabled

I.goTo("https://mystore.com")
I.click("Login") // <- go to login page

// leave the username and password fields blank
I.fill("Username", "")
I.fill("Password", "")

// assert that the #login-button is disabled
UI.hasAttribute("#login-button", "disabled") 

In this example, we can use the UI.hasAttribute to test if the #login-button element is disabled, when the Username and Password fields are empty.

# Validate that the element attribute should have a specific value

I.goTo("https://mystore.com")
UI.hasAttribute("#summer-sale-banner", "campaign-id", "SUMMERSALE2020")

This test will pass if the #summer-sale-banner element exists on the page, and has the campaign-id attribute, with the value of SUMMERSALE2020.