Data Grid
30 employees loaded from the API, 5 per page, with search and row selection.
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');| Name | Department | City |
|---|
Loading...
Next section · Dialogs & WindowsJavaScript Dialogs →Handle native alert, confirm and prompt dialogs, including the ones that arrive in pairs, late, or when you leave.
Related exercises
- TablesIntermediateSortable TableProve a table is sorted correctly, in both directions.
- Network & APIIntermediateAPI PlaygroundCheck both what the UI shows and what actually went over the network.
- Dynamic ElementsAdvancedFlaky LabMake tests that pass every time, then prove it by running them many times.
Read the guide: Playwright auto-waiting: how to fix flaky tests