Menu
Choose a product
Viewing: v1.7.x (latest)
Find another version
Scroll for more
Open source
toHaveAttribute()
The toHaveAttribute() method asserts that an element has a specific attribute and optionally a specific value. This is a retrying assertion that automatically waits for the element to have the expected attribute.
Syntax
JavaScript
await expect(locator).toHaveAttribute(attribute);
await expect(locator).toHaveAttribute(attribute, value);
await expect(locator).toHaveAttribute(attribute, value, options);
await expect(locator).not.toHaveAttribute(attribute);Parameters
| Parameter | Type | Description |
|---|---|---|
| attribute | string | The attribute name to check for |
| value | string | Optional. The expected attribute value |
| options | RetryConfig | Optional configuration options |
Returns
| Type | Description |
|---|---|
| Promise | A promise that resolves when the assertion passes |
Description
The toHaveAttribute() method checks if an element has a specific attribute. When called with just an attribute name, it verifies the attribute exists regardless of its value. When called with both attribute name and value, it verifies the attribute exists and has the exact specified value.
This is a retrying assertion that will automatically re-check the element’s attributes until the condition is met or the timeout is reached.
Usage
JavaScript
import { browser } from 'k6/browser';
import { expect } from 'https://jslib.k6.io/k6-testing/0.6.1/index.js';
export const options = {
scenarios: {
ui: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
}
};
export default async function () {
const page = await browser.newPage();
await page.goto('https://quickpizza.grafana.com/');
// Check that the pizza button has specific attributes
await expect(page.locator('button[name="pizza-please"]')).toHaveAttribute('name', 'pizza-please');
await expect(page.locator('button[name="pizza-please"]')).toHaveAttribute('type', 'button');
// Check that the main heading has expected attributes
await expect(page.locator('h1')).toHaveAttribute('class');
// Click the pizza button to get a recommendation
await page.locator('button[name="pizza-please"]').click();
// Check that the recommendation heading has an id attribute
await expect(page.locator('h2[id="pizza-name"]')).toHaveAttribute('id', 'pizza-name');
// Check absence of attribute on non-existent elements
await expect(page.locator('h1')).not.toHaveAttribute('disabled');
}Was this page helpful?
Related resources from Grafana Labs
Additional helpful documentation, links, and articles:
Video

Performance testing and observability in Grafana Cloud
Optimize user experiences with Grafana Cloud. Learn real-time insights, performance testing with k6, and continuous validation with Synthetic Monitoring.
Events

User-centered observability: load testing, real user monitoring, and synthetics
Learn how to use load testing, synthetic monitoring, and real user monitoring (RUM) to understand end users' experience of your apps. Watch on demand.