A page can be visible, clickable, and still not be ready for a test. CSS animations can move a target out from under a click, transitions can delay the final layout, and skeleton screens can make a dashboard look loaded while the data behind it is still missing. If a suite keeps failing around these moments, the problem is usually not the selector, it is the readiness signal.

The shortest path to stability is to stop treating “the DOM exists” as the same thing as “the UI is settled.” In browser automation, those are different states.

The failure pattern you are actually chasing

A flaky test around motion or loading states usually comes from one of four situations:

  1. The element exists before it is interactable. A button is in the DOM, but an overlay is still fading out.
  2. The layout is still changing. A transition shifts the target after the test resolves the locator.
  3. The app shows placeholders instead of content. Skeleton cards match the final structure closely enough to fool simple assertions.
  4. The test waits for the wrong signal. Network idle, fixed sleep, or page load completes before the UI is functionally ready.

A test that waits for the browser to become idle is not necessarily waiting for the product to become ready.

That distinction matters because browser automation tools observe different things. They can wait for navigation, DOM conditions, element states, and network activity, but none of those automatically mean the user can complete the task.

Start with the simplest reliable fix, disable motion in test runs

If your suite is testing behavior, not animation timing, disabling motion is the cleanest way to reduce timing flakes. The usual approach is to load a test-only stylesheet or inject a rule that removes transitions and animations.

A minimal CSS reset looks like this:

* , *::before, *::after {
  animation-duration: 0.001ms !important;
  animation-delay: 0s !important;
  animation-iteration-count: 1 !important;
  transition-duration: 0.001ms !important;
  transition-delay: 0s !important;
  scroll-behavior: auto !important;
}

This is intentionally blunt. It is useful because it turns a moving target into a static one. It is also risky if your product logic depends on animation end events, which is uncommon but possible. If you need a less aggressive approach, scope the rule to the app shell or a test flag.

A more maintainable version is to key off a test class on <html> or <body>:

html[data-test-mode="true"] *,
html[data-test-mode="true"] *::before,
html[data-test-mode="true"] *::after {
  animation: none !important;
  transition: none !important;
  scroll-behavior: auto !important;
}

In Playwright, you can set that flag before the app renders:

import { test } from '@playwright/test';

test.beforeEach(async ({ page }) => {

await page.addInitScript(() => {
    document.documentElement.setAttribute('data-test-mode', 'true');
  });
});

This pattern is usually better than trying to guess durations in the test. It keeps the change local, visible, and reversible.

For product code that should respect user motion preferences, the prefers-reduced-motion media query is the standards-aligned route. If your UI already uses it, test runs can emulate reduced motion instead of adding a separate test-only switch. That is cleaner when the app is designed to honor the preference, but it is not a substitute for a proper test hook if the app ignores the media query.

Make assertions transition-aware, not just existence-aware

A locator can resolve too early. What you want is an assertion about the final state you care about, not the first state the browser can see.

Examples:

  • Wait for an overlay to disappear, not for the button to exist.
  • Wait for a text value to change, not for a card shell to mount.
  • Wait for a control to be enabled, not merely visible.
  • Wait for a panel to stop moving, not just for it to appear.

In Playwright, built-in assertions help because they retry until the condition is true or times out:

await expect(page.getByRole('button', { name: 'Save' })).toBeEnabled();
await expect(page.getByTestId('loading-spinner')).toBeHidden();
await expect(page.getByTestId('profile-name')).toHaveText('Ada Lovelace');

If the button is hidden behind a fading overlay, asserting toBeVisible() is not enough. toBeEnabled() or toBeHidden() on the blocker gives a more precise readiness signal.

For Cypress, the same principle applies, even though the syntax differs:

javascript cy.get(‘[data-testid=”loading-spinner”]’).should(‘not.exist’); cy.contains(‘button’, ‘Save’).should(‘be.enabled’); cy.get(‘[data-testid=”profile-name”]’).should(‘have.text’, ‘Ada Lovelace’);

The important part is not the framework. It is the assertion choice.

Avoid asserting on animation frames

Do not wait for CSS timing to finish by sleeping for the expected transition duration. That couples your test to implementation detail and makes future refactors expensive. If the animation changes from 200 ms to 300 ms, your test should not care unless the product behavior changes.

If you truly need to wait for a transition-driven state, wait for the state transition, not the time. For example, assert that an element has a class removed, aria-busy="false", or a stable final text value.

Skeleton screens need explicit test coverage

Skeleton screens are not a visual annoyance only. They are a state. If your app uses placeholders while data loads, tests need to verify both the placeholder and the final content path.

There are two separate questions:

  1. Does the loading state appear when data is pending?
  2. Does the final content replace the placeholder correctly?

A good test plan covers both.

Test the skeleton as a product state

If a skeleton screen is part of the user experience, test that it appears only while loading and disappears when content arrives. Do not rely on the absence of runtime errors alone.

A simple example:

await expect(page.getByTestId('profile-skeleton')).toBeVisible();
await expect(page.getByTestId('profile-skeleton')).toBeHidden();
await expect(page.getByRole('heading', { name: 'Profile' })).toBeVisible();

This catches cases where the skeleton never disappears or the content renders underneath it.

Fake the data boundary, not the animation

Skeleton tests are more reliable when the network state is controlled. Use a mock response, fixture, or test API so you can hold the UI in its loading state long enough to inspect it, then release it.

That gives you deterministic coverage of the placeholder path without depending on a real backend response time. It also helps reveal whether your loading state is tied to a specific request, a request count, or a component lifecycle event.

Network idle is useful, but it is not a readiness guarantee

Many teams reach for networkidle because it sounds like “the app is done.” It is often the wrong choice.

Why it fails:

  • The app may preload additional data after the first screen is usable.
  • WebSockets, polling, analytics, or long-lived connections can keep the network busy.
  • A page can become network-idle while a final render or animation is still pending.

Use network-idle only when the application’s own behavior makes it meaningful. For most UI tests, prefer an application-level readiness signal.

Better signals include:

  • a top-level data-app-ready="true" attribute
  • a visible heading or landmark that only appears in the final state
  • a spinner or skeleton disappearing
  • a form control becoming enabled
  • an API-backed table reaching the expected row count

If you control the app, expose a deliberate readiness hook for test environments. That can be a DOM attribute, a global flag, or a visible marker that flips only when the user-facing state is truly ready.

Choose a readiness signal, then standardize it

The main maintenance problem is not one flaky test. It is ten different ways of deciding the page is ready.

A team should pick one preferred readiness model per app area:

  • Navigation pages: wait for a stable heading and key controls.
  • Forms: wait for inputs to be enabled and validation to settle.
  • Dashboards: wait for a title plus the main data widget, not the entire network.
  • Modal flows: wait for the modal to open and the overlay behind it to stop intercepting clicks.

Once the team agrees on the signal, encode it in helpers so the same logic is reused across tests.

async function waitForDashboardReady(page: Page) {
  await expect(page.getByTestId('dashboard-skeleton')).toBeHidden();
  await expect(page.getByRole('heading', { name: 'Dashboard' })).toBeVisible();
  await expect(page.getByTestId('main-metric-card')).toBeVisible();
}

This is better than repeating ad hoc waits in every test because it centralizes the failure semantics. When the app changes, you update one helper instead of 40 test files.

A compact decision table for common fixes

Problem Better fix When it works best Risk
CSS animation causes click misfires Disable motion in test mode Behavior tests, not animation tests May hide animation-specific regressions
Transition delays final state Assert on final state, not time Element visibility, enabled state, text changes Requires a clear product state
Skeleton screen appears too early Mock the loading boundary Data-driven screens Needs deterministic fixture or intercept
networkidle returns too soon or never Replace with app-level readiness signal SPAs, polling apps, long-lived connections Requires app instrumentation
Arbitrary sleep is used Replace with a retrying assertion Almost every browser suite Needs good selectors and state markers

What to avoid

Hard-coded sleeps

A sleep is a guess. It can pass on one machine and fail on another, and it grows stale as soon as a transition duration changes.

Timing assertions on CSS durations

Asserting that a transition lasted 200 ms is usually testing implementation detail, not user value. It adds brittleness without improving confidence.

Overusing force: true

Forcing clicks can paper over a real overlay problem. If a click needs force to succeed, ask why the UI was not ready. The test may be warning you about a genuine user-blocking bug.

Blindly waiting for page load

A load event tells you the document loaded, not that the app state is complete. That is especially weak in client-rendered applications.

When to keep motion in the suite

Disabling animations is not always the right answer. If the animation itself is part of the product contract, such as a collapsible panel, drag interaction, or a motion-driven onboarding step, then you need at least one test that checks the motion-linked behavior.

The trick is to separate concerns:

  • Most tests: disable motion, test behavior.
  • A few targeted tests: keep motion on, test the animation-related contract.

That balance reduces timing flakes without making motion invisible to the suite.

A practical implementation order

If you are trying to stabilize a noisy suite, I would sequence the work like this:

  1. Remove hard-coded sleeps.
  2. Add a test-mode switch that disables CSS motion.
  3. Replace generic waits with state-based assertions.
  4. Add explicit coverage for skeleton screens.
  5. Replace networkidle with app-specific readiness signals where it is misleading.
  6. Put common waits into helpers so the pattern is repeatable.

That order gives the fastest return because it attacks the broadest sources of flakiness first.

FAQ

Should I disable all animations in every test run?

Usually yes for functional browser tests, unless the animation is itself part of the behavior under test. Keep one or a few motion-aware tests if animation is user-relevant.

Is networkidle ever safe to use?

It can be useful for a narrow set of pages, but it is not a universal readiness signal. Use it only when you understand the page’s background requests and long-lived connections.

How do I test skeleton screens without waiting forever?

Mock the data boundary so the page stays in its loading state long enough to assert on the skeleton, then release the response and assert on the final content.

What is the best replacement for sleep?

A retrying assertion against a real UI state, such as hidden skeletons, enabled controls, final text, or a stable app-ready marker.

Should readiness be encoded in the app or only in tests?

Prefer an app-level readiness marker when the product can expose one cleanly. Tests should consume that signal rather than infer readiness from timing.

The goal is not to make browser automation ignore motion. It is to make tests wait for the same thing a user cares about, a page that is actually ready to use.