Test id, lists and the all() trap
all() and count() return the current state without waiting, while textContent() waits only for the element and reads it once. So a loop over all() on a still-empty list runs zero times and the test is falsely green. Collections are checked with retrying assertions: toHaveCount, toHaveText with an array. getByTestId is used when there is no stable role or text; testIdAttribute sets its attribute.
Playwright · Locators
The test is green but the list was still loading, and the loop checked nothing. How can that happen?
A test iterates over list items and checks that each has an image. It is green. Then the image is removed from one product and the test stays green. The cause is the line for (const item of await items.all()): when it ran, the list was still loading, the array was empty and the loop ran zero times. This lesson covers when getByTestId fits, how to check collections with retrying assertions, and why locator.all() is dangerous without a wait.
getByTestId and its configuration. By default getByTestId looks for the data-testid attribute. If your project uses another one, such as data-qa, set it in the config and getByTestId then uses it everywhere:
// playwright.config.ts
export default defineConfig({
use: { testIdAttribute: 'data-qa' },
});
// in a test
await page.getByTestId('cart-badge').click(); // [data-qa="cart-badge"]
await expect(page.getByTestId('total')).toHaveText('$1,250');
A test id fits when there is no stable role and text: icons without a label, dynamic containers, repeated cards, a multilingual UI. Where a role and a name exist, getByRole is better because it also checks accessibility. A test id does not depend on copy or markup, but it must be maintained in the app, so agree on naming (order-row, not arbitrary strings).
How it works: a snapshot versus waiting. Collection assertions (toHaveCount, toHaveText with an array, toContainText) retry until the timeout while the condition is false. locator.all() takes a one-moment snapshot: it returns an array of locators for the elements that are in the DOM right now and does not wait. The same goes for count(), allTextContents() and isVisible(). textContent() and innerText() wait for the element to appear but read the text once: all of them return the value "here and now", with no retries. That is a deliberate design: such methods are for logic ("is the dialog open?") where waiting is not wanted. The danger starts when they are used as a check.
A snapshot on an empty list
Example: a falsely green test and the fix.
// before: if the list has not loaded yet, the loop checks nothing
for (const item of await page.getByRole('listitem').all()) {
await expect(item.getByRole('img')).toBeVisible();
}
// after: first wait for a known count
const items = page.getByRole('listitem');
await expect(items).toHaveCount(3);
for (const item of await items.all()) {
await expect(item.getByRole('img')).toBeVisible();
}
The fix works because toHaveCount retries until the timeout and fails if the list never appears. After it, the snapshot is guaranteed to see three elements. The best version has no loop at all: "every item has an image" can be expressed with filter:
await expect(items.filter({ has: page.getByRole('img') })).toHaveCount(3);
Collection assertions. Instead of loops over elements, prefer retrying assertions:
const items = page.getByRole('listitem');
await expect(items).toHaveCount(3);
await expect(items).toHaveText(['Bread', 'Milk', 'Cheese']); // count, order, text
await expect(items).toContainText(['Bread', 'Cheese']); // text fragments
await expect(items).toHaveText([/Bread/, /Milk/, /Cheese/]); // regex for each
count() or toHaveCount()
expect(await items.count()).toBe(3)
The value is read once. If the list is still loading, the test fails at once or, worse, checks a stale state.
await expect(items).toHaveCount(3)
Playwright repeats the check until the timeout. The test is stable and fails only when the count is really wrong.
When the count is really needed for logic. When the value changes asynchronously, wrap it in expect.poll, which repeats the function until it succeeds:
await expect.poll(() => page.getByRole('listitem').count()).toBeGreaterThan(0);
Limits. toHaveText with an array checks count and order, so for a list with unstable order it will fail falsely: there you check individual elements with filter and toBeVisible. all() also has honest uses: the count is unknown in advance but the list is already stable (after await expect(spinner).toBeHidden()), or each element needs complex checks that do not fit one assertion. In Selenium the equivalent findElements also returns an empty list without an error, while Cypress cy.get retries by itself, so the trap is less visible there.
Common mistakes.
- A loop over
all()without a wait: a falsely green test on an empty list. expect(await locator.count()).toBe(3)instead oftoHaveCount(3): no retries.- Changing
testIdAttributemid-project without agreement: everygetByTestIdbreaks. - A test id on everything although an accessible role exists: you lose the accessibility check.
toHaveTextwith an array for a list whose order is not guaranteed: the test is flaky.
Check yourself
Why can the loop for (const row of await rows.all()) run with no checks and leave the test green?
Answer
all() does not wait and returns the elements that are in the DOM at that moment. If the list is still loading, the array is empty and the loop runs zero times. Put an assertion that waits before it, such as toHaveCount(n).
When is getByTestId better than getByRole, and when not?
Answer
Better when there is no stable role or text: an icon without a label, a container, a card, a multilingual UI. Worse when a role and name exist: a role also checks accessibility and a test id does not.
toHaveCount and toHaveText with an array because they retry. all() does not wait, so I put a count assertion before it. Middle/Senior: all(), count() and textContent() take a snapshot, and on an empty list a loop gives a falsely green test. I use them for logic, not for checks, and wrap changing values in expect.poll. I take getByTestId when there is no role or text, configure testIdAttribute if needed and agree on naming with developers.