In Playwright, actions are the building blocks of tests. They represent every interaction a user has with an application, such as clicking buttons, filling out forms, or navigating between pages. Playwright actions are auto-waiting by default, which means Playwright automatically waits for the target element to be visible, enabled, and ready before performing the action. This behavior makes tests more stable and reduces the need for manual waits.
Let’s look at some commonly used Playwright actions and their use cases.
Navigation Actions
These actions are used to control browser navigation, similar to how a user moves through pages using the browser controls.
|
Action |
Description |
Example |
|
page.goto(url) |
Open a URL |
await page.goto('https://example.com'); |
|
page.reload() |
Reload current page |
await page.reload(); |
|
page.goBack() |
Navigates back in browser history |
await page.goBack(); |
|
page.goForward() |
Navigates forward in browser history |
await page.goForward(); |
Basic Element Actions
These are the most common actions performed on locators to interact with elements on a page, such as buttons, inputs, and links.
|
Action |
Description |
Example (locator) |
|
click() |
Click an element |
await page.locator('#login').click(); |
|
dblclick() |
Double-click |
await page.locator('.file').dblclick(); |
|
hover() |
Move mouse over element |
await page.locator('text=Profile').hover(); |
|
fill(value) |
Clear + type new value |
await page.locator('#email').fill('user@test.com'); |
|
pressSequentially(value) |
Replicates keyboard action of typing a text |
await page.locator('#name'). pressSequentially ('John'); |
|
press(key) |
Press a keyboard key on element |
await page.locator('#search').press('Enter'); |
|
clear() |
Clear text |
await page.locator('#search').clear() |
|
focus() |
Set focus on element |
await page.locator('#name').focus(); |
|
blur() |
Remove focus |
await page.locator('#name').blur(); |
|
scrollIntoViewIfNeeded() |
Scroll element into view |
await page.locator('#footer').scrollIntoViewIfNeeded(); |
Form Controls (Checkboxes and Radio Buttons)
These actions are specifically designed for checkbox and radio button interactions.
|
Action |
Description |
Example (locator) |
|
check() |
Check a checkbox/radio |
await page.getByLabel('I agree').check(); |
|
unCheck() |
Uncheck a checkbox |
await page.getByLabel('I agree').uncheck(); |
|
isChecked() |
Check state (returns boolean) |
const state = await page.getByLabel('I agree').isChecked(); |
Keyboard Actions
Keyboard actions simulate real keyboard interactions. These are useful for shortcuts, form navigation, and accessibility testing.
|
Action |
Description |
Example |
|
keyboard.type(text) |
Type text at current focus |
await page.keyboard.type('Hello World'); |
|
keyboard.press(key) |
Press a key (can include modifiers) |
await page.keyboard.press('Enter'); |
|
keyboard.down(key) |
Key down (no release yet) |
await page.keyboard.down('Control'); |
|
keyboard.up(key) |
Key up (release) |
await page.keyboard.up('Control'); |
|
Combo example |
Key combo like Ctrl+A |
await page.keyboard.press('Control+A'); |
Mouse Actions
Mouse actions are useful for drag-and-drop, drawing, canvas interactions, or precise UI control.
|
Action |
Description |
Example |
|
mouse.move(x, y) |
Move to coordinates |
await page.mouse.move(100, 200); |
|
mouse.click(x, y) |
Click at coordinates |
await page.mouse.click(150, 250); |
|
mouse.dblclick(x, y) |
Double-click at coordinates |
await page.mouse.dblclick(150, 250); |
|
mouse.down() |
Press mouse button |
await page.mouse.down(); |
|
mouse.up() |
Release mouse button |
await page.mouse.up(); |
|
mouse.wheel(dx, dy) |
Scroll with wheel |
await page.mouse.wheel(0, 400); |
These actions cover the most common interactions used in Playwright automation. Understanding these grouped actions helps in writing stable, readable, and maintainable test scripts.
Drag and Drop, Dropdown actions, File upload and advanced actions were explained in next section.