Rows, cells and columns

Find the row first, then the cell

Tables are a grid of row and cell roles. Find the row by something unique in it, then look inside that row only:

const michael = page.getByRole('row').filter({ hasText: 'Michael' });

await expect(michael.getByRole('cell').nth(2)).toHaveText('michael@example.com');

Header and footer rows are rows too

getByRole('row') counts the header row and the footer row as well. To count only the data, look inside the <tbody>:

await expect(page.getByRole('row')).toHaveCount(6); // header + 4 users + footer
await expect(page.locator('tbody').getByRole('row')).toHaveCount(4);

Find a cell by its column name

Playwright has no built-in "cell in column X". Look up the column's position from the headers, then use it in the row. That keeps working if the columns are reordered:

const headers = await page.getByRole('columnheader').allTextContents();
const roleColumn = headers.indexOf('Role');

const anna = page.getByRole('row').filter({ hasText: 'Anna' });
await expect(anna.getByRole('cell').nth(roleColumn)).toHaveText('User');

Assert a whole column at once

toHaveText() accepts an array and checks every element, in order:

await expect(page.getByRole('columnheader')).toHaveText([
  'First Name', 'Last Name', 'Email', 'Age', 'Role',
]);

Users

List of users
First NameLast NameEmailAgeRole
JohnSmithjohn@example.com32Admin
AnnaKowalskianna@example.com28User
MichaelBrownmichael@example.com41Manager
SarahWilsonsarah@example.com35User
Total: 4 users