Assert Page Title Playwright

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

Table of Content

  1. Syntax
  2. Verify Page Title
  3. Verify Page Title using Regex
  4. 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>>

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/);
Tue, 10/04/2022 - 20:39
Tarun Goswami is a highly experienced professional, currently serving as Technical Lead at a well-established IT services company. With over 12 years of experience across multiple domains, he demonstrates his expertise and passion for the industry through his engaging and informative blog writing in his spare time.

Video Tutorial: Verify Page Title in Playwright

Tags

Comments