A contact-form test goes red. The terminal says it waited for Success! and did not find it. The trace says the click really happened and the network went quiet right after it. Both are true, so who is leaving something out? This lesson gives you five places to ask, from the quickest to the most thorough, and a rule: stop at the first one that answers. It follows the official docs (Debugging tests, Trace viewer, Dialogs), and every command and output comes from a real run.
What you need
- Node.js. 22, 24, 26.
- Playwright 1.65.1. The lesson was recorded with this version; other versions may print slightly different output.
- The code. Subscribers see the lesson repository in the Source code box under the video, with the tags
pw04-startandpw04-end.
The demo runs against automationexercise.com. It sometimes shows a "Please wait" page to automated browsers, so a run can fail for a reason that has nothing to do with your test. Times and counts you see in the tools will differ from the video.
Step 1: Two failures, not one
tests/contact.spec.ts fills the four fields of the contact form, clicks Submit and expects Success!:
npx playwright test tests/contact.spec.ts --project=site --workers=1
Fails on purpose.
contact.spec.tsis red by design.
| Run | What the terminal says | Real cause |
|---|---|---|
| First | fill on the Name field timed out after 30 seconds | the site showed a waiting page, so there was no Name field at all |
| Second | expect(locator).toBeVisible() failed, getByText('Success!'), timeout 5 seconds, line 11 | something after the click, which Step 9 finds |
The line in the error is where the test fell. Where it tripped is usually earlier.
Step 2: Station 1, the terminal
Everybody asks the terminal first. Read the error top to bottom: message, locator, call log, source line. On the second run it points at expect on line 11, and every word of it is true. But it only tells the end of the story: filling the form and clicking Submit are not in it.
Step 3: Station 2, error-context.md
When a test fails, Playwright saves test-results/<test folder>/error-context.md: a snapshot of the page at the moment of the failure. On the first run it is a single line, Please wait, which explains the 30-second timeout. On the second run the page is all there: Name still says Vi, the message still says Hi team, and the Submit button is still in place. The click happened, so why did the form not move?
Step 4: Station 3, --debug and the Inspector
To stand next to the click, run the test with the debug flag:
npx playwright test tests/contact.spec.ts --project=site --workers=1 --debug
A real browser opens, and the Playwright Inspector waits for you to step. Its Log column records every step. At the click you see a green check and no dialog; the page just sits there. The click did happen. Whatever went wrong came after it, faster than the eye can follow.
Step 5: Turn the trace on
"Faster than the eye" needs a recording: the trace. Add one line in use of your config:
trace: 'retain-on-failure',
It records every run and keeps the file only when the test fails. If your config already has trace: 'on-first-retry', change that line instead of adding a second one. The site project of the lesson repository already has it. Run the test again: test-results/ now has a folder named after the test file and title, with two files, error-context.md and trace.zip.
Step 6: Open the trace
npx playwright show-trace test-results/contact-contact-form-site/trace.zip
Playwright prints this command at the end of the error log; copy it from there. The other way is npx playwright show-report, which opens the report of the last run, where a trace icon sits next to the test. That needs the html reporter; the lesson config uses list, so here you open the trace directly. While you write tests on your own machine, the docs recommend UI mode.
Step 7: Station 4, the Trace Viewer
The viewer shows a film strip of the run, the actions on the left, and tabs on the right:
| Tab or view | What it told us |
|---|---|
| Before / After of the click | the page did not change, apart from the dot that marks the click |
| Log | the full log of the click: wait for the button, find exactly one element, check visible, enabled and stable, scroll into view, then click. Every condition passed |
| Network | requests only while the page loads. After the click, none |
| Console | red lines from the website itself, nothing from the test |
| Errors | one error: the expect on line 11, with a Copy prompt button |
So the click did not fail, and the form was not sent. The viewer showed everything it chooses to show, and the reason is not in it.
Step 8: The page's own code
Open view-source on the Contact Us page. When the form is submitted, the page calls confirm("Press OK to proceed!"). OK shows Success!. Cancel returns false and nothing happens. Nothing happening is exactly what you saw. But the test never pressed Cancel, so who did?
Step 9: Station 5, the trace file
The Trace Viewer only shows what it chooses to show; the trace file keeps everything. It is a zip: unzip it and search for dialog. There is one line: a confirm box, a fraction of a second after the click. The dialog did open, and closed before it reached the film.
The docs say who closed it: dialogs are auto-dismissed by Playwright. The test registered no handler, so Playwright pressed Cancel itself. The viewer in 1.65.1 has no tab that shows this, so the clue was in what did not happen: after the click the page did not move, the network went quiet, and the form kept its text. When you see all three, suspect a dialog.
Step 10: Handle the dialog the right way
There is a trap, and the docs warn about it with a wrong example: a handler that only prints the message.
// Wrong: the dialog is neither accepted nor dismissed, so the page hangs
page.on('dialog', (dialog) => console.log(dialog.message()));
// Right: accept (or dismiss) it
page.on('dialog', (d) => d.accept());
A web dialog blocks the whole page, so a handler that does nothing hangs the click. You must call accept or dismiss. With no handler at all, Playwright dismisses it for you, as in this lesson.
Step 11: One line, then the second error
tests/contact-ok.spec.ts registers the handler before page.goto, because the dialog opens on the click. Run the version with only that line added and it is still red, but this time the terminal is enough: strict mode violation, getByText('Success!') resolved to 2 elements. One is the real message in #contact-page, the other is in the newsletter box hidden in the footer. The first error was hiding the second. Narrow the locator:
const form = page.locator('#contact-page');
await expect(form.getByText('Success!')).toBeVisible();
npx playwright test tests/contact-ok.spec.ts --project=site --workers=1
You should see 1 passed.
Which tool first
| Station | Ask it when |
|---|---|
| 1. Terminal | the error already names the cause, like the strict mode violation |
| 2. error-context.md | you need to know what the page looked like (the "Please wait" page) |
3. --debug | you want to walk each step yourself |
| 4. Trace Viewer | you need before and after of every action, and the network |
| 5. Trace file | only it still holds the dialog that was dismissed |
Tests that fail on purpose
| File | Fails because |
|---|---|
contact.spec.ts | no dialog handler: Playwright dismisses the confirm box and Success! never shows up |
Check your result
npx playwright test tests/contact.spec.ts --project=site --workers=1→ 1 failed, and atrace.zipintest-results/npx playwright test tests/contact-ok.spec.ts --project=site --workers=1→ 1 passed
Recap
| Question | Tool |
|---|---|
| Where did the test fall? | terminal |
| What did the page look like? | error-context.md |
| What happens step by step? | --debug |
| What happened between the steps? | Trace Viewer and the trace file |
- The error line is where the test fell, not always where it tripped.
- Stop at the first station that answers.
- Add
trace: 'retain-on-failure'now, so the film is already on the desk next time. - If the page does not move, the network is quiet and the form keeps its text, suspect a dialog, and call
acceptordismiss.
Next lesson
Next, we tidy these tests with hooks and fixtures.