Skip to content

UI.hasAttribute()

Asserts that an element has an HTML attribute, and optionally checks that the attribute value matches the expected value.

Asserts that an element has a specific HTML attribute.

  • If value is not provided, the assertion passes when the attribute is present on the element.
  • If value is provided, the assertion passes when the attribute is present on the element and its value matches.

If the assertion fails, the test will fail.

Usage

js
UI.hasAttribute(element: string, name: string)
UI.hasAttribute(element: string, name: string, value: string)

Parameters

ParameterTypeDescription
elementstringTarget element — by CSS or XPath selector
namestringAttribute name to check (e.g. disabled, aria-label, href)
valuestring(Optional) Expected attribute value.

Examples

Assert attribute exists

js
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 the example above, the test asserts that the disabled attribute is set on the #login-button element.

Assert attribute value

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

In the example above, the test asserts that the #summer-sale-banner element has the campaign-id attribute and that its value is "SUMMERSALE2020".