Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
feat(cdp): add timeout and signal options to CDPSession.send (#43404)
Yury Semikhatsky4aa442855c15
diff --git a/docs/src/api/class-cdpsession.md b/docs/src/api/class-cdpsession.mdindex f2e40f7e26..5359b3bc03 100644--- a/docs/src/api/class-cdpsession.md+++ b/docs/src/api/class-cdpsession.md@@ -140,6 +140,19 @@ Optional method parameters.Optional method parameters.+### option: CDPSession.send.timeout+* since: v1.65+- `timeout` <[float]>++Maximum time in milliseconds to wait for the response. Defaults to `0` - no timeout.++### option: CDPSession.send.signal+* since: v1.65+* langs: js+- `signal` <[AbortSignal]>++Allows to cancel the operation using an [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal). If the signal is aborted, the operation will be aborted and throw an error.+## method: CDPSession.event* since: v1.30* langs: csharpdiff --git a/packages/playwright-client/types/types.d.ts b/packages/playwright-client/types/types.d.tsindex 898bed827b..12bfbd2d86 100644--- a/packages/playwright-client/types/types.d.ts+++ b/packages/playwright-client/types/types.d.ts@@ -18416,10 +18416,12 @@ export interface CDPSession {/*** @param method Protocol method name.* @param params Optional method parameters.+ * @param options*/send<T extends keyof Protocol.CommandParameters>(method: T,- params?: Protocol.CommandParameters[T]+ params?: Protocol.CommandParameters[T],+ options?: { timeout?: number, signal?: AbortSignal }): Promise<Protocol.CommandReturnValues[T]>;/*** Emitted when the session is closed, either because the target was closed or `session.detach()` was called.diff --git a/packages/playwright-core/src/client/cdpSession.ts b/packages/playwright-core/src/client/cdpSession.tsindex cb861295b6..6ecb6b7502 100644--- a/packages/playwright-core/src/client/cdpSession.ts+++ b/packages/playwright-core/src/client/cdpSession.ts@@ -47,9 +47,10 @@ export class CDPSession extends ChannelOwner<channels.CDPSessionChannel> implemeasync send<T extends keyof Protocol.CommandParameters>(method: T,- params?: Protocol.CommandParameters[T]+ params?: Protocol.CommandParameters[T],+ options: { timeout?: number, signal?: AbortSignal } = {}): Promise<Protocol.CommandReturnValues[T]> {- const result = await this._channel.send({ method, params }, kNoTimeout);+ const result = await this._channel.send({ method, params }, { signal: options.signal, timeout: options.timeout ?? 0 });return result.result as Protocol.CommandReturnValues[T];}diff --git a/packages/playwright-core/types/types.d.ts b/packages/playwright-core/types/types.d.tsindex 898bed827b..12bfbd2d86 100644--- a/packages/playwright-core/types/types.d.ts+++ b/packages/playwright-core/types/types.d.ts@@ -18416,10 +18416,12 @@ export interface CDPSession {/*** @param method Protocol method name.* @param params Optional method parameters.+ * @param options*/send<T extends keyof Protocol.CommandParameters>(method: T,- params?: Protocol.CommandParameters[T]+ params?: Protocol.CommandParameters[T],+ options?: { timeout?: number, signal?: AbortSignal }): Promise<Protocol.CommandReturnValues[T]>;/*** Emitted when the session is closed, either because the target was closed or `session.detach()` was called.diff --git a/tests/library/chromium/session.spec.ts b/tests/library/chromium/session.spec.tsindex aaf3ff5869..c2c1726ea5 100644--- a/tests/library/chromium/session.spec.ts+++ b/tests/library/chromium/session.spec.ts@@ -29,6 +29,23 @@ it('should work', async function({ page }) {expect(foo).toBe('bar');});+it('should respect timeout and signal options', async function({ page, playwright }) {+ const client = await page.context().newCDPSession(page);+ const evalResponse = await client.send('Runtime.evaluate', { expression: '1 + 2', returnByValue: true }, { timeout: 5000 });+ expect(evalResponse.result.value).toBe(3);+ const error = await client.send('Runtime.evaluate', { expression: 'new Promise(() => {})', awaitPromise: true }, { timeout: 500 }).catch(e => e);+ expect(error).toBeInstanceOf(playwright.errors.TimeoutError);+ expect(error.message).toContain('Timeout 500ms exceeded');++ const controller = new AbortController();+ const promise = client.send('Runtime.evaluate', { expression: 'new Promise(() => {})', awaitPromise: true }, { signal: controller.signal }).catch(e => e);+ const reason = new Error('Aborted by user');+ controller.abort(reason);+ const abortError = await promise;+ expect(abortError.name).toBe('AbortError');+ expect(abortError.cause).toBe(reason);+});+it('should send events', async function({ page, server }) {const client = await page.context().newCDPSession(page);await client.send('Network.enable');diff --git a/utils/generate_types/overrides.d.ts b/utils/generate_types/overrides.d.tsindex 7f802f637a..31e2b6a616 100644--- a/utils/generate_types/overrides.d.ts+++ b/utils/generate_types/overrides.d.ts@@ -238,7 +238,8 @@ export interface CDPSession {once<T extends keyof Protocol.Events | symbol>(event: T, listener: (payload: T extends symbol ? any : Protocol.Events[T extends keyof Protocol.Events ? T : never]) => void): this;send<T extends keyof Protocol.CommandParameters>(method: T,- params?: Protocol.CommandParameters[T]+ params?: Protocol.CommandParameters[T],+ options?: { timeout?: number, signal?: AbortSignal }): Promise<Protocol.CommandReturnValues[T]>;}