Rayrun

How do I use Playwright to check if a webpage has a specific title?

Answer

Checking Page Title with Playwright

You can easily check if a page has a specific title using Playwright's expect function. Here's how you can do it:

First, navigate to the page using the page.goto() method.

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

Then, use expect(page).toHaveTitle('foo') to assert that the title of the page is "foo".

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

This assertion will automatically wait for the title to match "foo" before moving on with the test.

Playwright automatically waits for a wide range of actionability checks to pass prior to performing each action. This means there's no need to wait for anything prior to performing an action and there's no need to deal with race conditions when performing checks.

Here's a complete 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 example, we're testing whether or not the title of https://ray.run contains "Ray Run". The toHaveTitle() method takes in either a string or regular expression as its argument.

So, testing if a page has a specific title using Playwright is simple and straightforward. Just navigate to the desired URL and use expect(page).toHaveTitle() with your desired string or regular expression as an argument.

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.