Rayrun

How do I use Playwright to verify a webpage's title?

Answer

Checking a Webpage's Title with Playwright

You can easily verify a webpage's title using Playwright's expect function. Here's how:

First, navigate to the webpage using the page.goto() method. For example, let's say you want to check the title of a page on https://ray.run/.

await page.goto('https://ray.run/');

Next, use expect(page).toHaveTitle('foo') to assert that the title of the page is "foo". This assertion will automatically wait for the title to match "foo" before proceeding.

await expect(page).toHaveTitle('foo');

Here's the full code:

import { test, expect } from '@playwright/test';

test('has title', async ({ page }) => {
  await page.goto('https://ray.run/');
  await expect(page).toHaveTitle('foo');
});

Remember, Playwright automatically waits for a wide range of actionability checks to pass before performing each action. So, you don't need to wait for anything before performing an action or deal with race conditions when performing checks.

Playwright assertions are designed to describe expectations that need to be eventually met. This design choice allows you to forget about flaky timeouts and racy checks in your tests. For more on handling flaky tests, check out this blog post.

Thank you!
Was this helpful?
Still have questions?

If you still have questions, please ask a question and I will try to answer it.

Related Questions

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.