Playwright + TS · Lesson 18 of 36

0/36 done

Module 18: Actions

18. Simulating Real User Interactions & Gestures

18 minlesson

Try it yourself

Implement validateRegistrationPayload:

  1. Check email contains '@'.
  2. Ensure agreeToTerms is true.
  3. Validate countryCode has length equal to 2.

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

The Core Actions API

typescript
// Text Inputs:
// fill() clears existing text and types the new text instantly
await page.getByLabel("Email").fill("qa@targetqa.com");

// press() fires keyboard keys (Enter, ArrowDown, Backspace)
await page.getByLabel("Search").press("Enter");

// Checkboxes and Radio buttons:
await page.getByLabel("Subscribe to newsletter").check();
await page.getByLabel("Dark theme").uncheck();

// Select Dropdowns:
await page.getByLabel("Country").selectOption("CA"); // by value
await page.getByLabel("Country").selectOption({ label: "Canada" }); // by visible label

// Mouse gestures:
await page.getByRole("button", { name: "Save" }).click({ button: "right" }); // right-click
await page.getByTestId("menu-item").hover(); // hover to reveal dropdown
await page.locator("#source-card").dragTo(page.locator("#target-column"));

fill() vs pressSequentially()

  • fill(): Sets the input value directly (ideal for fast form completion).
  • pressSequentially(): Types character by character with configurable delays (ideal for testing real-time search autosuggest or masked credit card inputs).
typescript
await page.getByPlaceholder("Search products").pressSequentially("Laptop", { delay: 100 });

File Upload & Download

typescript
// Upload file
await page.getByLabel("Upload Profile Picture").setInputFiles("test-data/avatar.png");

// Download file
const downloadPromise = page.waitForEvent("download");
await page.getByRole("button", { name: "Export CSV" }).click();
const download = await downloadPromise;
await download.saveAs("/tmp/export.csv");

Key Takeaways & Walkthrough Summary

Playwright actions simulate authentic user interactions. Before clicking or typing, Playwright automatically checks that the target element is visible, stable, enabled, and ready to receive events. In this lesson, we will master click, fill, check, selectOption, drag and drop, and file uploads. Let's get hands-on!

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

Common Doubts & Lesson Q&A

Why does fill() not trigger onKeyPress listeners?

fill() dispatches standard input and change events. If a reactive UI specifically relies on keydown/keyup events, use pressSequentially() instead.

Can Playwright drag and drop across frames?

Yes! Using page.mouse.down() and page.mouse.move() or locator.dragTo() allows smooth drag interactions.

Finished reading?

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

TypeScript
Skip