68 / 139 · 13 Browser Automation with Playwright · Contexts, Waiting, and Assertions← prev⊞ allnext →☰ Read as one page
6.5Putting It All Together
test('add item to cart', async ({ page }) => {
await page.goto('/products');
// Auto-wait: clicks when button is ready
await page.getByRole('button', { name: 'Add to Cart' }).first().click();
// Web-first assertion: retries until cart badge shows "1"
await expect(page.getByTestId('cart-count')).toHaveText('1');
// Navigate and verify
await page.getByRole('link', { name: 'Cart' }).click();
await expect(page).toHaveURL(/.*cart/);
await expect(page.getByRole('listitem')).toHaveCount(1);
});
No explicit waits. No sleep statements. No flaky timing issues. The auto-waiting and web-first assertions handle all synchronization.