Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothrwins@gmail.com
Vinoth Tech Solutions
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • E-Commerce Demo Application
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • E-Commerce Demo Application
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I

Playwright

  • Step-by-Step Node.js Installation on Windows
  • Step-by-Step VS Code Installation on Windows
  • Step-by-Step Playwright Installation on Windows
  • Playwright VS Code extension Installation and Execution
  • Playwright Folder Structure
  • Playwright Core Concepts
  • First Playwright Program
  • Validate Page Title and URL
  • Handle Browser Navigation
  • Playwright Built-in Locators
  • Locators in Playwright CSS Selector vs XPath
View Categories
  • Home
  • Tutorials
  • Playwright
  • Playwright
  • Validate Page Title and URL

Validate Page Title and URL

Validate Page Title and URL

In this example, we will learn how to validate a web page’s title and URL using Playwright assertions.

Code Example

📄
PageTitleURL.spec.ts
// Import Playwright test and expect assertion library
import { test, expect } from '@playwright/test';

// Define a test case with title
test('Validate Page Title and URL', async ({ page }) => {

  // Step 1: Navigate to the website
  await page.goto('https://vinothqaacademy.com/');

  // Step 2: Get the actual page title
  const actualTitle = await page.title();
  console.log('Page Title:', actualTitle); // For debugging

  // Step 3: Validate (assert) the page title
  await expect(page).toHaveTitle('Vinoth Tech Solutions – Empowering Tech Careers');

  // Negative scenario example (kept commented):
  // await expect(page).toHaveTitle('Vinoth Tech Solutions – Empowering Tech Career')

  // Step 4: Get the actual page URL
  const actualURL = page.url();
  console.log('Page URL:', actualURL); // For debugging

  // Step 5: Validate (assert) the page URL
  await expect(page).toHaveURL('https://vinothqaacademy.com/');
});

Explanation

– import { test, expect } from '@playwright/test' → imports Playwright’s built-in test runner and assertion library.

- test('Validate Page Title and URL', async ({ page }) => { ... }) → defines a test case with a descriptive title.

- page.goto(URL) → launches the browser and navigates to the given URL.

- page.title() → fetches the actual title of the loaded page.

– expect(page).toHaveTitle(expectedTitle) → asserts the title matches exactly.

– page.url() → fetches the actual URL of the page.

– expect(page).toHaveURL(expectedURL) → asserts the URL is correct.

– Commented negative test shows how an assertion failure would look.

Behind the Scenes

– Playwright automatically opens a new browser context for each test and closes it at the end.

– The expect assertion library is optimized with auto-waiting, so it retries until the title/URL matches or times out.

– console.log() is optional and mainly used for debugging values

What You’ll See

– Browser opens and navigates to vinothqaacademy.com.

– Console prints the page title and URL.

– Title and URL assertions pass successfully.

– Browser closes automatically.

Try It Yourself

– Change the URL to another site (e.g., https://playwright.dev/) and validate its title.

– Uncomment the negative scenario to see how assertion errors appear.

– Replace toHaveTitle with a regex for flexible validation:

– awaitexpect(page).toHaveTitle(/Vinoth Tech/);

Playwright Tutorials
What are your Feelings

Share This Article :

  • Facebook
  • X
  • LinkedIn
First Playwright ProgramHandle Browser Navigation
© 2018 – 2025 V-Tech Solutions Ltd (UK), Reg. No: 16489105