DEV Community


Data-QA Attribute! A better way to select elements for UI test automation

Christine Zierold
Proven ISTQB agile certified testing expert, with 10+ years of automation engineering experience, designing, maintaining, and expanding automated tests.
・1 min read

I guess everyone who had to automate a web UI had the situation that it was a struggle to find a stable and study selector for some elements on a page. It is important to have stable selectors that the UI automated tests don’t fail if the structure of a page changes or the styling changes.

Why not using ID’s or CSS class names?

ID’s and also css class names have another purpose than for the usage in UI automated tests. Css classes are primarily used to style elements and they also can get uglified on the production page. ID’s have also another purpose and can also sometimes just change or they are not get implemented because they interfere with other functions on the page.

Solution: Data-qa Attributes

Data-*Attributes are new in HTML 5. These HTML attributes enable us to create custom attributes on every HTML element. So the solution is to create ‘data-QA’ attributes for HTML elements. These attributes can be only used for the UI automated test and don’t interfere with any functions or purposes on the page.

Examples for data-qa attributes

"Code Example"

Discussion (5)

timabell profile image
Tim Abell

It's worth referring to the original article on data-js attributes to understand why this separation of concerns is so valuable.

kostadinmarinov profile image
Kostadin Marinov

If the web app is ARIA compliant, what do you think to use aria-label attribute for selectors instead of data-qa? Of course, the condition is that the QA tests are checking also the labels on the page.

marcellothearcane profile image

Is there a way to remove data-qa links from the production version?

nmiddleweek profile image
Nick Middleweek

You could use a plugin like this...

alberto666 profile image
Alberto Vara

Hi Christine, nice post.

I developed a browser extension to highlight these data-elements in screen.
This is useful in my work for QA professionals and I want to share with people who may need it.

Firefox extension:
Chrome extension:
Github source code: