Table Actions
Practice interacting with buttons inside table rows.
Scope every action to its row
Every row has its own Edit and Delete buttons, so a page-wide getByRole('button', { name: 'Delete' }) matches four buttons. Find the row first, then the button inside it:
const anna = page.getByRole('row').filter({ hasText: 'Anna' });
await anna.getByRole('button', { name: 'Delete' }).click();
await anna.getByRole('button', { name: 'Yes, delete' }).click();
await expect(page.getByRole('row').filter({ hasText: 'Anna' })).toHaveCount(0);Rows change while you work with them
In edit mode, the names become input values, and filter({ hasText }) only reads text, so it no longer finds the row. Find the editing row by what it contains instead:
const editingRow = page.getByRole('row').filter({
has: page.getByRole('textbox', { name: 'Email' }),
});
await editingRow.getByRole('textbox', { name: 'Email' }).fill('anna@test.dev');
await editingRow.getByRole('button', { name: 'Save' }).click();Scope forms too
The add form also has First name, Last name and Email fields. While a row is being edited at the same time, page-wide getByLabel() finds both. Scope to the form:
await page.getByRole('button', { name: 'Add user' }).click();
const form = page.getByRole('form', { name: 'New user' });
await form.getByLabel('First name').fill('Zoe');Empty tables
When the last user is deleted, the table shows a message row instead of data. Assert that state too: an empty table is easy to forget and easy to break.
User Management
| First Name | Last Name | Actions | |
|---|---|---|---|
| John | Smith | john@example.com | |
| Anna | Kowalski | anna@example.com | |
| Michael | Brown | michael@example.com | |
| Sarah | Wilson | sarah@example.com |
Related exercises
- TablesBeginnerBasic TableRead and locate data in a table.
- Dialogs & WindowsIntermediateModals & OverlaysTest modals built into the page: native and custom, stacked, and one that interrupts you.
- LocatorsIntermediateLocator ChallengesTarget exactly one element in each section without CSS selectors or test ids.
Read the guide: Playwright locators: a practical guide with exercises