The usual suspects: why tests fail
Why This Matters
A red test is a question: is the product broken, is the test broken, or is the environment having a bad day? Answer it wrongly and you either file a bug that is not there or “fix” a test that was right. Most failures fall into a handful of recognisable groups. Learn to recognise them from the error message alone, and half the debugging is done before you open a trace.
The exercise
The exercise/debugging branch has five tests that each fail for a different reason:
git checkout exercise/debugging
npx playwright test tests/module-3 --project=chromium
This is the real run:
Running 5 tests using 5 workers
✘ 1 …ests/module-3/debugging.spec.ts:19:1 › searches for the keyboard (30.1s)
✘ 2 …spec.ts:37:1 › the shop still shows products when the API is slow (8.3s)
✘ 3 …odule-3/debugging.spec.ts:24:1 › search updates the results count (2.5s)
✘ 4 …/module-3/debugging.spec.ts:14:1 › adds the Desk Lamp to the cart (2.8s)
✘ 5 …module-3/debugging.spec.ts:30:1 › checkout total includes 21% tax (7.4s)
Before reading on, try to sort them yourself.
1. Strict mode violation: the locator matches too much
Error: locator.click: Error: strict mode violation: getByRole('button', { name: 'Add to Cart' }) resolved to 5 elements:
1) <button data-testid="add-to-cart-5" …>Add to Cart</button> aka getByTestId('add-to-cart-5')
2) <button data-testid="add-to-cart-9" …>Add to Cart</button> aka getByTestId('add-to-cart-9')
…
Recognise it by: strict mode violation … resolved to N elements. Playwright even lists each match with a locator that would find just that one (aka getByTestId(...)).
Group: test bug. Fix: say which product you mean.
await page.getByTestId(/^product-\d+$/).filter({ hasText: "Desk Lamp" })
.getByRole("button", { name: "Add to Cart" }).click();
2. A locator that finds nothing: waiting until the timeout
Test timeout of 30000ms exceeded.
Error: locator.fill: Test timeout of 30000ms exceeded.
Call log:
- waiting for getByTestId('search-box')
Recognise it by: the call log stops at waiting for … and the test hits its 30-second timeout. Actions wait for the element to appear, so a wrong locator is not an immediate error; it is a long wait. The test id is search-input, not search-box.
Group: test bug, unless the element really disappeared from the product. Check the screenshot: the search box is there.
Fix: use the role and name the user sees: getByRole("textbox", { name: "Search for items" }).
3. A value read too early: timing
Error: expect(received).toBe(expected) // Object.is equality
Expected: "1"
Received: "10"
Recognise it by: expect(received) instead of expect(locator), and no Timeout: line. The assertion compared a plain string, once. The count was read right after typing, before the shop had filtered the list.
Group: test bug (a flaky one: on a fast day it might pass).
Fix: a web-first assertion that retries.
await expect(page.getByTestId("results-count")).toHaveText("1");
4. A wrong expectation
Error: expect(locator).toHaveText(expected) failed
Locator: getByTestId('cart-total')
Expected: "$72.99"
Received: "$72.59"
Timeout: 5000ms
Call log:
- Expect "toHaveText" getByTestId('cart-total') with timeout 5000ms
- waiting for getByTestId('cart-total')
11 × locator resolved to <span data-testid="cart-total">$72.59</span>
- unexpected value "$72.59"
Recognise it by: the element was found every time (11 × locator resolved), with the same value every time. Nothing is loading or flaky; the page and the test simply disagree.
Group: this is the one to think about. Either the product is wrong or the test is. Work it out: $59.99 + 21% tax ($12.60) = $72.59. The page is right; the expected value was calculated wrongly by hand.
Fix: correct the expectation, or better, calculate it with the totalPrice() helper from module 1 so nobody does the sum by hand again.
5. A slow environment
Error: expect(locator).toBeVisible() failed
Locator: getByTestId(/^product-\d+$/).first()
Expected: visible
Timeout: 5000ms
Error: element(s) not found
Recognise it by: element(s) not found after the full timeout, while the same locator works in other tests. Something was slower than the test allowed. The trace’s Network tab shows the products request taking six seconds.
Group: environment.
Fix: decide how long this test may wait, for this assertion only: toBeVisible({ timeout: 15_000 }).
The cheat sheet
| Error shows | Most likely | First check |
|---|---|---|
strict mode violation … resolved to N elements | Locator too broad | The listed matches |
waiting for … until the test timeout | Locator wrong, or element gone | Screenshot: is it there? |
expect(received) … no Timeout: | Value read too early | Use expect(locator) |
| Same unexpected value every retry | Wrong expectation, or a real bug | Do the sum; read the spec |
element(s) not found after the timeout | Slow page or backend | Trace: Network tab |
Exercise
Fix the four test bugs on the exercise branch, and handle the slow environment explicitly. Run the five tests until they pass, then compare with main:
git diff main -- tests/module-3
Show suggested solution
main, in tests/module-3/debugging.spec.ts. If your git diff is empty or only differs in wording, you found all five. The one that deserves a second look is number 4: before you change an expected value, prove the page is right. If the page had shown $72.99, that would have been a product bug to report, not a test to “fix”.