Prompt
I need end-to-end testing for a React app with flaky selectors and CI parallelization
Latest observation
To make end-to-end testing reliable for a React app with flaky selectors and CI parallelization, I’d recommend a setup centered on Playwright (best fit for stable selectors, parallel runs, and CI).
Recommended approach
1) Use robust selectors
Avoid brittle CSS/XPath selectors tied to styling or DOM structure.
Prefer:
data-testid- accessible roles/text via Playwright selectors
- semantic labels
Examples:
<button data-testid="save-button">Save</button>
await page.getByTestId('save-button').click();
await page.getByRole('button', { name: 'Save' }).click();
Best practices:
- Add
data-testidonly for elements that are hard to target semantically. - Keep selectors stable across UI redesigns.
- Avoid selecting by class names or nth-child.
2) Use Playwright for auto-waiting and retries
Playwright helps reduce flakiness with:
- auto-waiting for elements to be visible/enabled
- built-in retries for actions/assertions
- trace/video/screenshot debugging
Example:
await expect(page.getByText('Welcome')).toBeVisible();
3) Structure tests for parallelization
Keep tests isolated:
- no shared state between tests
- create unique data per test
- clean up after each test when needed
Use beforeEach to start from a known state:
test.beforeEach(async ({ page }) => {
await page.goto('/login');
});
For data:
- generate unique email/usernames
- avoid reusing fixed accounts unless tests reset state
4) Parallelize in CI safely
Playwright supports parallel execution out of the box.
Example playwright.config.ts:
import { defineConfig, devices } from '@playwright/test';
export default defineConfig({
testDir: './tests/e2e',
fullyParallel: true,
retries: process.env.CI ? 2 : 0,
workers: process.env.CI ? 4 : undefined,
use: {
baseURL: 'http://localhost:3000',
trace: 'on-first-retry',
screenshot: 'only-on-failure',
video: 'retain-on-failure',
},
});
Good CI rules:
- run tests in parallel workers
- retry only in CI
- collect traces on failure
- shard tests across CI machines if the suite gets large
5) Use sharding for faster CI
If you have many tests, split them across CI jobs.
Example:
npx playwright test --shard=1/4
npx playwright test --shard=2/4
npx playwright test --shard=3/4
npx playwright test --shard=4/4
This is ideal when you have enough CI capacity to run jobs concurrently.
6) Make the app test-friendly
Add these to your React app:
data-testidon important controls- stable headings/labels
- clear loading/error states
- deterministic API mocking where useful
For highly dynamic flows, consider mocking network calls with Playwright:
await page.route('**/api/user', route =>
route.fulfill({
json: { name: 'Test User' }
})
);
7) Handle flakiness systematically
Common causes and fixes:
- animation timing → disable animations in tests
- race conditions → wait for specific UI states, not arbitrary timeouts
- network dependency → mock or seed test data
- shared test data → use isolated test accounts/db resets
Avoid:
await page.waitForTimeout(3000);
Prefer:
await expect(page.getByText('Loaded')).toBeVisible();
Suggested stack
- Playwright for E2E
- React Testing Library for component-level tests
- MSW for API mocking in dev/tests
- GitHub Actions / GitLab CI / CircleCI for parallel CI
Example CI setup
name: E2E
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
shard: [1, 2, 3, 4]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npx playwright install --with-deps
- run: npm run build
- run: npx playwright test --shard=${{ matrix.shard }}/4
Minimal recommendation
If you want the shortest path to stable E2E with CI parallelization:
- Add
data-testidto key elements. - Use Playwright.
- Run tests with
fullyParallel: true. - Shard tests in CI.
- Capture traces/screenshots on failure.
- Keep tests isolated and deterministic.
If you want, I can also give you:
- a sample Playwright setup for a React project
- a CI config for GitHub Actions
- or a selector strategy guide for fixing flaky tests.