Rayrun

How do I use Playwright to verify the title of a webpage?

Answer

Checking Page Title with Playwright

With Playwright, checking a page title is a breeze. You'll use the expect function and the toHaveTitle method.

First, navigate to the page:

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

Then, assert the title:

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

This will wait until the title matches 'foo' before proceeding.

Playwright's design eliminates the need for flaky timeouts and racy checks. It automatically waits for a range of actionability checks to pass before performing each action.

Here's a full test example:

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

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

In this test, we're checking if the title of https://ray.run contains "Ray Run". The toHaveTitle() method accepts a string or regular expression.

So, to check a page title with Playwright, just navigate to the URL and use expect(page).toHaveTitle() with your desired string or regex. It's that simple!

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.