lecture 20 of 200 completed

Reading a failing test

NoSuchElement, stale elements and assertion errors. Failures are reports, not riddles.

When a test fails, most beginners do one of two things. They change something at random and run it again, or they ask a senior engineer. Both are slow.

Selenium names its failures precisely. Each error means one specific thing, and once you know the four names below you can usually find the cause in a minute. Interviewers ask about these too, so it is worth learning them properly.

NoSuchElementException. findElement found no match at that moment. Check in this order:

  1. The selector is wrong. Open the app, inspect the element, compare the real attribute. Most of the time it is this.
  2. The lookup ran too early. The element appears after a delay and you did not wait. The fix is an explicit wait, driver.wait(until.elementLocated(...), 5000), never a sleep.

StaleElementReferenceException. You found an element, the page re-rendered, and your saved reference now points at a DOM node that no longer exists. The fix: find the element again after the action that changes the page. Keeping element references across page updates is the classic Selenium beginner trap.

TimeoutError from driver.wait. The condition never became true within the timeout. Same two causes as NoSuchElement: wrong selector, or the app never reached that state at all. Raising the timeout almost never helps. If the toast never appears, 60 seconds fails the same way, slower.

An assertion error.

AssertionError: Expected '1', got '0'

This one is a gift: the app really did something different from your claim. Either the claim is wrong (fix the test) or the app is broken (you found a bug). Deciding which is the whole job, and it starts with reading the message, not re-running and hoping.

On TestAcademy there is a third verdict you will meet: "your test passed, but it also passed when we broke the app." That is not fixed with selectors or waits. It means an assertion is missing or too weak. Go back to what the test claims.

The official error guide: Understanding common errors.

The final problem of this track: prove that logout really logs you out. Read every failure you hit on the way. Each one tells you your next step.

now prove it

Reading is the setup. Solve this problem in the editor. We break the app on purpose to check your test would catch it.