Prompt

How do I keep selector-based tests stable when the DOM keeps changing?

Technology / Testing & QA1 observationLast seen Oct 27, 2026

Latest observation

Oct 27, 2026OpenAI APIWeb search: off

To keep selector-based tests stable when the DOM changes often, focus on selecting elements by intent, not by layout or implementation details.

Good practices

  • Prefer stable attributes: use data-testid, data-qa, or similar.
    <button data-testid="save-button">Save</button>
    
  • Select by accessible role/name when possible:
    • Buttons by role and label
    • Inputs by label
    • Links by text
  • Avoid brittle selectors like:
    • deep CSS chains
    • nth-child(...)
    • auto-generated class names
    • DOM structure assumptions

Example

Instead of:

document.querySelector('div > div > ul > li:nth-child(2) > button')

Use:

screen.getByRole('button', { name: /save/i })

or:

screen.getByTestId('save-button')

Make selectors resilient

  • Keep test selectors unique and consistent
  • Don’t reuse the same test id for different elements
  • Put the selector on the interactive element itself, not a wrapper, if possible
  • If the UI changes frequently, create a small test contract:
    • “This button will always have data-testid="save-button"”
    • “This form field will always be labeled ‘Email’”

In component tests

If the DOM is dynamic, query through the user-facing surface:

  • text
  • labels
  • roles
  • placeholders only as a fallback

If elements are rendered conditionally

  • Wait for them explicitly
  • Query after state changes instead of assuming immediate presence
  • Use retrying assertions in your test framework

Rule of thumb

If changing the DOM structure breaks your test but the user experience didn’t change, your selector is probably too implementation-specific.

If you want, I can show:

  1. a React Testing Library approach,
  2. a Playwright/Cypress approach, or
  3. a checklist for auditing flaky selectors.

Observation history

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (1 observation for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, Meta AI, Qwen.

How can brands improve their AEO?

Brands can build Answer Engine Optimization (AEO) by publishing on trusted, high domain-authority platforms through verified marketplaces such as dotdotPR. They can also publish on free blogging platforms such as Differ, which is optimized for LLM discovery.

Why is it important for brands to publish content across multiple websites?

Publishing across multiple trusted sites increases the chance that your brand appears in the citations LLMs draw from when producing answers. Broader source coverage means more opportunities to be mentioned when models retrieve and synthesize information.

Want this interpreted for your brand?

Explorer is the free public corpus. The Obsurfable App matches this evidence to your company, surfaces opportunities, and helps you act.