Playwright Navigation
Playwright Navigation
Navigation is how a test moves from page to page: by opening a URL, by clicking a link, or by going back in the history.
Every test starts on an empty page, so the first step is almost always page.goto().
Open a Page with goto()
page.goto() opens a URL and waits until the page has loaded.
page.url() returns the URL of the current page:
Example
tests/goto.spec.js
// @ts-check
import { test, expect } from '@playwright/test';
test('open a page', async ({ page }) => {
await page.goto('https://www.w3schools.com/playwright/practice/shop.html', {
waitUntil: 'domcontentloaded',
});
console.log('Now on:', page.url());
await expect(page.getByRole('heading', { name: 'Shop' })).toBeVisible();
});
Output:
Running 1 test using 1 worker
[1/1] [chromium] › tests/goto.spec.js:4:1 › open a page
[chromium] › tests/goto.spec.js:4:1 › open a page
Now on: https://www.w3schools.com/playwright/practice/shop.html
1 passed (1.5s)
The waitUntil option decides when goto() is done:
| Value | goto() returns when |
|---|---|
| 'load' | The load event fires: the page and its images, styles and scripts are loaded. This is the default |
| 'domcontentloaded' | The HTML is loaded and parsed, without waiting for images and styles |
| 'networkidle' | There has been no network traffic for 500 milliseconds. Not recommended |
| 'commit' | The server has answered and the browser starts to load the page |
In most tests, keep the default.
You do not need to wait for every request to finish: the assertions that follow wait for the content they check.
Note: In this chapter, the tests use full URLs.
In the Config File chapter you will learn to set a baseURL, so you can write page.goto('shop.html').
Links, Back and Forward
Click a link like any other element, and check where you landed with toHaveURL().
goBack(), goForward() and reload() work like the browser buttons:
Example
tests/links.spec.js
// @ts-check
import { test, expect } from '@playwright/test';
test('follow links and go back', async ({ page }) => {
await page.goto('https://www.w3schools.com/playwright/practice/index.html');
await page.getByRole('link', { name: 'Login page' }).click();
await expect(page).toHaveURL('https://www.w3schools.com/playwright/practice/login.html');
await page.getByRole('link', { name: 'Shop' }).click();
await expect(page).toHaveURL(/shop\.html$/);
await page.goBack();
await expect(page).toHaveURL(/login\.html$/);
await page.goForward();
await expect(page).toHaveURL(/shop\.html$/);
await page.reload();
await expect(page.getByRole('heading', { name: 'Shop' })).toBeVisible();
});
Output:
Running 1 test using 1 worker
[1/1] [chromium] › tests/links.spec.js:4:1 › follow links and go back
1 passed (4.0s)
Example Explained
toHaveURL()takes the full URL as a string, or a regular expression that matches a part of it/shop\.html$/matches any URL that ends withshop.html, and\.matches a real dottoHaveURL()retries, so it waits for the new page after the clickreload()loads the page again, so anything the page kept only in memory, like a cart, is gone
Tip: Prefer expect(page).toHaveURL() over expect(page.url()).toBe().
page.url() reads the URL once, maybe before the new page has opened.
Links That Open a New Tab
A link with target="_blank" opens a new tab.
The new tab is a new page in the same browser context.
The context is the browser profile your test runs in.
The practice navigation page has a link that opens a new tab, and a button that opens a popup window.
Start waiting for the new page before you click, then click, then get the page:
Example
tests/tabs.spec.js
// @ts-check
import { test, expect } from '@playwright/test';
test('link opens a new tab', async ({ page, context }) => {
await page.goto('https://www.w3schools.com/playwright/practice/navigation.html');
const pagePromise = context.waitForEvent('page');
await page.getByRole('link', { name: 'Open the shop in a new tab' }).click();
const newTab = await pagePromise;
await expect(newTab).toHaveTitle('Shop | Playwright Practice');
await expect(newTab.getByTestId('product')).toHaveCount(5);
console.log('Tabs open:', context.pages().length);
});
test('button opens a popup', async ({ page }) => {
await page.goto('https://www.w3schools.com/playwright/practice/navigation.html');
const popupPromise = page.waitForEvent('popup');
await page.getByRole('button', { name: 'Open help window' }).click();
const popup = await popupPromise;
await expect(popup.getByRole('heading', { name: 'Log in' })).toBeVisible();
console.log('Popup URL:', popup.url());
await popup.close();
});
Output:
Running 2 tests using 2 workers
[1/2] [chromium] › tests/tabs.spec.js:4:1 › link opens a new tab
[2/2] [chromium] › tests/tabs.spec.js:16:1 › button opens a popup
[chromium] › tests/tabs.spec.js:16:1 › button opens a popup
Popup URL: https://www.w3schools.com/playwright/practice/login.html
[chromium] › tests/tabs.spec.js:4:1 › link opens a new tab
Tabs open: 2
2 passed (1.9s)
Tabs and Popups Explained
- The first test asks for
contextnext topage, andcontext.waitForEvent('page')waits for the next new tab in that context page.waitForEvent('popup')waits for a window opened by this page, for example withwindow.open()or atarget="_blank"link- The new page works like the first one: you can use locators, actions and assertions on it
context.pages()returns all open pages: the first page and the new tab make 2popup.close()closes the popup, but Playwright closes all pages at the end of the test anyway
The order matters.
If you click first and start waiting after, the new page may already be open, and the wait never ends.
That is why the code saves the promise first (pagePromise), clicks, and only then uses await.