Ever written an automated test, only to have it fail the following day because the 'Submit' button changed its class name? Frustrating, I'm sure.
Why are UI tests so flaky and why are selectors so flaky? Testers use CSS classes or IDs. Every time they see them, the class or ID changes every time the developers build the code. It makes automation brittle and long to update, especially due to the deep nesting and dynamic DOM elements.
However, what if there was a more efficient way? What if you could write tests to be reliable even if the UI is updated frequently?
In that case, data-testid
is what we need. Though data-testid
is similar to CSS classes or regular IDs, it provides a unique, persisting approach to identifying elements for automation testing. It guarantees your tests will be reliable even in case your styles or layouts change.
Say for example, that using data-testid
attributes can help reduce any problems that arise due to changes in styling or structure, ultimately making the tests more resilient to the failure of these DOM elements.
In this article, we will go through why data-testid
is a must-have for every automation engineer and why it can help UI testing become more stable and more efficient.
{{cta-image}}
What is the data-testid attribute?
The data-testid
is a custom HTML attribute that is specifically going to help you select elements during automation testing. It serves as a unique ID, and since the point is to locate elements without fragile selectors like CSS classes or complex XPath queries, it makes it easier.
Purpose of data-testid attribute
- Stable Test Selectors → Unlike class names or text content (which may change frequently), data-testid provides a consistent way to find elements in tests.
- Framework-Agnostic → Works with any frontend framework (React, Angular, Vue, etc.) and testing tools.
- Does Not Affect UI or Behavior → Since it’s a data-* attribute, it doesn’t interfere with styling or JavaScript functionality.
- Enhances Readability in Tests → Indicates elements meant for testing.
How it Works
The data-testid
attribute is implemented as a tag in HTML to make testing easier. Testing tools can use this attribute to identify elements to interact with the page without any problems.
Example
<button data-testid="submit-button">Submit</button>
Common Data TestIDs and Their Purpose
To create stable and reliable tests, data-testid
attributes should be used. There are ten frequently used data-testid
values outlined below, along with their intended purposes:
Why is the data-testid Attribute Important for Automation Testing?
The quality evaluation of software relies heavily on both the reliability and validity of tests in present-day scenarios. The element selection process requires the data-testid attribute most essentially since it solves problems that emerge from automated testing.

1. Simplifies Element Selection
Instead of using CSS classes or XPath selectors, stable and simple data-testid gives you a way to identify elements rather than relying on fragile selectors. It simplifies and makes the end-to-end test scripts reliable.
2. Ensures Maintainability and Scalability
Replacing UI labels and organizational elements with data-testid makes tests more resilient to style and structure changes even when new elements are added.
3. Improves Test Stability
data-testid enables making tests more resilient to dynamic class names, deeply nested DOM elements, or auto-generated attributes. Tests are less fragile because the dynamic properties do not change.
4. Enhanced Collaboration Between Developers and QA
By assigning meaningful test IDs, developers make it easier for QA engineers to write and maintain automation scripts. This fosters better alignment between teams and improves overall testing efficiency.
5. Works Across Various Testing Types
The data-testid attribute is framework-agnostic and works seamlessly across different testing methodologies, including:
- Component Testing (React Testing Library)
- Integration Testing (Jest, Cypress)
- End-to-End (E2E) Testing (Cypress, Selenium)
How to Implement data-testid in Popular Automation Frameworks?
Web applications become easier to automate through data-testid attributes, which provide both stable and easily detectable selectors. Below is how you can use data-testid in popular automation frameworks:
1. Selenium
You can locate elements through By.cssSelector
by using Selenium.
Java Example
WebElement element = driver.findElement(By.cssSelector("[data-testid='submit-button']")); element.click();
Python Example
element = driver.find_element(By.CSS_SELECTOR, "[data-testid='submit-button']")
element.click()
2. Cypress
Cypress implements data-testid
support as part of its built-in features.
JavaScript Example
cy.get("[data-testid='submit-button']").click();
Custom Command for Reusability
Cypress.Commands.add("getByTestId", (testId) => {
return cy.get(`[data-testid='${testId}']`);
});
// Usage
cy.getByTestId("submit-button").click();
3. Playwright
Playwright has direct support for data-testid
.
JavaScript Example
await page.getByTestId('submit-button').click();
Python Example
await page.get_by_test_id("submit-button").click()
4. WebdriverIO
WebdriverIO gives users a way to select elements through data-testid
.
JavaScript Example
const button = await $('[data-testid="submit-button"]');
await button.click();
{{cta-image-second}}
Conclusion
The data-testid attribute is a useful automation testing feature, providing reliability, readability, and effectiveness when locating elements on a web application. QA engineers can build better and more maintainable test scripts that are less subject to failure through UI changes by utilizing data-testid. This benefits the test efforts overall.
If you have any test team that is serious about achieving scalable and efficient test automation, adopting data-testid is a must-do as it will enable your test runs to be smooth and maintainable in the long run.
This methodology made testing with automation more effective, so test script maintenance and debugging time were reduced and the final software quality and release time improved.