Skip to the editor

Replace the positional selector

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.

The problem

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.

Done when

  • Adding a product still makes the badge read 1.
  • No position-based selectors.

Bugs we plant

  • Cart badge always shows 0

We break each one in a copy of the app. Your cleaned-up test has to fail on every copy.

Loading the editor…