Assert Page Title Playwright

Profile picture for user devraj

In Playwright, to verify the page title, you can use the toHaveTitle method of PageAssertions.

Table of Content

  1. Syntax
  2. Get title of the page
  3. Verify Page Title
  4. Verify Page Title using Regex
  5. Video Tutorial

Syntax

expect(page).toHaveTitle(titleOrRegExp[, options])
  • titleOrRegExp <string|RegExp>: Specify the expected title in string or regular expression form.
  • options? <Object>:
    • timeout? <number>: Time to retry the assertion for. Defaults to timeout in TestConfig.expect.
  • returns: <Promise<void>>

Get Page Title

To get the title of the page use below command

page.title();

Verify Page Title

The parameter is case-sensitive, and you need to specify the exact page title which is inside <title> tag in page source.

HTML

 <title>Online Technical Courses</title>

Code

test.only('verify page title', async ({page})=>{
    await page.goto('https://www.programsbuzz.com/');
    await expect(page).toHaveTitle("Online Technical Courses");
    })

Verify Page Title using Regex

You can use regular expressions in the method for partial match. The Below code will verify that the page title contains the word Online.

await expect(page).toHaveTitle(/Online/);

Video Tutorial: Verify Page Title in Playwright