Playwright + JS · Lesson 23 of 35

0/35 done

Module 23: Browser Events & File Handling

23. Listening to Browser Events & Managing Files

15 minlesson

Try it yourself

Implement validateFileSize:

  1. Accept sizeBytes number and minBytes number (default 1024).
  2. Return sizeBytes >= minBytes.

The editor has a worked example. Read it, tweak it, then press Mark as complete.

Browser Events & Download Handling

javascript
// Catch console errors:
page.on("pageerror", (error) => {
  console.error("Uncaught exception on page:", error.message);
});

// Download verification:
const downloadPromise = page.waitForEvent("download");
await page.getByRole("button", { name: "Download Invoice" }).click();
const download = await downloadPromise;
await download.saveAs("test-results/invoice.pdf");

Key Takeaways & Walkthrough Summary

A comprehensive automated test does more than click buttons; it also verifies that no console errors occurred and validates downloaded files. In this lesson, we will learn how to capture browser events, intercept console logs, handle custom file uploaders, and verify downloaded documents. Let's get started!

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

Common Doubts & Lesson Q&A

Where are temporary downloads stored before saveAs()?

Playwright stores downloads in a temporary isolated cache directory and auto-deletes them when the context closes if not saved.

Finished reading?

Review the worked example in the editor, then mark the lesson complete.

JavaScript
Skip