Before you check elements on a page, check the page itself. Did we land on the right URL? Does the page title match? These are the foundation assertions -- if the page is wrong, everything else is wrong too.
Verifies the current page URL. Accepts exact strings or regex patterns. Auto-retries -- so if a redirect takes a second, it waits.
import { test, expect } from '@playwright/test';
test('URL assertions after login', async ({ page }) => {
await page.goto('/banking');
await page.getByLabel('Username').fill('testuser');
await page.getByLabel('Password').fill('pass123');
await page.getByRole('button', { name: 'Login' }).click();
// Exact URL match
await expect(page).toHaveURL('https://www.testerrank.com/banking/dashboard');
// Regex match -- more practical, ignores domain
await expect(page).toHaveURL(/\/banking\/dashboard/);
// URL with query params
await expect(page).toHaveURL(/\/search\?q=laptop/);
// Just check URL contains something
await expect(page).toHaveURL(/dashboard/);
});Checks the page title -- the text in the browser tab. Also supports strings and regex.
import { test, expect } from '@playwright/test';
test('page title assertions', async ({ page }) => {
await page.goto('/banking');
// Exact title match
await expect(page).toHaveTitle('Banking Portal - Login');
// Regex -- partial match
await expect(page).toHaveTitle(/Banking/);
// Case-insensitive
await expect(page).toHaveTitle(/banking portal/i);
// After navigation
await page.getByRole('link', { name: 'Dashboard' }).click();
await expect(page).toHaveTitle(/Dashboard/);
});Start every test with a page-level check. It is like checking you are in the right building before looking for your office.
import { test, expect } from '@playwright/test';
test('verify banking dashboard loads correctly', async ({ page }) => {
await page.goto('/banking/dashboard');
// First: are we on the right page?
await expect(page).toHaveURL(/\/banking\/dashboard/);
await expect(page).toHaveTitle(/Dashboard/);
// Then: check elements on the page
await expect(page.getByRole('heading', { name: 'Dashboard' })).toBeVisible();
await expect(page.getByTestId('account-balance')).toBeVisible();
});Use regex for URL assertions in CI/CD. Your local URL is localhost:3000, staging is staging.app.com, production is app.com. Regex like /\/dashboard/ works everywhere. Hardcoded full URLs break across environments.
Navigate to the page
Assert URL matches expected pattern using toHaveURL
Assert page title is correct using toHaveTitle
Then proceed to check individual elements
Q: How do you verify that a page loaded correctly in Playwright?
A: I start with page-level assertions: expect(page).toHaveURL() to verify the URL is correct, and expect(page).toHaveTitle() to verify the page title. Both auto-retry, so if there is a redirect or the title updates dynamically, they handle it. I use regex patterns instead of exact strings so the tests work across environments -- localhost, staging, production. After confirming the page, I check key elements like the main heading and critical UI components using toBeVisible().
Key Point: Always start with toHaveURL and toHaveTitle to confirm you are on the right page. Use regex for environment-independent checks.