Playwright + Java · Lesson 25 of 38

0/38 done

Module 25: Browser Events

25. Event Listeners: Console Logs, Network Traffic & Page Errors

15 minlesson

Your task

Implement registerConsoleErrorTracker:

  1. Accept Page page and List<String> errorLogs.
  2. Listen to page.onConsoleMessage and add msg.text() to errorLogs if "error".equals(msg.type()).

Write it in the editor, then press Check answer (Ctrl + Enter).

Monitoring Browser Events in Java

java
page.onPageError(error -> {
    System.err.println("Client JS Exception: " + error);
});

page.onConsoleMessage(msg -> {
    if ("error".equals(msg.type())) {
        System.err.println("Console Error: " + msg.text());
    }
});

Key Takeaways & Walkthrough Summary

Playwright allows you to listen to internal browser events in real time. In this lesson, you will capture browser console errors, unhandled JavaScript exceptions, network requests, and WebSocket messages during test execution. Let's inspect browser events!

Part of the Playwright + Java course (Playwright with Java). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

Can I fail tests if any console.error() is emitted by the frontend?

Yes! Storing console messages in a list and asserting errorLogs.isEmpty() in an afterEach hook is a great QA practice to catch client regressions.

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

Java
Skip