Prompt
How do I set up end-to-end tests for a SPA with dynamic DOM updates?
Latest observation
For a SPA with dynamic DOM updates, the key is to use an E2E setup that waits for the UI to reach the right state instead of relying on fixed sleeps.
Recommended approach
1) Pick a browser automation tool
Common choices:
- Playwright — great default for modern SPAs
- Cypress — also popular and easy to start with
- Selenium/WebDriver — more traditional, but usually more verbose
If you’re starting fresh, I’d recommend Playwright.
2) Use a real app server in tests
Run the app the same way users see it:
- build the SPA
- start a local test server
- run browser tests against
http://localhost:...
For example:
- dev server for local debugging
- production-like build for CI
This helps catch routing, asset loading, and async rendering issues.
3) Make tests wait for UI states, not time
Dynamic DOM updates mean elements may appear later after:
- fetch requests
- state changes
- route transitions
- animations
- client-side rendering
Avoid:
sleep(2000)- arbitrary delays
Prefer:
- waiting for an element to appear
- waiting for text to change
- waiting for navigation to finish
- waiting for network idle only when appropriate
Example in Playwright
await page.getByRole('button', { name: 'Save' }).click();
await expect(page.getByText('Saved successfully')).toBeVisible();
Playwright automatically retries assertions until timeout.
4) Use stable selectors
Dynamic SPAs often re-render the DOM, so avoid brittle selectors like:
- long CSS chains
nth-child- class names generated by CSS modules or build tools
Prefer:
data-testid- accessible roles
- labels
- text content when stable
Example
<button data-testid="save-button">Save</button>
await page.getByTestId('save-button').click();
Even better when possible:
await page.getByRole('button', { name: 'Save' }).click();
5) Test user-visible behavior, not implementation details
Good E2E tests verify things like:
- page loads
- user can log in
- form submits
- error message appears
- list updates after create/delete
- route changes work
Avoid testing internal store state or component methods in E2E tests.
6) Handle API dependencies carefully
If your SPA depends on backend APIs, you have two main options:
Option A: Use a dedicated test backend
- Good for full integration
- More realistic
- Slower and more fragile if shared data isn’t isolated
Option B: Mock network requests
- Faster and deterministic
- Good for frontend-focused E2E
- Less realistic than a real backend
With Playwright, you can intercept requests:
await page.route('**/api/todos', async route => {
await route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify([{ id: 1, title: 'Test todo' }]),
});
});
Use mocks for predictable tests, but keep a smaller set of tests against the real backend too.
7) Wait for SPA-specific async behavior
For dynamic DOM updates, watch out for:
- loading spinners
- React/Vue hydration
- route transitions
- debounced search
- infinite scroll
- lazy-loaded components
Examples:
Wait for content
await expect(page.getByText('Dashboard')).toBeVisible();
Wait for loading to disappear
await expect(page.getByTestId('loading-spinner')).toBeHidden();
Wait for URL changes
await expect(page).toHaveURL(/\/dashboard/);
8) Keep tests isolated
Each test should start from a clean state:
- reset database or use seeded data
- clear localStorage/sessionStorage if relevant
- avoid dependence on previous tests
In Playwright, use test fixtures or beforeEach to prepare state.
9) Add CI execution
Run E2E tests in CI on every important change:
- headless browsers
- consistent environment
- retries for flaky tests
- artifacts on failure: screenshots, videos, traces
Playwright supports these well out of the box.
10) Debug flaky dynamic DOM issues
If tests are flaky:
- use explicit expectations on visible UI
- inspect traces/screenshots
- check if the app rerenders elements and invalidates locators
- ensure the app has proper loading/error states
- confirm the test doesn’t click before the element is actually ready
A solid Playwright starter setup
Install
npm init playwright@latest
Example test
import { test, expect } from '@playwright/test';
test('user can add a todo', async ({ page }) => {
await page.goto('http://localhost:3000');
await page.getByLabel('New Todo').fill('Buy milk');
await page.getByRole('button', { name: 'Add' }).click();
await expect(page.getByText('Buy milk')).toBeVisible();
});
Practical rules of thumb
- Use Playwright
- Prefer role/text/testid selectors
- Assert on visible UI states
- Wait for elements, text, and URLs, not arbitrary timeouts
- Keep tests independent
- Run against a real build in CI
- Mock APIs when you want speed and stability
If you want, I can also give you:
- a Playwright project structure for a SPA, or
- an example for React/Vue/Angular specifically.