Course contentsShow
← React
Lesson 2168 of 2,39648. End-to-End TestingFree lesson

Testing Navigation and Routing

Verify route changes, URL patterns, and navigation flows with page.url() and waitForURL() assertions.

Testing Navigation and Routing

You'll learn how to verify that your application navigates to the correct pages and updates URLs properly during end-to-end tests.

Why Navigation Testing Matters

When users click links or submit forms in your React app, they expect to land on the right page with the correct URL. In E2E tests, you need to verify this entire flow works—not just that a component renders, but that the browser actually navigates where it should.

Think of it like checking a GPS: you don't just verify the route was calculated, you confirm you actually arrived at the destination.

Verifying URL Changes

Playwright provides page.url() to check the current browser location:

test('navigates to profile page', async ({ page }) => {
  await page.goto('http://localhost:3000');
  
  await page.getByRole('link', { name: 'My Profile' }).click();
  
  // Verify the URL changed
  expect(page.url()).toBe('http://localhost:3000/profile');
});

Waiting for Navigation with waitForURL

Sometimes navigation takes a moment (redirects, API calls, etc.). Use waitForURL() to wait for the URL to match a pattern:

test('redirects after login', async ({ page }) => {
  await page.goto('http://localhost:3000/login');
  
  await page.getByLabel('Email').fill('user@example.com');
  await page.getByLabel('Password').fill('password123');
  await page.getByRole('button', { name: 'Sign In' }).click();
  
  // Wait for redirect to dashboard
  await page.waitForURL('**/dashboard');
  expect(page.url()).toContain('/dashboard');
});

You can use glob patterns (**/profile), regular expressions, or exact strings with waitForURL() to handle dynamic URLs.

Key Takeaway: Use page.url() for immediate URL checks and page.waitForURL() to wait for navigation to complete, ensuring your routing logic works correctly from the user's perspective.