Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST TOOLS MATH

Playwright Debugging


Share

Debugging Playwright Tests

Debugging means finding out why a test does not do what you expect.

Playwright gives you several tools for this:

  • Clear error messages with a call log
  • The Playwright Inspector, to run a test step by step
  • page.pause(), to stop a test at one line (see Inspector)
  • Headed mode (a visible browser) and slowMo, to watch the test (see Inspector)
  • console.log(), to print values
  • The Trace Viewer, to replay a test after it ran

Read the Error Message

Always start with the error message.

This test has a mistake: the button on the login page says "Log in", not "Sign in":

Example

tests/login.spec.js

// @ts-check
import { test, expect } from '@playwright/test';

test('student can log in', async ({ page }) => {
  await page.goto('https://www.w3schools.com/playwright/practice/login.html');
  await page.getByLabel('Username').fill('student');
  await page.getByLabel('Password').fill('w3schools');
  await page.getByRole('button', { name: 'Sign in' }).click();
  await expect(page.getByTestId('welcome')).toHaveText('Welcome, student!');
});

Output:

Running 1 test using 1 worker

[1/1] [chromium] › tests/login.spec.js:4:1 › student can log in
  1) [chromium] › tests/login.spec.js:4:1 › student can log in ─────────────────────────────────────

    Test timeout of 30000ms exceeded.

    Error: locator.click: Test timeout of 30000ms exceeded.
    Call log:
      - waiting for getByRole('button', { name: 'Sign in' })


       6 |   await page.getByLabel('Username').fill('student');
       7 |   await page.getByLabel('Password').fill('w3schools');
    >  8 |   await page.getByRole('button', { name: 'Sign in' }).click();
         |                                                       ^
       9 |   await expect(page.getByTestId('welcome')).toHaveText('Welcome, student!');
      10 | });
      11 |
        at /home/user/my-project/tests/login.spec.js:8:55

    Error Context: test-results/login-student-can-log-in-chromium/error-context.md


  1 failed
    [chromium] › tests/login.spec.js:4:1 › student can log in ──────────────────────────────────────

Read it from the top:

  • The error: locator.click did not finish before the test timeout of 30 seconds
  • The call log: what Playwright was doing, here still waiting for a button named "Sign in"
  • The code frame: the line that failed, with a ^ under the call

When a locator waits until the timeout, it usually matches nothing on the page.

Check the name, the role and the spelling against the page.



The Error Context File

The last line of the error points to a file named error-context.md.

It holds the error, your test code, and a page snapshot: what the page looked like when the test failed.

The page snapshot lists the elements on the page, each with its role and name.

This is part of the page snapshot for the failed test above:

test-results/login-student-can-log-in-chromium/error-context.md

    - generic [ref=e12]:
      - generic [ref=e13]: Username
      - textbox "Username" [ref=e14]:
        - /placeholder: Enter your username
        - text: student
      - generic [ref=e15]: Password
      - textbox "Password" [active] [ref=e16]:
        - /placeholder: Enter your password
        - text: w3schools
      - paragraph [ref=e17]:
        - button "Log in" [ref=e18] [cursor=pointer]

The snapshot shows the problem: the page has a button named "Log in", and no button named "Sign in".


Print Values with console.log()

You can use console.log() in a test, like in any JavaScript code.

The output is shown in the terminal, next to the name of the test:

Example

tests/todo.spec.js

// @ts-check
import { test, expect } from '@playwright/test';

test('count after adding two todos', async ({ page }) => {
  await page.goto('https://www.w3schools.com/playwright/practice/todo.html');
  const input = page.getByPlaceholder('What needs to be done?');

  await input.fill('Learn Playwright');
  await input.press('Enter');
  console.log('After 1 todo:', await page.getByTestId('todo-count').textContent());

  await input.fill('Write tests');
  await input.press('Enter');
  console.log('After 2 todos:', await page.getByTestId('todo-count').textContent());

  await expect(page.getByRole('listitem')).toHaveCount(2);
});

Output:

Running 1 test using 1 worker

[1/1] [chromium] › tests/todo.spec.js:4:1 › count after adding two todos
[chromium] › tests/todo.spec.js:4:1 › count after adding two todos
After 1 todo: 1 item left

After 2 todos: 2 items left

  1 passed (2.7s)

Remember await when you log a value from the page, like textContent().

Without it, you print a promise (a value that is not ready yet) instead of the text.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.