use waitForPageLoad inall examples

This commit is contained in:
Tommy D. Rossi
2026-01-25 13:48:04 +01:00
parent 28f5734b31
commit 26a353e3bc
5 changed files with 54 additions and 9 deletions
+1 -1
View File
@@ -37,7 +37,7 @@ console.log('url:', page.url()); console.log(await accessibilitySnapshot({ page
For visually complex pages (grids, galleries, dashboards), use `screenshotWithAccessibilityLabels({ page })` instead to understand spatial layout.
If nothing changed, try `await page.waitForLoadState('networkidle', {timeout: 3000})` or you may have clicked the wrong element.
If nothing changed, try `await waitForPageLoad({ page, timeout: 3000 })` or you may have clicked the wrong element.
## accessibility snapshots
+6 -5
View File
@@ -26,7 +26,7 @@ console.log('Heading text:', headingText)
await page.getByRole('button', { name: 'Submit Form' }).click()
console.log('Clicked submit button')
await page.waitForLoadState('networkidle')
await waitForPageLoad({ page })
console.log('Form submitted successfully')
```
@@ -74,8 +74,9 @@ await page.locator('//div[@class="content"]')
```javascript
await page.goto('https://example.com')
// Wait for network idle (no requests for 500ms)
await page.goto('https://example.com', { waitUntil: 'networkidle' })
// Wait for page load (smart detection that ignores analytics/ads)
await page.goto('https://example.com', { waitUntil: 'domcontentloaded' })
await waitForPageLoad({ page })
```
### Navigate Back/Forward
@@ -345,8 +346,8 @@ await page.waitForFunction(
// Wait for navigation
await page.waitForURL('**/success')
// Wait for page load
await page.waitForLoadState('networkidle')
// Wait for page load (smart detection that ignores analytics/ads)
await waitForPageLoad({ page })
// Wait for specific condition
await page.waitForFunction(
+19
View File
@@ -131,6 +131,25 @@ export async function waitForPageLoad(options: WaitForPageLoadOptions): Promise<
return result
}
// Fast path: check immediately first. If already ready, return without waiting.
try {
const firstCheck = await checkPageReady()
if (firstCheck.ready) {
return {
success: true,
readyState: firstCheck.readyState,
pendingRequests: [],
waitTimeMs: Date.now() - startTime,
timedOut: false,
}
}
lastReadyState = firstCheck.readyState
lastPendingRequests = firstCheck.pendingRequests
} catch (e) {
// First check failed, continue with polling
}
// Not ready yet - wait minWait to let JS settle and catch late-starting requests
await sleep(minWait)
while (Date.now() - startTime < timeout) {