Eliminating Flaky Tests in CI: Deterministic Wait Patterns and DOM State Guards
Why waitForTimeout is an anti-pattern. How web-first assertions, auto-waiting locators, and isolated test workers cut CI false negatives to zero.
Akusara Studio Labs
QA & Test Engineering
Why waitForTimeout is an anti-pattern. How web-first assertions, auto-waiting locators, and isolated test workers cut CI false negatives to zero.
Why Flaky Tests Erode Trust
When continuous integration pipelines fail intermittently on unrelated pull requests, developers stop trusting test results. They hit "Re-run failed jobs" until it turns green by chance, and critical regressions slip into production.
The root cause of flakiness is almost always non-deterministic synchronization between asynchronous JavaScript rendering and test assertions.
1. Ban page.waitForTimeout()
Hardcoded sleeps like page.waitForTimeout(2000) fail because CI runner CPU allocation varies. On a busy shared CI runner, 2000ms is not enough; on a local laptop, it wastes time.
Instead, rely on Playwright's web-first assertions, which automatically retry until the condition is met or the timeout expires:
// Anti-pattern: arbitrary sleep
await page.click('#submit-btn');
await page.waitForTimeout(3000);
expect(await page.isVisible('.success-badge')).toBeTruthy();
// Deterministic pattern: auto-retrying assertion
await page.getByRole('button', { name: 'Save Changes' }).click();
await expect(page.getByText('Settings updated successfully')).toBeVisible();2. Waiting for Network Idleness Is Risky
Using page.waitForLoadState('networkidle') frequently times out on applications that maintain active WebSocket heartbeats, telemetry beacons, or polling loops.
Instead, wait for the specific response payload your test depends on:
// Wait deterministically for the specific API endpoint
const [response] = await Promise.all([
page.waitForResponse((res) => res.url().includes('/api/projects') && res.status() === 200),
page.getByRole('button', { name: 'Create Project' }).click(),
]);
const data = await response.json();
expect(data.id).toBeDefined();3. Database Isolation Per Test Run
Flakiness also occurs when tests read and write to shared database rows simultaneously. In our CI pipelines, each parallel test worker connects to an isolated tenant schema or runs inside a dedicated lightweight test database spun up via Docker compose.
When tests run against independent data states, CI failure rates drop to genuine regression alerts.
Published by Akusara Studio Labs
QA & Test Engineering
Akusara Digital is an engineering and product studio building mission-critical software, custom SaaS platforms, and developer tooling.
Read Next from Akusara
All Articles
Synthesizing Automated Test Suites: How SnapTest Generates Playwright Specs from DOM Snapshots
How our in-house test generator parses live DOM trees, generates resilient accessible role selectors, and outputs ready-to-run Playwright tests without fragile XPath locators.

Eliminating 'Cannot Reproduce': Why Visual & Network Diagnostics Matter in Bug Reports
The engineering story behind Bugsnap. How capturing DOM mutations, network payloads, and console logs at the point of failure cuts triage time by 70%.
