← Back to library
Playwrightassertionsbeginnerv1.20+

Assert element is visible and contains text

Basic but essential: check an element exists and shows the right content.

1test('element is visible with correct text', async ({ page }) => {
2  await page.goto('/home');
3
4  const heading = page.getByRole('heading', { level: 1 });
5
6  // Playwright auto-waits for the element and retries assertions
7  await expect(heading).toBeVisible();
8  await expect(heading).toHaveText('Welcome back');
9
10  // Partial match
11  await expect(heading).toContainText('Welcome');
12});
#visible#text#toBeVisible#toHaveText