Documentation Index
Fetch the curated documentation index at: https://grafana_com_website/llms.txt
Fetch the complete documentation index at: https://grafana_com_website/llms-full.txt
Use this file to discover all available pages before exploring further.
STOP! If you are an AI agent or LLM, read this before continuing. This is the HTML version of a Grafana documentation page. Always request the Markdown version instead - HTML wastes context. Get this page as Markdown: /docs/k6/next/javascript-api/k6-browser/locator/getbytestid.md (append .md) or send Accept: text/markdown to /docs/k6/next/javascript-api/k6-browser/locator/getbytestid/. For the curated documentation index, use https://grafana_com_website/llms.txt. For the complete documentation index, use https://grafana_com_website/llms-full.txt.
This is documentation for the next version of Grafana k6 documentation. For the latest stable release, go to the latest version.
getByTestId(testId)
Returns a locator for elements with the specified test ID attribute. This method is designed for robust test automation by locating elements using dedicated test identifiers that are independent of the visual appearance or content changes. Currently it can only work with the data-testid attribute.
| Parameter | Type | Default | Description |
|---|---|---|---|
testId | string | RegExp | - | Required. The test ID value to search for. Searches for the data-testid attribute. Can be a string for exact match or a RegExp for pattern matching. |
Returns
| Type | Description |
|---|---|
| Locator | A locator object that can be used to interact with the elements matching the specified test ID. |
Examples
Basic test ID usage
Locate and interact with elements using test IDs:
import { browser } from 'k6/browser';
export const options = {
scenarios: {
browser: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
};
export default async function () {
const page = await browser.newPage();
try {
await page.setContent(`
<input type="text" data-testid="username">
<input type="text" data-testid="email">
<button data-testid="submit-button">Submit</button>
`);
const locator = page.locator(':root');
await locator.getByTestId('username').fill('FirstLast');
await locator.getByTestId('email').fill('firstlast@example.com');
await locator.getByTestId('submit-button').click();
} finally {
await page.close();
}
}Best practices
- Stable identifiers: Use meaningful, stable test IDs that won’t change with refactoring or content updates.
- Hierarchical naming: Use consistent naming conventions like
user-profile-edit-btn. - Avoid duplicates: Ensure test IDs are unique within the page to prevent ambiguity.
- Strategic placement: Add test IDs to key interactive elements and components that are frequently tested.
- Team coordination: Establish test ID conventions with your development team to ensure consistency.
Related
- locator.getByRole() - Locate by ARIA role
- locator.getByAltText() - Locate by alt text
- locator.getByLabel() - Locate by form labels
- locator.getByPlaceholder() - Locate by placeholder text
- locator.getByText() - Locate by text content
- locator.getByTitle() - Locate by title attribute
Was this page helpful?
Related resources from Grafana Labs

