Comparison
Brittle selectorvsTest id
Brittle selector
the test broke because someone added a wrapping div, not because anything the user sees changed.
A query tied to implementation detail — a generated class name, a nth-child chain, a deep CSS path — which fails on refactors that change nothing observable. Every such failure trains the team to distrust the suite, which is more expensive than the bug it might have caught. Querying by role, label and text is the durable alternative because those are the things that are meant to stay stable.
Full entry →Test id
you added an attribute to the element purely so the test could find it.
A dedicated attribute used as a stable selector, immune to changes in text, structure and styling. It is the escape hatch for elements with no accessible name and no distinguishing text. Reaching for it first is a smell: it makes the test pass while telling you nothing about whether a user could have found the element either.
Full entry →