import logger from '@wdio/logger' import { ScreencastRecorderBase, type ScreencastLogger, SCREENCAST_HANDSHAKE_TIMEOUT_MS, errorMessage, withTimeout } from '@wdio/devtools-core' import { BLANK_FRAME_THRESHOLD_BYTES } from './constants.js' import { getDriverOriginals } from './driverPatcher.js' import { cdpUnavailableReason } from './helpers/cdp-endpoint.js' import { makeCapGet } from './helpers/driverMetadata.js' import type { SeleniumDriverLike } from './types.js' const log = logger('@wdio/selenium-devtools:ScreencastRecorder') /** Selenium 4's CDP connection helper — shape stable across patch releases. * IMPORTANT: `execute(method, params)` in selenium-webdriver is fire-and- * forget — it writes to the underlying WebSocket and returns `undefined`, * not a Promise. Don't await it and don't call `.then`/`.catch` on the * return value. To know when Chrome actually starts pushing frames, gate * on the first `Page.screencastFrame` message instead. */ interface SeleniumCdpWebSocket { on(event: 'message', listener: (data: unknown) => void): void off?: (event: 'message', listener: (data: unknown) => void) => void close?: () => void } interface SeleniumCdpConnection { _wsConnection?: SeleniumCdpWebSocket execute(method: string, params?: Record): void } /** Max time to wait for Chrome's first screencast frame before declaring * recording active anyway. Most tests see the first frame in <100ms; the * ceiling protects against a totally unresponsive CDP target. */ const FIRST_FRAME_TIMEOUT_MS = 2000 /** * Selenium-specific screencast recorder. Inherits the frame buffer, polling * fallback, and public API from {@link ScreencastRecorderBase}; overrides the * CDP hooks to use selenium-webdriver's `createCDPConnection('page')` API and * listens directly on the underlying CDP WebSocket for `Page.screencastFrame`. */ export class ScreencastRecorder extends ScreencastRecorderBase { #cdp: SeleniumCdpConnection | undefined #cdpFrameListener: ((data: unknown) => void) | undefined /** Resolved by `#makeCdpFrameHandler` on the first arriving frame. Lets * `tryStartCdp` await actual readiness instead of returning eagerly. */ #firstFrameResolve: (() => void) | undefined protected override readonly logger: ScreencastLogger = log protected override async takeScreenshot(): Promise { const driver = this.driver const takeShot = getDriverOriginals().takeScreenshot if (!driver || !takeShot) { return null } return takeShot(driver) } #makeCdpFrameHandler(cdp: SeleniumCdpConnection): (raw: unknown) => void { return (raw: unknown) => { let parsed: { method?: string params?: { data?: string sessionId?: number metadata?: { timestamp?: number } } } try { parsed = JSON.parse(String(raw)) } catch { return // non-JSON message — ignore } if (parsed.method !== 'Page.screencastFrame') { return } const params = parsed.params ?? {} this.pushCdpFrame(params.data ?? '', params.metadata?.timestamp) // Anchor frame 0 at the first content-bearing frame to trim the // leading about:blank dead-air. Approximate decoded size: base64 // expands by ~33%, so multiply by 0.75 for a rough decoded byte count. if (!this.hasStartMarker) { const decodedSize = Math.floor((params.data?.length ?? 0) * 0.75) if (decodedSize >= BLANK_FRAME_THRESHOLD_BYTES) { this.markStartAtLatest() } } // Tell tryStartCdp that Chrome is actively pushing frames now. Cleared // after firing so subsequent frames don't reach for a missing resolver. if (this.#firstFrameResolve) { this.#firstFrameResolve() this.#firstFrameResolve = undefined } // Chrome throttles/stops sending frames if acks lag — keep this fire- // and-forget but DON'T treat the return as a Promise. selenium-webdriver's // CDPConnection.execute() returns `undefined` synchronously, so any // .then/.catch on it throws a TypeError (was crashing test runs). if (params.sessionId !== undefined) { try { cdp.execute('Page.screencastFrameAck', { sessionId: params.sessionId }) } catch (err) { log.warn(`Screencast: failed to ack frame — ${errorMessage(err)}`) } } } } /** * Open the CDP connection under the handshake ceiling. The base class * serialises start against stop, so a connection that never answers would * park teardown behind this handshake for good. */ async #openCdpConnection(): Promise { const driver = this.driver if (!driver || typeof driver.createCDPConnection !== 'function') { return undefined } // selenium-webdriver types createCDPConnection() as Promise; the // runtime shape is stable across patch releases and captured by // SeleniumCdpConnection above. const connection = driver.createCDPConnection( 'page' ) as Promise const cdp = await withTimeout( connection, SCREENCAST_HANDSHAKE_TIMEOUT_MS, undefined ) if (!cdp) { // A connection that lands after the ceiling belongs to nobody — close // its socket when it does, so it cannot outlive the recording. connection .then((late) => late?._wsConnection?.close?.()) .catch(() => undefined) } return cdp } /** Declines before calling: without an endpoint selenium-webdriver throws a TypeError rather than declining. */ async #cdpDeclineReason(): Promise { const driver = this.driver if (typeof driver?.getCapabilities !== 'function') { return undefined } // selenium-webdriver answers getCapabilities from the session it already holds, so no round trip. const capabilities = await withTimeout( driver.getCapabilities(), SCREENCAST_HANDSHAKE_TIMEOUT_MS, undefined ) return capabilities === undefined ? undefined : cdpUnavailableReason(makeCapGet(capabilities)) } protected override async tryStartCdp(): Promise { try { const declined = await this.#cdpDeclineReason() if (declined) { log.info(`${declined}; polling`) return false } const cdp = await this.#openCdpConnection() if (!cdp) { log.warn('CDP connection unavailable — falling back to polling') return false } this.#cdp = cdp const ws = cdp._wsConnection if (!ws || typeof ws.on !== 'function') { log.warn('CDP connection has no underlying WebSocket — falling back') return false } const onMessage = this.#makeCdpFrameHandler(cdp) this.#cdpFrameListener = onMessage ws.on('message', onMessage) // Arm the first-frame promise BEFORE firing Page.startScreencast so we // don't miss the race where Chrome pushes its first frame before we // start awaiting. const firstFrame = new Promise((resolve) => { this.#firstFrameResolve = resolve }) // cdp.execute is fire-and-forget (returns void) in selenium-webdriver // — see the SeleniumCdpConnection comment above. Wait on the actual // first-frame arrival instead: that's the unambiguous signal that // Chrome is pushing. Timeout-capped so an unresponsive target falls // through to the polling path rather than hanging the test. cdp.execute('Page.startScreencast', { format: this.options.captureFormat, quality: this.options.quality, maxWidth: this.options.maxWidth, maxHeight: this.options.maxHeight }) let timeoutHandle: ReturnType | undefined const timeout = new Promise((resolve) => { timeoutHandle = setTimeout(resolve, FIRST_FRAME_TIMEOUT_MS) }) await Promise.race([firstFrame, timeout]) if (timeoutHandle) { clearTimeout(timeoutHandle) } this.#firstFrameResolve = undefined log.info('✓ Screencast recording started (CDP mode)') return true } catch (err) { log.info( `CDP screencast unavailable (${errorMessage(err)}); will try polling` ) return false } } protected override async tryStopCdp(): Promise { try { // cdp.execute is fire-and-forget (returns void) — see the // SeleniumCdpConnection comment. The buffer is already populated // synchronously by the frame handler; we just need to stop new frames // arriving. Session/Target-closed throws here are expected during // driver.quit teardown. this.#cdp?.execute('Page.stopScreencast') } catch (err) { const msg = errorMessage(err) if ( msg.includes('Session closed') || msg.includes('Target closed') || msg.includes('no such session') ) { // expected during teardown } else { log.warn(`Screencast: error stopping CDP — ${msg}`) } } try { if (this.#cdpFrameListener && this.#cdp?._wsConnection?.off) { this.#cdp._wsConnection.off('message', this.#cdpFrameListener) } } catch { // detach best-effort } // Each createCDPConnection overwrites the driver's single slot, and quit // closes only the current one — without this, every recording rotation // on the same driver orphans this recording's socket for the session's // life. The buffer is final and the listener is gone, so nothing reads it. try { this.#cdp?._wsConnection?.close?.() } catch { // best-effort — the socket may already be gone } // If start was called but the first frame never arrived (timeout path), // the resolver is still set. Releasing it lets any pending Promise.race // unblock the test teardown cleanly. if (this.#firstFrameResolve) { this.#firstFrameResolve() this.#firstFrameResolve = undefined } log.info(`✓ Screencast stopped — ${this.buffer.length} frame(s) collected`) this.#cdp = undefined this.#cdpFrameListener = undefined } }