* Resolves the URL to scope the instant navigation cookie to. Prefers * an explicit `baseURL` option, then falls back to the page's current URL. * Throws a descriptive error if neither is available (e.g. fresh page * before any navigation).
(
page: PlaywrightPage,
options?: { baseURL?: string }
)
| 103 | * before any navigation). |
| 104 | */ |
| 105 | function resolveURL( |
| 106 | page: PlaywrightPage, |
| 107 | options?: { baseURL?: string } |
| 108 | ): string { |
| 109 | const url = options?.baseURL ?? page.url() |
| 110 | if (url && url !== 'about:blank') { |
| 111 | return url |
| 112 | } |
| 113 | const error = new Error( |
| 114 | `Could not infer the base URL of the application. |
| 115 | |
| 116 | instant() needs to know the base URL so it can configure the |
| 117 | browser before the first page load. If the page is already |
| 118 | loaded, the base URL is detected automatically. |
| 119 | Otherwise, you can fix this in one of two ways: |
| 120 | |
| 121 | 1. Pass a baseURL option: |
| 122 | |
| 123 | await instant(page, async () => { |
| 124 | await page.goto('http://localhost:3000') |
| 125 | // ... |
| 126 | }, { baseURL: 'http://localhost:3000' }) |
| 127 | |
| 128 | Tip: If you use baseURL in your Playwright config, you can |
| 129 | get it from the test fixture: |
| 130 | |
| 131 | test('my test', async ({ page, baseURL }) => { |
| 132 | await instant(page, async () => { |
| 133 | // ... |
| 134 | }, { baseURL }) |
| 135 | }) |
| 136 | |
| 137 | 2. Navigate to a page before calling instant(): |
| 138 | |
| 139 | await page.goto('http://localhost:3000') |
| 140 | await instant(page, async () => { |
| 141 | // ... |
| 142 | })` |
| 143 | ) |
| 144 | // Remove resolveURL and instant from the stack trace so the error |
| 145 | // points at the caller's code. |
| 146 | Error.captureStackTrace(error, instant) |
| 147 | throw error |
| 148 | } |