Acting on one specific row

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

User management table
First NameLast NameEmailActions
JohnSmithjohn@example.com
AnnaKowalskianna@example.com
MichaelBrownmichael@example.com
SarahWilsonsarah@example.com