Package Exports
- graceful-playwright
- graceful-playwright/core.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (graceful-playwright) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
graceful-playwright
Gracefully handle timeout and network error with auto retry.
Features
auto retry when
page.goto()timeout or encounteredERR_NETWORK_CHANGEDauto restart page when
page.goto()crashed with/page crashed/ierrorhelper method to auto retry when failed with
The object has been collected to prevent unbounded heap growtherrorsupport restarting page from
BrowserorBrowserContextinstancesupport wrapping existing
Pageinstanceproxy frequently used methods
create
Pageinstance lazily (on-demand)
Installation
npm install graceful-playwrightYou can install the package with yarn, pnpm or slnpm as well.
Usage Example
More usage examples see: example.ts and core.spec.ts
import { GracefulPage } from 'graceful-playwright'
let browser = await chromium.launch()
let page = new GracefulPage({ from: browser })
let lines: string[] = await page.autoRetryWhenFailed(async () => {
await page.goto('http://example.net')
return await page.evaluate(() =>
Array.from(document.querySelectorAll('a'), a => a.href),
)
})
console.log('lines:', lines)
await page.close()
await browser.close()Typescript Signature
Main Class: GracefulPage
import { Browser, BrowserContext, Page, Response } from 'playwright'
export class GracefulPage {
constructor(
public options: {
from: Browser | BrowserContext
page?: Page | Promise<Page>
/**
* @default 5000 ms
*/
retryInterval?: number
/**
* @default error => console.error(error)
*/
onError?: (error: unknown) => void
},
)
fork(): GracefulPage
getPage(): Page | Promise<Page>
restart(options?: Parameters<Page['close']>[0]): Promise<Page>
/** @description optimized version of page.close() */
close: Page['close']
/**
* @description graceful version of page.goto()
* @throws GotoError with response details when got 429 Too Many Requests without retry-after header
*/
goto(
url: string,
/**
* @default { waitUtil: "domcontentloaded" }
*/
options?: Parameters<Page['goto']>[1],
): Promise<Response | null>
autoRetryWhenFailed<T>(f: () => T | Promise<T>): Promise<T>
/** @description proxy method to (await this.getPage())[method] */
evaluate: Page['evaluate']
waitForSelector: Page['waitForSelector']
fill: Page['fill']
click: Page['click']
content: Page['content']
title: Page['title']
innerHTML: Page['innerHTML']
innerText: Page['innerText']
}Error Class: GotoError
export class GotoError extends Error {
constructor(message: string, public details: GotoErrorDetails)
}
export type GotoErrorDetails = {
url: string
options?: Parameters<Page['goto']>[1]
response: Awaited<ReturnType<Page['goto']>>
}License
This project is licensed with BSD-2-Clause
This is free, libre, and open-source software. It comes down to four essential freedoms [ref]:
- The freedom to run the program as you wish, for any purpose
- The freedom to study how the program works, and change it so it does your computing as you wish
- The freedom to redistribute copies so you can help others
- The freedom to distribute copies of your modified versions to others