Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readTesting

Debug a failing Playwright test with UI mode

Inspect the page before and after each action in a failing Playwright test using UI mode or a trace file.

Your Playwright test fails with element(s) not found and the page looks fine when you open it by hand. Open the Playwright trace to see what the page displayed when the assertion failed.

This test fails that way. The page is inline so you can run it as written.

// tests/cart.spec.ts
import { expect, test } from '@playwright/test'

const page = `
  <main>
    <label>Code <input aria-label="Promo code" /></label>
    <button id="apply">Apply</button>
    <p id="total">Total: $100</p>
  </main>
  <script>
    document.getElementById('apply').addEventListener('click', () => {
      const code = document.querySelector('input').value
      setTimeout(() => {
        document.getElementById('total').textContent =
          code === 'SAVE10' ? 'Total: $90' : 'Total: $100'
      }, 200)
    })
  </script>
`

test('promo code takes 10 off', async ({ page: p }) => {
  await p.setContent(page)
  await p.getByLabel('Promo code').fill('save10')
  await p.getByRole('button', { name: 'Apply' }).click()
  await expect(p.getByText('Total: $90')).toBeVisible({ timeout: 2000 })
})

Save it as tests/cart.spec.ts in a Playwright Test project. A headless run reports the failed assertion.

$ npx playwright test
  1) tests/cart.spec.ts › promo code takes 10 off

    Error: expect(locator).toBeVisible() failed

    Locator: getByText('Total: $90')
    Expected: visible
    Timeout: 2000ms
    Error: element(s) not found

The text never appeared. The output does not say what the page showed instead.

Step through it in UI mode

UI mode shipped in Playwright 1.32. Start it with the test command and one flag, npx playwright test --ui.

A window opens with your tests on the left. Press the run triangle next to the test, and the Actions list fills with every step and its duration: Set content, Fill "save10", Click, Expect "toBeVisible".

Click a step and the middle panel shows the page as it was at that moment. The Before and After tabs above it show the page on each side of the action. On the Fill step the input was empty in Before and held save10 in After.

Click the red Expect step. The snapshot shows the input holding save10 and the total still at $100. The component compares against SAVE10, so my test typed the wrong case.

Change the input to SAVE10 and rerun the test. The total changes to $90 and the assertion passes.

Use Source to find the line behind the selected action. Errors, Console and Network show what happened during the run.

The same view without the window

On a CI box or over SSH, record a trace and open it later.

npx playwright test --trace on
npx playwright show-trace path/to/trace.zip

--trace on writes a trace.zip for each test attempt. Use the path from the failure report or open the trace attached to the HTML report. The viewer shows the same Actions list and the same Before and After snapshots. --trace retain-on-failure keeps the file only for tests that fail.