Every way a user can click, hover and type. Each block shows a result you can assert on.
Every solution on this page starts with this hook. With baseURL set to https://hiraanachwani.com in your playwright.config.ts, it opens this page before each test.
The simplest action. The counter shows how many times the button was clicked.
Try this
Click Click me three times and check the result says Clicked 3 times.
Click Reset and check the count goes back to 0.
Locator tip:page.getByRole('button', { name: 'Click me' }) matches the button's text. Add { exact: true } if another button's name contains the same words.
Right-clicking the box opens a custom context menu instead of the browser's own.
Right-click anywhere in this box
Open
Rename
Delete
Try this
Right-click the box and check the menu appears with three items.
Choose Rename and check the result says You chose: Rename and the menu closes.
Locator tip: right-click is a click with an option: click({ button: 'right' }). The menu items have the menuitem role, so find them with getByRole('menuitem', { name: 'Rename' }).
Focus the box and press keys. It shows the last key and any modifiers held with it.
Try this
Press Enter in the box and check the result says You pressed: Enter.
Press Control+K and check the result shows both keys.
Type hello one key at a time and check the last key was o.
Locator tip:press('Control+K') sends a key combination. pressSequentially('hello') types one key at a time, firing every key event, unlike fill() which sets the value in one go.
A button that stays disabled until you accept, and a toggle button that remembers whether it's on.
Try this
Check Continue is disabled, tick the checkbox, and check it becomes enabled.
Click Continue and check the result starts with Continued.
Click Notifications and check it is now pressed and the result says Notifications on.
Locator tip: a toggle button reports its state with aria-pressed. Playwright can match on it directly: getByRole('button', { name: 'Notifications', pressed: true }).