diff --git a/docs/api/puppeteer.framewaitforfunctionoptions.md b/docs/api/puppeteer.framewaitforfunctionoptions.md index cba2ed79a02..7215d9ac080 100644 --- a/docs/api/puppeteer.framewaitforfunctionoptions.md +++ b/docs/api/puppeteer.framewaitforfunctionoptions.md @@ -15,4 +15,5 @@ export interface FrameWaitForFunctionOptions | Property | Modifiers | Type | Description | Default | | -------- | --------------------- | ----------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------- | | polling | optional | 'raf' \| 'mutation' \| number |

An interval at which the pageFunction is executed, defaults to raf. If polling is a number, then it is treated as an interval in milliseconds at which the function would be executed. If polling is a string, then it can be one of the following values:

- raf - to constantly execute pageFunction in requestAnimationFrame callback. This is the tightest polling mode which is suitable to observe styling changes.

- mutation - to execute pageFunction on every DOM mutation.

| | +| signal | optional | AbortSignal | A signal object that allows you to cancel a waitForFunction call. | | | timeout | optional | number | Maximum time to wait in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable the timeout. Puppeteer's default timeout can be changed using [Page.setDefaultTimeout()](./puppeteer.page.setdefaulttimeout.md). | | diff --git a/packages/puppeteer-core/src/common/Frame.ts b/packages/puppeteer-core/src/common/Frame.ts index 10a79c3d3a1..b605e60637c 100644 --- a/packages/puppeteer-core/src/common/Frame.ts +++ b/packages/puppeteer-core/src/common/Frame.ts @@ -64,6 +64,10 @@ export interface FrameWaitForFunctionOptions { * using {@link Page.setDefaultTimeout}. */ timeout?: number; + /** + * A signal object that allows you to cancel a waitForFunction call. + */ + signal?: AbortSignal; } /** diff --git a/test/src/waittask.spec.ts b/test/src/waittask.spec.ts index 7187b1dc481..eb65d66b87d 100644 --- a/test/src/waittask.spec.ts +++ b/test/src/waittask.spec.ts @@ -330,6 +330,22 @@ describe('waittask specs', function () { }); await watchdog; }); + it('should be cancellable', async () => { + const {page, server} = getTestState(); + + await page.goto(server.EMPTY_PAGE); + const abortController = new AbortController(); + const task = page.waitForFunction( + () => { + return (globalThis as any).__done; + }, + { + signal: abortController.signal, + } + ); + abortController.abort(); + await expect(task).rejects.toThrow(/aborted/); + }); }); describe('Page.waitForTimeout', () => {