DEV Community

TestDino
TestDino

Posted on

Playwright Mistake 10/14: Flaky Tests Are Not Random

You re-run. It passes. You move on.

Until your whole team is trained to ignore failures. That's when real bugs slip through.

Here are the 3 root causes behind 90% of flaky Playwright tests.

Race Condition

// bad
const text = await page.locator('.welcome').textContent();

// good
await expect(page.getByText('Welcome back')).toBeVisible();
// auto-retries until element exists
Enter fullscreen mode Exit fullscreen mode

Shared Data

// bad
await request.delete('/api/users/test-user-1');
// 5 workers fight over this

// good
const user = await createUniqueUser(request);
await request.delete(`/api/users/${user.id}`);
// each test gets its own
Enter fullscreen mode Exit fullscreen mode

Network Timing

// bad
await page.goto('/search?q=laptop');
await page.locator('.result').first().click();
// results not loaded yet

// good
await page.goto('/search?q=laptop');
await page.waitForResponse('**/api/search');
await page.getByTestId('result').first().click();

Enter fullscreen mode Exit fullscreen mode

Which of these three has burned you the most on your current project?

Top comments (0)