|
| 1 | +# Playwright Advanced Navigation Guide |
| 2 | + |
| 3 | +This guide demonstrates advanced navigation scenarios in Playwright, including login-driven navigation, multi-tab handling, query params/fragments, error handling, and DOM content waits. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## 1. Login-Driven Navigation (Data-Driven) |
| 8 | +- Uses CSV data for login credentials |
| 9 | +- Navigates to dashboard on success, shows error on failure |
| 10 | + |
| 11 | +```js |
| 12 | +const path = require('path'); |
| 13 | +const DataProvider = require('../../utils/DataProvider'); |
| 14 | +const csvPath = path.join(__dirname, '../../testdata/login_data.csv'); |
| 15 | +const rows = DataProvider.fetchDataFromCsv(csvPath); |
| 16 | + |
| 17 | +test.describe('Login Navigation Scenario', () => { |
| 18 | + for (const { testName, username, password, expected } of rows) { |
| 19 | + test(`${testName} (${username})`, async ({ page }) => { |
| 20 | + await page.goto('/login'); |
| 21 | + await page.fill('#login-username', username); |
| 22 | + await page.fill('#login-password', password); |
| 23 | + await page.click('#login-submit'); |
| 24 | + if (expected === 'success') { |
| 25 | + await expect(page).toHaveURL(/dashboard/); |
| 26 | + } else { |
| 27 | + await expect(page.locator('#login-error')).toContainText('Invalid login credentials. Please check your username and password.'); |
| 28 | + } |
| 29 | + }); |
| 30 | + } |
| 31 | +}); |
| 32 | +``` |
| 33 | + |
| 34 | +--- |
| 35 | + |
| 36 | +## 2. Multiple Tabs/Windows |
| 37 | +```js |
| 38 | +const context = await browser.newContext(); |
| 39 | +const page1 = await context.newPage(); |
| 40 | +await page1.goto(DEMO_URL); |
| 41 | +const page2 = await context.newPage(); |
| 42 | +await page2.goto('https://playwright.dev'); |
| 43 | +await expect(page1).toHaveTitle(/TodoMVC/); |
| 44 | +await expect(page2).toHaveTitle(/Playwright/); |
| 45 | +``` |
| 46 | + |
| 47 | +--- |
| 48 | + |
| 49 | +## 3. Navigation with Query Params and Fragments |
| 50 | +```js |
| 51 | +await page.goto(DEMO_URL + '?foo=bar#section'); |
| 52 | +await expect(page).toHaveURL(/\?foo=bar#section/); |
| 53 | +``` |
| 54 | + |
| 55 | +--- |
| 56 | + |
| 57 | +## 4. Navigation Error Handling |
| 58 | +```js |
| 59 | +let error; |
| 60 | +try { |
| 61 | + await page.goto('https://nonexistent.playwright.dev'); |
| 62 | +} catch (e) { |
| 63 | + error = e; |
| 64 | +} |
| 65 | +expect(error).toBeDefined(); |
| 66 | +expect(error.message).toContain('ERR_NAME_NOT_RESOLVED'); |
| 67 | +``` |
| 68 | + |
| 69 | +--- |
| 70 | + |
| 71 | +## 5. Wait for Navigation and DOM Content Loaded |
| 72 | +```js |
| 73 | +await Promise.all([ |
| 74 | + page.waitForNavigation({ waitUntil: 'domcontentloaded' }), |
| 75 | + page.reload(), |
| 76 | +]); |
| 77 | +await expect(page).toHaveTitle(/TodoMVC/); |
| 78 | +``` |
| 79 | + |
| 80 | +--- |
| 81 | + |
| 82 | +## Best Practices |
| 83 | +- Use data-driven login for real-world navigation flows |
| 84 | +- Handle navigation errors gracefully |
| 85 | +- Use waitForNavigation for actions that trigger navigation |
| 86 | +- Manage multiple tabs/windows for complex scenarios |
| 87 | + |
| 88 | +--- |
| 89 | + |
| 90 | +**For more, see Playwright docs:** https://playwright.dev/docs/navigation |
0 commit comments