qa / test automation
Test Automation: Choose Playwright, Cypress or Selenium, Then Fix Flaky Tests
A decision table for the three main browser-automation tools, a flaky test that was reproduced and fixed on this page's own machine, and the page object pattern in a short working example. Tool facts come from each project's documentation and license file.
Test automation is programming that checks your software for you. The code part is easy to get started with and hard to get right, because automated browser tests fail for reasons that have nothing to do with bugs: a button that appears 300 milliseconds late, a shared test user that another test just changed, a staging service that was down. A suite that fails randomly teaches a team to ignore red builds, which is worse than having no suite. So this guide spends its time on the two decisions that matter most: which tool to commit to, and how to write tests that fail only when the product is broken. You need some programming to follow it; if you are not there yet, work through the frameworks by language page first.
01 · PICKING A TOOL
Playwright, Cypress and Selenium compared on checkable facts
| Playwright | Cypress | Selenium | |
|---|---|---|---|
| Languages | JavaScript and TypeScript, Python, Java, .NET | JavaScript and TypeScript, installed through npm | Java, Python, C#, Ruby, JavaScript, Kotlin |
| Browsers | Chromium, WebKit and Firefox on Windows, Linux and macOS | Chrome-family browsers including Edge, and Firefox, plus experimental WebKit | Many browsers through WebDriver, the interface at Selenium's core |
| Waiting | Built-in auto-waiting, and assertions that retry for 5 seconds by default | Commands and assertions retry automatically, per its retry-ability guide | You choose explicit or implicit waits; its docs warn against mixing them |
| License | Apache 2.0 | MIT | Apache 2.0 |
| Runtime needed | Node.js 22, 24 or 26 for the JavaScript runner | Node.js 22, 24 or 26 | The runtime of whichever language binding you use |
The honest decision rule is about your situation, not about which tool is best. If your team writes Java and already has Selenium suites, keep them and improve the waits. If you are starting fresh in JavaScript or TypeScript and want cross-browser coverage, Playwright's own runner includes parallelism, retries and a trace viewer. If your team lives in the browser and wants a tight interactive runner, look at Cypress. All three are free and open source, so you lose nothing by trying two on the same small app. For job searches, learn one tool deeply and the others at the level of reading a test.
02 · THE FLAKE EXPERIMENT
Why a sleep is a guess, shown by a test that fails
Here is a page whose result appears 800 milliseconds after load, like a slow API response, and two tests against it. Both were run with Playwright.
import { test, expect } from '@playwright/test';
const html = `<div id="out"></div>
<script>setTimeout(() => {
document.getElementById('out').innerHTML = '<b id="late">done</b>';
}, 800);</script>`;
test('guessing a sleep', async ({ page }) => {
await page.setContent(html);
await page.waitForTimeout(500); // a guess
expect(await page.locator('#late').count()).toBe(1);
});
test('web-first assertion', async ({ page }) => {
await page.setContent(html);
await expect(page.locator('#late')).toHaveCount(1);
});The first test failed with Expected: 1, Received: 0, because the 500 ms guess was shorter than the app's 800 ms. The second passed, because expect(locator).toHaveCount() retries until the condition holds or the 5-second assertion timeout runs out. Raising the sleep to 1,000 ms would make the first test pass today and fail on a slow CI runner next month. That is the shape of nearly every flaky test: a hard-coded wait that only works under good conditions.
The rule to take away is to wait for a condition, never for time. In Playwright that means web-first assertions such as toBeVisible and toHaveText. In Cypress, assertions chained to a command retry. In Selenium, use explicit waits on a condition and avoid mixing them with implicit waits.
03 · FLAKE CAUSES
Where flaky tests come from and what to do about each
Google's testing blog reported that about 1.5 percent of all its test runs showed a flaky result, meaning the same code both passed and failed, and that about 84 percent of the pass-to-fail transitions it observed in post-submit testing involved a flaky test. The expensive part is the time people spend investigating failures that were never bugs. Martin Fowler's essay on non-determinism groups the usual culprits.
| Cause | What you see | Fix |
|---|---|---|
| Fixed sleeps | Passes locally, fails on slow CI | Wait on a condition with a retrying assertion |
| Shared test data | Fails only when another test runs first or at the same time | Each test creates its own user and records |
| Test order dependence | Fails when run alone or reordered | Make each test independent; randomize order to expose the dependency |
| External services | Fails when a third party is slow or down | Stub the call at the network layer for most tests; keep a few real integration checks |
| Brittle selectors | Fails after a harmless layout change | Select by role, label or test id rather than long CSS paths |
Retries are a tool, not a cure. Playwright supports retrying failed tests, and a test that passes on a retry is reported as flaky rather than as a plain pass, so you can see it. Google's post says it handles known-flaky tests by reporting a failure only if the test fails three times in a row. Keep a list of those tests and fix them in order; do not let the retry count quietly hide the problem. When a test does fail, Playwright's trace viewer replays the run step by step, which usually shows the cause faster than reading logs.
04 · STRUCTURE
Page objects, kept small
A page object puts the selectors and actions for one page in one class, so a UI change is fixed in one place instead of in forty tests. Playwright's documentation describes the pattern directly. This version was run against Playwright's public TodoMVC demo, with a happy path, a negative case and an edge case:
class TodoPage {
readonly input: Locator;
readonly items: Locator;
constructor(private page: Page) {
this.input = page.getByPlaceholder('What needs to be done?');
this.items = page.getByTestId('todo-title');
}
async goto() { await this.page.goto('https://demo.playwright.dev/todomvc'); }
async add(text: string) {
await this.input.fill(text);
await this.input.press('Enter');
}
}
test('adds a todo', async ({ page }) => {
const todos = new TodoPage(page);
await todos.goto();
await todos.add('buy milk');
await expect(todos.items).toHaveText(['buy milk']);
});
// negative: pressing Enter on an empty field adds nothing -> toHaveCount(0)
// edge: a whitespace-only title is trimmed away -> toHaveCount(0)Keep assertions in the tests, not in the page object, and keep the page object free of waits that belong to the test. Do not build a framework before you have ten tests. Two page objects and a shared login helper cover most small suites.
05 · KEEP READING
Free documentation, courses and essays
- Playwright best practices ↗The project's own guidance on locators, isolation and avoiding flaky tests.playwright.dev
- Playwright retries ↗How retries work and how flaky tests are labeled in the report.playwright.dev
- Playwright trace viewer ↗Replay a failed run with its DOM snapshots and network calls.playwright.dev
- Page object models (Playwright) ↗The official walkthrough of the pattern shown above.playwright.dev
- Retry-ability in Cypress ↗How Cypress decides which commands and assertions retry.docs.cypress.io
- Selenium waiting strategies ↗Explicit, implicit and fluent waits, with the warning about mixing them.selenium.dev
- Test Automation University ↗Free video courses with quizzes and certificates, taught by working automation engineers.testautomationu.applitools.com
- The Practical Test Pyramid ↗Why most checks belong below the browser, and how to split them.martinfowler.com
- Eradicating Non-Determinism in Tests ↗Fowler's essay on the causes of flaky tests and how to quarantine them.martinfowler.com
- Flaky Tests at Google and How We Mitigate Them ↗The source of the flake-rate numbers quoted above.testing.googleblog.com
06 · PRACTICE
Reproduce a flake, then remove it
npm init playwright@latest and paste in the two tests from section two. Confirm the first fails. Change the page's delay to 3,000 ms and see that even the web-first test now needs a longer timeout, then read the Playwright timeouts page to find where to set it. Finish by writing three tests against any public demo site: one happy path, one bad input, one boundary value.07 · FAQ
Frequently asked questions
Do I need to know how to code for test automation?
Yes, at a basic level. Automated tests are programs, so you need variables, functions, conditions and loops, and you should be comfortable reading errors. You do not need to be a software engineer; a few weeks of practice in one language is enough to write useful tests.
Which test automation tool should I learn first?
If you are choosing fresh, Playwright is a strong first choice because it supports several languages, three browser engines and has built-in waiting and tracing. If you already work in Java, Selenium is the natural fit, and Cypress suits teams that work only in JavaScript or TypeScript.
What is the Page Object Model?
It is a pattern where each page or component of an application gets a class that holds its selectors and actions. Tests call methods like login or add instead of repeating selectors, so a change to the interface is fixed in one place.
Why are my automated tests flaky?
The most common causes are fixed sleeps instead of waiting for a condition, tests that share data or depend on order, calls to unreliable external services, and brittle selectors. Replace sleeps with retrying assertions, isolate test data, stub external calls and select elements by role or test id.
Should every test be an end-to-end browser test?
No. Browser tests are the slowest and most fragile layer. The test pyramid idea is to have many fast unit tests, fewer service-level tests and a small number of end-to-end tests that cover the key journeys.