Search, paginate and select

The table reloads: wait for the new state

Every search and page change sends a request. Until it returns, the old rows are still on screen. A locator alone cannot tell old rows from new ones, so first assert something that changes, such as the page indicator. Then read the rows.

await page.getByRole('button', { name: 'Next page' }).click();
await expect(page.getByTestId('page-indicator')).toHaveText('Page 2 of 6');
await expect(page.locator('tbody').getByRole('row')).toHaveCount(5);

Prefer search over paging

To find one person, search for them. It is faster and does not depend on page order:

await page.getByLabel('Search employees').fill('Quinn');
await expect(page.getByTestId('result-count')).toHaveText('1 employee found');
await expect(page.getByRole('row').filter({ hasText: 'Quinn Harris' })).toBeVisible();

When you must page: loop until found

Check the current page; if the row is not there, go to the next page and wait for it to load before checking again:

const target = page.getByRole('row').filter({ hasText: 'Owen Virtanen' });
const indicator = page.getByTestId('page-indicator');

for (let pageNumber = 1; !(await target.isVisible()); pageNumber++) {
  await page.getByRole('button', { name: 'Next page' }).click();
  await expect(indicator).toHaveText(`Page ${pageNumber + 1} of 6`);
}

Without the indicator check, the loop can click Next again before the page has loaded and skip right past the row.

Checkboxes inside rows

await page.getByRole('checkbox', { name: 'Select Liam Nowak' }).check();
await page.getByRole('checkbox', { name: 'Select all on this page' }).check();
await expect(page.getByTestId('selected-count')).toHaveText('5 selected');
0 selected
Employees
NameDepartmentCity