Playwright Actions
Playwright Actions
An action is something a user does on the page, like typing, clicking or choosing an option.
In Playwright, you call the action on a locator:
Example
await page.getByLabel('Username').fill('student');
await page.getByRole('button', { name: 'Log in' }).click();
Before every action, Playwright waits until the element is ready, for example visible and enabled.
You will learn more about this in the Auto-Waiting chapter.
Fill in a Form
This test fills in every field of the practice sign up form:
Example
tests/form.spec.js
// @ts-check
import { test, expect } from '@playwright/test';
test('fill in the sign up form', async ({ page }) => {
await page.goto('https://www.w3schools.com/playwright/practice/form.html');
await page.getByLabel('Full name').fill('Ada Lovelace');
await page.getByLabel('Email').fill('ada@example.com');
await page.getByLabel('Age').fill('36');
await page.getByLabel('Course').selectOption('JavaScript');
await page.getByLabel('Start date').fill('2027-01-15');
await page.getByLabel('Advanced').check();
await page.getByLabel('Profile photo').setInputFiles('tests/fixtures/photo.png');
await page.getByLabel('About you').fill('I like puzzles.');
await page.getByLabel('I accept the terms').check();
await page.getByRole('button', { name: 'Sign up' }).click();
await expect(page.getByRole('status')).toHaveText(
'Thank you, Ada Lovelace! You signed up for JavaScript. Photo: photo.png.'
);
});
The upload needs a file, so the project has a small image at tests/fixtures/photo.png.
Any small file works for this form.
Example Explained
fill()clears the field and enters the new value. It works for text, email, number, date fields and textareas- A date field takes the date in the format
yyyy-mm-dd selectOption()chooses an option in a<select>, by its value or its labelcheck()checks a checkbox or a radio buttonsetInputFiles()chooses a file in a file input. The path is relative to the folder you run the tests fromclick()clicks the button
selectOption() also takes an object, or an array for a list where you can choose many options:
Example
await page.getByLabel('Course').selectOption({ value: 'js' });
await page.getByLabel('Course').selectOption({ label: 'JavaScript' });
To clear a file input, pass an empty array: setInputFiles([]).
Check and Uncheck
check() checks a checkbox, and uncheck() unchecks it.
If the box already has the right state, they do nothing, so they are safer than click().
Example
tests/checkbox.spec.js
// @ts-check
import { test, expect } from '@playwright/test';
test('check and uncheck a todo', async ({ page }) => {
await page.goto('https://www.w3schools.com/playwright/practice/todo.html');
await page.getByPlaceholder('What needs to be done?').fill('Buy milk');
await page.getByPlaceholder('What needs to be done?').press('Enter');
const box = page.getByRole('checkbox', { name: 'Complete Buy milk' });
await box.check();
await expect(page.getByTestId('todo-count')).toHaveText('0 items left');
await box.uncheck();
await expect(page.getByTestId('todo-count')).toHaveText('1 item left');
});
The test also uses press('Enter') to add the todo, the same way a user submits the form with the keyboard.
Mouse Actions
click() has options for other kinds of clicks, and there are actions for hovering and dragging.
These tests use the practice actions page:
Example
tests/mouse.spec.js
// @ts-check
import { test, expect } from '@playwright/test';
test('mouse actions', async ({ page }) => {
await page.goto('https://www.w3schools.com/playwright/practice/actions.html');
const button = page.getByRole('button', { name: 'Click me' });
const result = page.getByTestId('click-result');
await button.click();
await expect(result).toHaveText('Clicked');
await button.dblclick();
await expect(result).toHaveText('Double-clicked');
await button.click({ button: 'right' });
await expect(result).toHaveText('Right-clicked');
await page.getByText('Hover over me').hover();
await expect(page.getByRole('tooltip')).toHaveText('Tooltips appear when you hover.');
});
test('drag a card', async ({ page }) => {
await page.goto('https://www.w3schools.com/playwright/practice/actions.html');
await page.getByText('Write tests').dragTo(page.getByLabel('Done'));
await expect(page.getByTestId('done-count')).toHaveText('1 card done');
});
| Action | Description |
|---|---|
| click() | Clicks the element |
| dblclick() | Double-clicks the element |
| click({ button: 'right' }) | Right-clicks the element. The button can be 'left', 'right' or 'middle' |
| click({ modifiers: ['Shift'] }) | Clicks while holding a key, for example Shift |
| hover() | Moves the mouse over the element |
| dragTo(target) | Drags the element and drops it on another element |
Typing and Keys
fill() sets the whole value at once, and that is what you want in most tests.
Some pages react to every key, for example a search box that filters a list while you type.
For those, pressSequentially() types one character at a time, like a real keyboard:
Example
tests/keyboard.spec.js
// @ts-check
import { test, expect } from '@playwright/test';
test('type one key at a time', async ({ page }) => {
await page.goto('https://www.w3schools.com/playwright/practice/shop.html');
await expect(page.getByTestId('product')).toHaveCount(5);
await page.getByLabel('Search products').pressSequentially('mouse', { delay: 100 });
await expect(page.getByTestId('product')).toHaveCount(1);
});
test('keyboard shortcuts', async ({ page }) => {
await page.goto('https://www.w3schools.com/playwright/practice/actions.html');
const notes = page.getByLabel('Notes');
await notes.focus();
await page.keyboard.type('Hello');
await page.keyboard.press('ControlOrMeta+A');
await page.keyboard.press('Backspace');
await expect(notes).toHaveValue('');
await notes.fill('Buy milk');
await notes.press('ControlOrMeta+Enter');
await expect(page.getByTestId('note-status')).toHaveText('Note saved: Buy milk');
});
Output:
Running 2 tests using 2 workers
[1/2] [chromium] › tests/keyboard.spec.js:11:1 › keyboard shortcuts
[2/2] [chromium] › tests/keyboard.spec.js:4:1 › type one key at a time
2 passed (3.1s)
Example Explained
pressSequentially('mouse', { delay: 100 })types the letters one by one, with 100 milliseconds between themfocus()puts the cursor in the field without clicking itpage.keyboard.type()types into the element that has focus (the element that gets the keys you press)press('ControlOrMeta+A')presses Control+A on Windows and Linux, and Command+A on macOSnotes.press('ControlOrMeta+Enter')focuses the field and presses the shortcut that saves the note
Key names follow the browser's key names, for example Enter, Tab, Escape, ArrowDown, Backspace and F1.
Combine keys with +: Shift+Tab, Control+Shift+K.
Run All the Tests
The project now has four test files, and all of them pass:
Example
npx playwright test
Output:
Running 6 tests using 6 workers
[1/6] [chromium] › tests/keyboard.spec.js:4:1 › type one key at a time
[2/6] [chromium] › tests/checkbox.spec.js:4:1 › check and uncheck a todo
[3/6] [chromium] › tests/mouse.spec.js:4:1 › mouse actions
[4/6] [chromium] › tests/form.spec.js:4:1 › fill in the sign up form
[5/6] [chromium] › tests/keyboard.spec.js:11:1 › keyboard shortcuts
[6/6] [chromium] › tests/mouse.spec.js:22:1 › drag a card
6 passed (3.4s)
Tip: Prefer actions on locators, like button.click(), over the low-level page.mouse and page.keyboard.
Locator actions wait for the element, and scroll the page so the element can be seen, before they act.