Documentation for automated readers
A curated documentation index is available at: https://grafana.com/llms.txt
A complete documentation index is available at: https://grafana.com/llms-full.txt
These indexes can help with page discovery before fetching individual documents.
This page is also available in Markdown, which may be easier for automated readers and AI tools to parse than HTML. The Markdown version is available at https://grafana.com/docs/k6/next/javascript-api/jslib/testing/retrying-assertions/tohaveattribute.md, or by sending Accept: text/markdown to https://grafana.com/docs/k6/next/javascript-api/jslib/testing/retrying-assertions/tohaveattribute/. For broader documentation discovery, the curated index is available at https://grafana.com/llms.txt and the complete index is available at https://grafana.com/llms-full.txt.
This is documentation for the next version of Grafana k6 documentation. For the latest stable release, go to the latest version.
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
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
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

