Appearance
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
valueis not provided, the assertion passes when the attribute is present on the element. - If
valueis 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
| Parameter | Type | Description |
|---|---|---|
element | string | Target element — by CSS or XPath selector |
name | string | Attribute name to check (e.g. disabled, aria-label, href) |
value | string | (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".