Playwright · Senior · Question 23 of 77
Which pattern often causes flaky tests after a click that navigates?
await page.getByRole("link", { name: "Orders" }).click();
const title = await page.locator("h1").textContent();
expect(title).toBe("Orders");- 1Using
getByRoleinstead of a CSS selector - 2Reading text once, before the new page has loaded
- 3Clicking a link instead of calling
page.goto
Show the answer
Correct answer: Reading text once, before the new page has loaded
Explanation
textContent() doesn't wait for the new heading, so it may read the old page. await expect(page.locator('h1')).toHaveText('Orders') retries.