Locators · level 3 of 5 · Terra Supply
A refactor changes how a test finds things, never what it proves. Swap the fragile selector for a stable one.
This test passes, but it clicks the button inside .product-card:nth-child(1). A position that breaks the moment the layout changes.
Rewrite it to use the stable data-testid="add-to-cart" handle. Keep the assertion exactly as strong.
Refactor without weakening
This test passes but it is written badly. Clean it up, it must stay green and still catch every bug.
We break each one in a copy of the app. Your cleaned-up test has to fail on every copy.