Hacker News new | past | comments | ask | show | jobs | submit login

We add data-test-id, data-test-name, or data-test-value attributes (depending on the case) to our elements, and selectors are rock-solid.



Using data-test-name or data-test="name" for e2e automation is the right answer. Data attributes are not going to conflict with your classes or IDs, won't get mangled, and they are tolerant to DOM, style, and backend/form refactoring. Essentially ignored by everything but your test framework.


I disagree completely.

E2E tests should mimic how users interact with the page and they see no data attributes.

You can query by text, label, html semantics, position.

Hell even for clickable icons you should have alt text.


Minor copy changes shouldn't break your tests. If you're selecting elements by their content the tests are much more brittle.

Nor should unimportant stylistic changes in class, or changes in element order/positioning, break our functional tests. Thus we give important elements data-attributes which are resilient to all forms of refactoring.


Let's agree to disagree I guess.

HTML has specific semantics, and I find that interacting with applications the same way a blind person would (aria labels, text, etc) leads to much more solid tests.

Copy changes are far from common on stuff like labels and any actionable item you aren't changing "submit" to "send" every other week or "pay" to "checkout" (moreover, such a button would have a meaningful aria role like "search" or "register").

And if you do, fixing the tests is generally very cheap and quick, so I see it as a non-issue.

Nothing will change my idea that abusing data attributes (which are sometimes, but rarely, a necessary evil) leads to the great amount of non accessible and semantically incorrect bloated HTML we see everywhere on the net.

Good tests will lead to better websites, and data-attributes do nothing to help in that direction.


Humans are faaar too smart to even try mimicking their behavior. All you can hope for is to make sure that the intended usage works fine.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: