| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395 |
- import { captureBrowserScreenshot, observeBrowserDownload } from './browser-action-artifacts.js';
- import {
- buttonName,
- classifyBrowserNavigationEvent,
- waitExpression,
- } from './browser-action-helpers.js';
- import { BrowserActionTargetSupport } from './browser-action-targets.js';
- import { browserPrintableKeyDescriptor } from './browser-keyboard.js';
- import {
- asNumber,
- asString,
- cdp,
- delay,
- evaluate,
- resolveBrowserSnapshotElement,
- stringArray,
- type BrowserSessionState as SessionState,
- } from './browser-state.js';
- import type { CDPEditableState, CDPRenderedState } from './cdp-helper-contracts.js';
- import { isBrowserOperationInterruption } from './operation-timeout.js';
- const DEFAULT_WAIT_TIMEOUT_MS = 30_000;
- const ACTION_SETTLE_DELAY_MS = 100;
- const CLICK_NAVIGATION_DETECTION_MS = 1_000;
- const NAVIGATION_READY_TIMEOUT_MS = 30_000;
- const MAX_NAVIGATION_READY_TIMEOUT_MS = 120_000;
- const HISTORY_READY_TIMEOUT_MS = 10_000;
- const SCROLL_BOUNDARY_EPSILON = 1;
- const VERIFIED_FILL_SAMPLE_COUNT = 4;
- const VERIFIED_FILL_SAMPLE_DELAY_MS = 50;
- interface BrowserScrollSnapshot {
- ownerId: string;
- kind: 'document' | 'element' | 'unknown';
- scrollable: boolean;
- scrollTop: number;
- scrollLeft: number;
- scrollHeight: number;
- scrollWidth: number;
- clientHeight: number;
- clientWidth: number;
- }
- function normalizeScrollSnapshot(value: unknown, axis: 'x' | 'y'): BrowserScrollSnapshot | null {
- if (value === null || typeof value !== 'object' || Array.isArray(value)) return null;
- const candidate = value as Record<string, unknown>;
- const numericFields = [
- 'scrollTop',
- 'scrollLeft',
- 'scrollHeight',
- 'scrollWidth',
- 'clientHeight',
- 'clientWidth',
- ] as const;
- if (
- (candidate.kind === 'document' ||
- candidate.kind === 'element' ||
- candidate.kind === 'unknown') &&
- typeof candidate.ownerId === 'string' &&
- candidate.ownerId.length > 0 &&
- typeof candidate.scrollable === 'boolean' &&
- numericFields.every(
- (field) => typeof candidate[field] === 'number' && Number.isFinite(candidate[field]),
- )
- ) {
- return candidate as unknown as BrowserScrollSnapshot;
- }
- // Keep the original document-only response shape readable while providers
- // and focused test transports move to owner-aware snapshots.
- const legacyNumericFields = [
- 'x',
- 'y',
- 'scrollHeight',
- 'scrollWidth',
- 'clientHeight',
- 'clientWidth',
- ] as const;
- if (
- !legacyNumericFields.every(
- (field) => typeof candidate[field] === 'number' && Number.isFinite(candidate[field]),
- )
- ) {
- return null;
- }
- const scrollHeight = candidate.scrollHeight as number;
- const scrollWidth = candidate.scrollWidth as number;
- const clientHeight = candidate.clientHeight as number;
- const clientWidth = candidate.clientWidth as number;
- return {
- ownerId: 'document',
- kind: 'document',
- scrollable:
- axis === 'y'
- ? scrollHeight > clientHeight + SCROLL_BOUNDARY_EPSILON
- : scrollWidth > clientWidth + SCROLL_BOUNDARY_EPSILON,
- scrollTop: candidate.y as number,
- scrollLeft: candidate.x as number,
- scrollHeight,
- scrollWidth,
- clientHeight,
- clientWidth,
- };
- }
- interface DocumentReadiness {
- readonly url: string;
- readonly readyState: string;
- readonly timeOrigin: number;
- }
- interface NavigationHistoryEntry {
- readonly id: number;
- readonly url: string;
- }
- interface NavigationHistory {
- readonly currentIndex: number;
- readonly entries: readonly NavigationHistoryEntry[];
- }
- interface NavigationEventObservation {
- readonly method: 'Page.frameNavigated' | 'Page.navigatedWithinDocument';
- readonly params: unknown;
- readonly sessionId?: string;
- }
- function observeMainDocumentNavigation(
- session: SessionState,
- timeoutMs: number,
- mainFrameId?: string,
- ): Promise<NavigationEventObservation | undefined> {
- const childSessionIds = new Set(
- (session.transport.listAttachedFrames?.() ?? []).map((frame) => frame.sessionId),
- );
- return new Promise((resolve) => {
- let finished = false;
- let timer: ReturnType<typeof setTimeout> | undefined;
- let unsubscribe: (() => void) | undefined;
- const signal = session.commandSignal;
- const finish = (value: NavigationEventObservation | undefined): void => {
- if (finished) return;
- finished = true;
- if (timer) clearTimeout(timer);
- signal?.removeEventListener('abort', onAbort);
- unsubscribe?.();
- resolve(value);
- };
- const onAbort = (): void => finish(undefined);
- signal?.addEventListener('abort', onAbort, { once: true });
- if (signal?.aborted) {
- onAbort();
- return;
- }
- unsubscribe = session.transport.onEvent((event) => {
- if (
- event.method !== 'Page.frameNavigated' &&
- event.method !== 'Page.navigatedWithinDocument'
- ) {
- return;
- }
- if (event.sessionId && childSessionIds.has(event.sessionId)) return;
- const observation: NavigationEventObservation = {
- method: event.method,
- params: event.params,
- ...(event.sessionId ? { sessionId: event.sessionId } : {}),
- };
- if (classifyBrowserNavigationEvent(observation, session.transport, mainFrameId) !== 'main')
- return;
- finish(observation);
- });
- if (finished) {
- unsubscribe();
- return;
- }
- timer = setTimeout(() => finish(undefined), timeoutMs);
- timer.unref?.();
- });
- }
- export abstract class BrowserActionSupport extends BrowserActionTargetSupport {
- protected async navigate(
- session: SessionState,
- input: Record<string, unknown>,
- ): Promise<unknown> {
- const url = asString(input.url);
- if (!isSupportedBrowserUrl(url)) {
- throw new Error('Unsupported Browser URL: expected http(s), file, data, or about URL');
- }
- const startedAt = Date.now();
- const before = await this.readPageSummary(session);
- // Register the shared diagnostics listener before the first external
- // navigation. Enabling Runtime/Network at the transport layer does not
- // buffer events for a CDPHelper listener that has not been attached yet.
- await session.cdpHelper.startDOMListener(session.commandSignal);
- const beforeTimeOrigin = await this.evaluateMainDocument<number>(
- session,
- 'performance.timeOrigin',
- );
- const navigation = await this.sendMainDocumentCommand<{ errorText?: string }>(
- session,
- 'Page.navigate',
- { url },
- );
- if (navigation.errorText) throw new Error(`NAVIGATION_FAILED: ${navigation.errorText}`);
- await this.waitForDocumentReady(
- session,
- (state) => state.url !== before.url || state.timeOrigin !== beforeTimeOrigin,
- NAVIGATION_READY_TIMEOUT_MS,
- );
- await delay(ACTION_SETTLE_DELAY_MS, session.commandSignal);
- this.invalidate(session);
- const page = await this.readPageSummary(session);
- this.assertNotChromeErrorPage(page, `while navigating to ${url}`);
- return {
- success: true,
- url: page.url,
- title: page.title,
- durationMs: Date.now() - startedAt,
- navigation: {
- detected: true,
- urlChanged: true,
- loaded: true,
- generation: session.generation,
- },
- };
- }
- protected async history(session: SessionState, direction: 'back' | 'forward'): Promise<unknown> {
- const startedAt = Date.now();
- const before = await this.readPageSummary(session);
- const history = await this.readNavigationHistory(session);
- const targetIndex = history.currentIndex + (direction === 'back' ? -1 : 1);
- const target = history.entries[targetIndex];
- if (!target) {
- return {
- success: false,
- durationMs: Date.now() - startedAt,
- url: before.url,
- title: before.title,
- navigation: { detected: false, urlChanged: false, loaded: true },
- };
- }
- await this.sendMainDocumentCommand(session, 'Page.navigateToHistoryEntry', {
- entryId: target.id,
- });
- await this.waitForHistoryEntryReady(
- session,
- { index: targetIndex, entryId: target.id },
- HISTORY_READY_TIMEOUT_MS,
- );
- await delay(ACTION_SETTLE_DELAY_MS, session.commandSignal);
- this.invalidate(session);
- const after = await this.readPageSummary(session);
- this.assertNotChromeErrorPage(after, `while navigating ${direction}`);
- return {
- success: true,
- durationMs: Date.now() - startedAt,
- url: after.url,
- title: after.title,
- navigation: {
- detected: true,
- urlChanged: before.url !== after.url,
- loaded: true,
- generation: session.generation,
- },
- };
- }
- protected async reload(session: SessionState): Promise<unknown> {
- const beforeTimeOrigin = await this.evaluateMainDocument<number>(
- session,
- 'performance.timeOrigin',
- );
- await this.sendMainDocumentCommand(session, 'Page.reload', { ignoreCache: false });
- await this.waitForDocumentReady(
- session,
- (state) => state.timeOrigin !== beforeTimeOrigin,
- NAVIGATION_READY_TIMEOUT_MS,
- );
- await delay(ACTION_SETTLE_DELAY_MS, session.commandSignal);
- this.invalidate(session);
- const page = await this.readPageSummary(session);
- this.assertNotChromeErrorPage(page, 'while reloading the current page');
- return {
- success: true,
- url: page.url,
- title: page.title,
- navigation: { detected: true, loaded: true },
- };
- }
- protected async waitForDocumentReady(
- session: SessionState,
- acceptsUrl: (state: DocumentReadiness) => boolean,
- timeoutMs: number,
- ): Promise<DocumentReadiness> {
- const deadline = Date.now() + timeoutMs;
- while (Date.now() <= deadline) {
- try {
- const state = await this.evaluateMainDocument<DocumentReadiness>(
- session,
- '({ url: location.href, readyState: document.readyState, timeOrigin: performance.timeOrigin })',
- );
- if (
- acceptsUrl(state) &&
- (state.readyState === 'interactive' || state.readyState === 'complete')
- ) {
- return state;
- }
- } catch (error) {
- if (session.commandSignal?.aborted || isBrowserOperationInterruption(error)) throw error;
- // A navigation may briefly destroy the prior execution context.
- }
- await delay(50, session.commandSignal);
- }
- throw new Error(`ACTION_TIMEOUT: Browser navigation was not DOM-ready within ${timeoutMs}ms`);
- }
- protected assertNotChromeErrorPage(page: { readonly url: string }, context: string): void {
- if (!page.url.startsWith('chrome-error://')) return;
- throw new Error(`NAVIGATION_FAILED: Browser rendered an internal error page ${context}`);
- }
- protected evaluateMainDocument<T>(session: SessionState, expression: string): Promise<T> {
- return session.transport.evaluate<T>(expression, {
- ...(session.commandSignal ? { signal: session.commandSignal } : {}),
- ...(session.commandTimeoutMs ? { timeoutMs: session.commandTimeoutMs } : {}),
- });
- }
- private sendMainDocumentCommand<T = unknown>(
- session: SessionState,
- method: string,
- params?: Record<string, unknown>,
- ): Promise<T> {
- return session.transport.send<T>(method, params, undefined, {
- ...(session.commandSignal ? { signal: session.commandSignal } : {}),
- ...(session.commandTimeoutMs ? { timeoutMs: session.commandTimeoutMs } : {}),
- });
- }
- private async readMainFrameId(session: SessionState): Promise<string | undefined> {
- try {
- const result = await this.sendMainDocumentCommand<{
- frameTree?: { frame?: { id?: string } };
- }>(session, 'Page.getFrameTree');
- return typeof result.frameTree?.frame?.id === 'string'
- ? result.frameTree.frame.id
- : undefined;
- } catch (error) {
- if (isBrowserOperationInterruption(error)) throw error;
- return undefined;
- }
- }
- private readNavigationHistory(session: SessionState): Promise<NavigationHistory> {
- return this.sendMainDocumentCommand<NavigationHistory>(session, 'Page.getNavigationHistory');
- }
- private async waitForHistoryEntryReady(
- session: SessionState,
- target: { index: number; entryId: number },
- timeoutMs: number,
- ): Promise<DocumentReadiness> {
- const deadline = Date.now() + timeoutMs;
- while (Date.now() <= deadline) {
- try {
- const history = await this.readNavigationHistory(session);
- const currentEntry = history.entries[history.currentIndex];
- if (history.currentIndex === target.index && currentEntry?.id === target.entryId) {
- const state = await this.evaluateMainDocument<DocumentReadiness>(
- session,
- '({ url: location.href, readyState: document.readyState, timeOrigin: performance.timeOrigin })',
- );
- if (state.readyState === 'interactive' || state.readyState === 'complete') return state;
- }
- } catch (error) {
- if (session.commandSignal?.aborted || isBrowserOperationInterruption(error)) throw error;
- // Cross-document history traversal can briefly destroy the old
- // execution context. Keep polling the root page until the target
- // history entry and its document are both observable.
- }
- await delay(50, session.commandSignal);
- }
- throw new Error(`ACTION_TIMEOUT: Browser history was not DOM-ready within ${timeoutMs}ms`);
- }
- protected async click(session: SessionState, input: Record<string, unknown>): Promise<unknown> {
- const startedAt = Date.now();
- const before = await this.readPageSummary(session);
- const explicitNavigationTimeout = input.__navigationTimeoutMs;
- const navigationTimeoutMs =
- explicitNavigationTimeout === undefined
- ? (session.clickNavigationDetectionMs ?? CLICK_NAVIGATION_DETECTION_MS)
- : Math.max(
- CLICK_NAVIGATION_DETECTION_MS,
- Math.min(
- MAX_NAVIGATION_READY_TIMEOUT_MS,
- Math.floor(asNumber(explicitNavigationTimeout, CLICK_NAVIGATION_DETECTION_MS)),
- ),
- );
- const target = await this.resolveTarget(session, input);
- const coordinateTarget =
- input.position !== undefined || input.normalized_position !== undefined;
- const hitTarget = coordinateTarget
- ? await this.inspectCoordinateHitTarget(
- session,
- target.contextX ?? target.x,
- target.contextY ?? target.y,
- )
- : undefined;
- const clickCount = Math.max(1, Math.floor(asNumber(input.clickCount ?? input.click_count, 1)));
- await session.beforePointerAction?.(
- {
- action: clickCount > 1 ? 'double_click' : 'click',
- point: { x: target.x, y: target.y },
- },
- session.commandSignal,
- );
- const effectProbe = await this.startClickEffectProbe(
- session,
- target.contextX ?? target.x,
- target.contextY ?? target.y,
- );
- const downloadEvent = observeBrowserDownload(session, { signal: session.commandSignal });
- const mainFrameId = await this.readMainFrameId(session);
- const navigationEvent = observeMainDocumentNavigation(
- session,
- navigationTimeoutMs,
- mainFrameId,
- );
- await this.dispatchMouse(session, target.x, target.y, 'mouseMoved');
- const delayMs = Math.max(0, Math.floor(asNumber(input.delay, 50)));
- if (delayMs > 0) await delay(delayMs, session.commandSignal);
- for (let currentClick = 1; currentClick <= clickCount; currentClick += 1) {
- await this.dispatchMouse(session, target.x, target.y, 'mousePressed', {
- button: buttonName(input.button),
- clickCount: currentClick,
- });
- if (delayMs > 0) await delay(delayMs, session.commandSignal);
- await this.dispatchMouse(session, target.x, target.y, 'mouseReleased', {
- button: buttonName(input.button),
- clickCount: currentClick,
- });
- if (currentClick < clickCount && delayMs > 0) {
- await delay(delayMs, session.commandSignal);
- }
- }
- const navigationEventResult = await navigationEvent;
- await delay(ACTION_SETTLE_DELAY_MS, session.commandSignal);
- const download = await downloadEvent;
- // A navigation can replace the execution context before the first
- // post-click summary is available. The navigation event is the source of
- // truth for click-and-wait; keep ordinary click bounded and let its caller
- // perform the full ready-state wait.
- const after = await this.readPageSummary(session).catch((error) => {
- if (isBrowserOperationInterruption(error)) throw error;
- return before;
- });
- const effect = await this.finishClickEffectProbe(session, effectProbe).catch((error) => {
- if (isBrowserOperationInterruption(error)) throw error;
- return {
- observed: false,
- mutations: 0,
- focusChanged: false,
- targetStateChanged: false,
- };
- });
- const navigationDetected =
- classifyBrowserNavigationEvent(navigationEventResult, session.transport, mainFrameId) ===
- 'main' || before.url !== after.url;
- this.invalidateObservation(session);
- const result = {
- // Ordinary click success means that Browser input was dispatched. DOM
- // mutation/focus probes are diagnostics, not proof that the caller's
- // business goal completed. Main-document navigation remains the one
- // fail-closed exception for this non-navigation action.
- success: !navigationDetected,
- durationMs: Date.now() - startedAt,
- target: {
- resolved: true,
- rect: target.rect,
- ...(coordinateTarget
- ? {
- resolvedPosition: { x: target.x, y: target.y },
- ...(hitTarget ? { hitTarget, rect: hitTarget.rect } : {}),
- }
- : {}),
- },
- effect: {
- ...effect,
- dispatched: true,
- verified: false,
- verificationRequired: true,
- },
- navigation: {
- detected: navigationDetected,
- urlChanged: navigationDetected,
- generation: session.generation,
- },
- } as Record<string, unknown>;
- if (download) {
- result.download = download;
- if (download.state === 'canceled') {
- result.success = false;
- result.code = 'DOWNLOAD_CANCELED';
- result.error = 'Browser download was canceled before completion.';
- } else if (download.state !== 'completed') {
- // The click and download start were observed. Do not report an action
- // failure merely because a large/slow file outlived our observation
- // window; callers can inspect the explicit in-progress timeout state.
- result.success = true;
- result.download = { ...download, observation: 'timeout' };
- result.recovery = 'The download is still in progress; do not click again.';
- } else {
- result.success = true;
- }
- }
- if (navigationDetected) {
- result.success = false;
- result.code = 'UNEXPECTED_NAVIGATION';
- result.error = 'Ordinary click changed the page URL; inspect the new page before continuing.';
- result.recovery = 'Inspect the current page and use a newly observed ref.';
- }
- return result;
- }
- private async inspectCoordinateHitTarget(
- session: SessionState,
- x: number,
- y: number,
- ): Promise<
- | {
- tag: string;
- role: string;
- name: string;
- text: string;
- rect: { x: number; y: number; width: number; height: number };
- }
- | undefined
- > {
- return evaluate<
- | {
- tag: string;
- role: string;
- name: string;
- text: string;
- rect: { x: number; y: number; width: number; height: number };
- }
- | undefined
- >(
- session,
- `(() => {
- const point = { x: ${JSON.stringify(x)}, y: ${JSON.stringify(y)} };
- let element = document.elementFromPoint(point.x, point.y);
- while (element?.shadowRoot && typeof element.shadowRoot.elementFromPoint === 'function') {
- const deeper = element.shadowRoot.elementFromPoint(point.x, point.y);
- if (!deeper || deeper === element) break;
- element = deeper;
- }
- if (!(element instanceof Element)) return undefined;
- const rect = element.getBoundingClientRect();
- const text = String(element.textContent || '').replace(/\\s+/gu, ' ').trim();
- const name = element.getAttribute('aria-label') || element.getAttribute('title') || text;
- return {
- tag: String(element.tagName || '').toLowerCase().slice(0, 80),
- role: String(element.getAttribute('role') || '').slice(0, 80),
- name: String(name || '').slice(0, 300),
- text: text.slice(0, 300),
- rect: { x: rect.x, y: rect.y, width: rect.width, height: rect.height },
- };
- })()`,
- ).catch((error) => {
- if (isBrowserOperationInterruption(error)) throw error;
- return undefined;
- });
- }
- private async startClickEffectProbe(
- session: SessionState,
- x: number,
- y: number,
- ): Promise<{ key: string; before: string }> {
- const key = `__mavisClickProbe_${Date.now()}_${Math.random().toString(36).slice(2)}`;
- const before = await evaluate<string>(
- session,
- `(() => {
- const key = ${JSON.stringify(key)};
- const pointed = document.elementFromPoint(${x}, ${y});
- const state = (node) => node ? JSON.stringify({
- tag: node.tagName,
- id: node.id,
- value: 'value' in node ? String(node.value ?? '') : '',
- checked: 'checked' in node ? Boolean(node.checked) : undefined,
- pressed: node.getAttribute?.('aria-pressed'),
- expanded: node.getAttribute?.('aria-expanded'),
- selected: node.getAttribute?.('aria-selected'),
- disabled: node.getAttribute?.('aria-disabled'),
- className: typeof node.className === 'string' ? node.className : '',
- }) : '';
- const record = { count: 0, observer: null, beforeTarget: state(pointed), x: ${x}, y: ${y} };
- record.observer = new MutationObserver((entries) => { record.count += entries.length; });
- record.observer.observe(document, { subtree: true, childList: true, attributes: true, characterData: true });
- window[key] = record;
- return JSON.stringify({ active: state(document.activeElement), target: record.beforeTarget });
- })()`,
- );
- return { key, before };
- }
- private async finishClickEffectProbe(
- session: SessionState,
- probe: { key: string; before: string },
- ): Promise<{
- observed: boolean;
- mutations: number;
- focusChanged: boolean;
- targetStateChanged: boolean;
- }> {
- return evaluate(
- session,
- `(() => {
- const key = ${JSON.stringify(probe.key)};
- const record = window[key];
- if (!record) return { observed: false, mutations: 0, focusChanged: false, targetStateChanged: false };
- record.observer?.disconnect();
- const pointed = document.elementFromPoint(record.x, record.y);
- const state = (node) => node ? JSON.stringify({
- tag: node.tagName,
- id: node.id,
- value: 'value' in node ? String(node.value ?? '') : '',
- checked: 'checked' in node ? Boolean(node.checked) : undefined,
- pressed: node.getAttribute?.('aria-pressed'),
- expanded: node.getAttribute?.('aria-expanded'),
- selected: node.getAttribute?.('aria-selected'),
- disabled: node.getAttribute?.('aria-disabled'),
- className: typeof node.className === 'string' ? node.className : '',
- }) : '';
- const before = JSON.parse(${JSON.stringify(probe.before)});
- const active = state(document.activeElement);
- const target = state(pointed);
- const focusChanged = before.active !== active;
- const targetStateChanged = before.target !== target;
- const mutations = Number(record.count || 0);
- delete window[key];
- return { observed: mutations > 0 || focusChanged || targetStateChanged, mutations, focusChanged, targetStateChanged };
- })()`,
- );
- }
- protected async type(session: SessionState, input: Record<string, unknown>): Promise<unknown> {
- const startedAt = Date.now();
- const beforeGeneration = session.generation;
- const target = await this.resolveTarget(session, input, true, false);
- const text = asString(input.text);
- const delayMs = Math.max(0, Math.floor(asNumber(input.delay, 0)));
- const unexpectedNavigation = (dispatched: boolean): Record<string, unknown> => ({
- success: false,
- code: 'UNEXPECTED_NAVIGATION',
- error: 'Typing changed the active Browser page unexpectedly.',
- retryable: true,
- recovery: 'Inspect the current page before deciding whether to retry.',
- durationMs: Date.now() - startedAt,
- target: { resolved: true, rect: target.rect },
- effect: {
- dispatched,
- verified: false,
- verificationRequired: true,
- expectedTextLength: text.length,
- },
- navigation: { detected: true, generation: session.generation },
- });
- let inputDispatched = false;
- if (session.generation !== beforeGeneration) return unexpectedNavigation(false);
- if (target.backendNodeId === undefined) {
- const normalizedCoordinateTarget =
- input.normalized_position !== undefined &&
- input.position === undefined &&
- !asString(input.ref) &&
- !asString(input.selector);
- if (!normalizedCoordinateTarget) throw new Error('TYPE_REQUIRES_ELEMENT_REF');
- await session.beforePointerAction?.(
- { action: 'type', point: { x: target.x, y: target.y } },
- session.commandSignal,
- );
- await this.dispatchMouse(session, target.x, target.y, 'mouseMoved');
- inputDispatched = true;
- if (session.generation !== beforeGeneration) return unexpectedNavigation(inputDispatched);
- await delay(50, session.commandSignal);
- if (session.generation !== beforeGeneration) return unexpectedNavigation(inputDispatched);
- const clickCount = input.clear === true ? 3 : 1;
- for (let currentClick = 1; currentClick <= clickCount; currentClick += 1) {
- await this.dispatchMouse(session, target.x, target.y, 'mousePressed', {
- button: 'left',
- clickCount: currentClick,
- });
- inputDispatched = true;
- await delay(50, session.commandSignal);
- await this.dispatchMouse(session, target.x, target.y, 'mouseReleased', {
- button: 'left',
- clickCount: currentClick,
- });
- if (session.generation !== beforeGeneration) return unexpectedNavigation(inputDispatched);
- if (currentClick < clickCount) {
- await delay(50, session.commandSignal);
- if (session.generation !== beforeGeneration) {
- return unexpectedNavigation(inputDispatched);
- }
- }
- }
- await delay(ACTION_SETTLE_DELAY_MS, session.commandSignal);
- if (session.generation !== beforeGeneration) return unexpectedNavigation(inputDispatched);
- inputDispatched =
- (await this.insertText(session, text, delayMs, beforeGeneration)) || inputDispatched;
- if (session.generation !== beforeGeneration) return unexpectedNavigation(inputDispatched);
- await delay(ACTION_SETTLE_DELAY_MS, session.commandSignal);
- if (session.generation !== beforeGeneration) return unexpectedNavigation(inputDispatched);
- this.invalidateObservation(session);
- return {
- success: true,
- durationMs: Date.now() - startedAt,
- target: {
- resolved: true,
- rect: target.rect,
- resolvedPosition: { x: target.x, y: target.y },
- },
- effect: {
- dispatched: true,
- verified: false,
- verificationRequired: true,
- expectedTextLength: text.length,
- },
- };
- }
- const backendNodeId = target.backendNodeId;
- const ref = asString(input.ref);
- const observedElement = ref
- ? (resolveBrowserSnapshotElement(session, ref)?.element ??
- session.elementMap.getElementByOpaqueRef(ref))
- : null;
- const inspectionOptions = {
- ...(session.commandSignal ? { signal: session.commandSignal } : {}),
- ...(session.commandTimeoutMs ? { timeoutMs: session.commandTimeoutMs } : {}),
- ...(session.commandSessionId ? { commandSessionId: session.commandSessionId } : {}),
- };
- const inspectTextState = (
- expectedText?: string,
- ): Promise<CDPEditableState | CDPRenderedState> =>
- target.virtualEditor === true
- ? session.cdpHelper.inspectRenderedStateByBackendNodeId(
- backendNodeId,
- expectedText,
- inspectionOptions,
- )
- : session.cdpHelper.inspectEditableStateByBackendNodeId(
- backendNodeId,
- expectedText,
- inspectionOptions,
- );
- let before: CDPEditableState | CDPRenderedState;
- try {
- before = await inspectTextState();
- } catch (error) {
- if (isBrowserOperationInterruption(error)) throw error;
- if (session.generation !== beforeGeneration) return unexpectedNavigation(false);
- throw error;
- }
- if (session.generation !== beforeGeneration) return unexpectedNavigation(false);
- const targetMetadata = {
- resolved: true,
- tag: 'tag' in before ? before.tag : (observedElement?.tag ?? ''),
- role: 'role' in before ? before.role : (observedElement?.role ?? ''),
- contentEditable:
- 'contentEditable' in before
- ? before.contentEditable
- : observedElement?.attributes.contenteditable === 'true',
- ...(target.virtualEditor === true ? { virtual: true } : {}),
- rect: target.rect,
- };
- if (target.virtualEditor !== true && 'editable' in before && !before.editable) {
- return {
- success: false,
- code: 'TARGET_NOT_EDITABLE',
- error: 'The resolved Browser target is not editable.',
- message: 'The resolved Browser target is not editable.',
- retryable: false,
- recovery: 'Inspect the page and choose an input, textarea, or contenteditable ref.',
- durationMs: Date.now() - startedAt,
- target: targetMetadata,
- effect: { dispatched: false, verified: false, verificationRequired: true },
- navigation: { detected: false, generation: session.generation },
- };
- }
- if (target.coordinateSpace !== 'unavailable') {
- await session.beforePointerAction?.(
- {
- action: 'type',
- point: { x: target.x, y: target.y },
- },
- session.commandSignal,
- );
- }
- if (target.virtualEditor === true) {
- if (target.coordinateSpace === 'unavailable') {
- throw new Error(
- 'FRAME_COORDINATES_UNAVAILABLE: virtual editor activation requires safe viewport coordinates',
- );
- }
- inputDispatched = true;
- try {
- await this.activateVirtualEditor(session, {
- x: target.x,
- y: target.y,
- backendNodeId: target.backendNodeId,
- });
- } catch (error) {
- if (isBrowserOperationInterruption(error)) throw error;
- if (session.generation !== beforeGeneration) {
- return unexpectedNavigation(inputDispatched);
- }
- throw error;
- }
- } else {
- await cdp(session, 'DOM.focus', { backendNodeId: target.backendNodeId });
- }
- if (session.generation !== beforeGeneration) return unexpectedNavigation(inputDispatched);
- if (input.clear === true) {
- inputDispatched = true;
- await session.cdpHelper.selectAllAtCurrentFocus({
- ...(session.commandSignal ? { signal: session.commandSignal } : {}),
- ...(session.commandTimeoutMs ? { timeoutMs: session.commandTimeoutMs } : {}),
- });
- if (session.generation !== beforeGeneration) return unexpectedNavigation(inputDispatched);
- await this.dispatchKey(session, 'Backspace', []);
- if (session.generation !== beforeGeneration) return unexpectedNavigation(inputDispatched);
- }
- if (target.virtualEditor === true) {
- for (const char of text) {
- if (session.generation !== beforeGeneration) return unexpectedNavigation(inputDispatched);
- inputDispatched = true;
- await this.dispatchVirtualEditorCharacter(session, char);
- if (session.generation !== beforeGeneration) return unexpectedNavigation(inputDispatched);
- if (delayMs > 0) {
- await delay(delayMs, session.commandSignal);
- if (session.generation !== beforeGeneration) {
- return unexpectedNavigation(inputDispatched);
- }
- }
- }
- } else {
- inputDispatched =
- (await this.insertText(session, text, delayMs, beforeGeneration)) || inputDispatched;
- if (session.generation !== beforeGeneration) return unexpectedNavigation(inputDispatched);
- }
- await delay(ACTION_SETTLE_DELAY_MS, session.commandSignal);
- const clear = input.clear === true;
- const samples: Array<CDPEditableState | CDPRenderedState> = [];
- try {
- const sampleCount = clear ? VERIFIED_FILL_SAMPLE_COUNT : 1;
- for (let sampleIndex = 0; sampleIndex < sampleCount; sampleIndex += 1) {
- if (sampleIndex > 0) {
- await delay(VERIFIED_FILL_SAMPLE_DELAY_MS, session.commandSignal);
- if (session.generation !== beforeGeneration) {
- return unexpectedNavigation(inputDispatched);
- }
- }
- samples.push(await inspectTextState(text));
- }
- } catch (error) {
- if (isBrowserOperationInterruption(error)) throw error;
- if (session.generation !== beforeGeneration) return unexpectedNavigation(inputDispatched);
- throw error;
- }
- if (session.generation !== beforeGeneration) return unexpectedNavigation(inputDispatched);
- const after = samples[samples.length - 1];
- const previous = samples[samples.length - 2];
- if (!after) throw new Error('Browser input verification did not produce a final sample.');
- const changed = after.fingerprint !== before.fingerprint;
- const matchesExpected = clear
- ? previous?.matchesExpected === true &&
- after.matchesExpected === true &&
- previous.fingerprint === after.fingerprint
- : undefined;
- const containsExpected = clear
- ? undefined
- : text.length === 0 || (changed && after.containsExpected === true);
- let focused = 'focused' in after ? after.focused === true : false;
- if (target.virtualEditor === true) {
- const focusedState = await session.cdpHelper.inspectFocusedEditableStateByBackendNodeId(
- target.backendNodeId,
- inspectionOptions,
- );
- if (session.generation !== beforeGeneration) return unexpectedNavigation(inputDispatched);
- focused =
- focusedState.editable === true &&
- focusedState.focused === true &&
- focusedState.withinTarget === true;
- }
- const verified =
- target.virtualEditor === true && !focused
- ? false
- : clear
- ? matchesExpected === true
- : containsExpected === true;
- const continuationRef = asString(input.ref);
- this.invalidateObservation(session);
- if (continuationRef) {
- session.keyboardContinuationTarget = {
- ref: continuationRef,
- backendNodeId: target.backendNodeId,
- ...(target.rect ? { rect: target.rect } : {}),
- ...(observedElement?.frameId ? { frameId: observedElement.frameId } : {}),
- ...(session.commandSessionId ? { commandSessionId: session.commandSessionId } : {}),
- };
- }
- const result = {
- success: verified,
- durationMs: Date.now() - startedAt,
- target: targetMetadata,
- effect: {
- dispatched: true,
- verified,
- verificationRequired: true,
- focused,
- expectedTextLength: text.length,
- textLength: after.textLength,
- textChanged: changed,
- ...(clear ? { matchesExpected } : {}),
- ...(!clear ? { containsExpected } : {}),
- },
- navigation: { detected: false, generation: session.generation },
- } as Record<string, unknown>;
- if (!verified) {
- const virtualEditor = target.virtualEditor === true;
- const code =
- virtualEditor && !focused
- ? 'INPUT_FOCUS_REJECTED'
- : clear
- ? virtualEditor
- ? changed
- ? 'INPUT_PARTIAL_EFFECT'
- : 'INPUT_EVENT_NOT_CONSUMED'
- : 'ACTION_EFFECT_MISMATCH'
- : changed
- ? 'INPUT_PARTIAL_EFFECT'
- : virtualEditor
- ? 'INPUT_EVENT_NOT_CONSUMED'
- : 'ACTION_EFFECT_NOT_OBSERVED';
- const error =
- code === 'INPUT_FOCUS_REJECTED'
- ? 'The visible editor host no longer owns the current editable focus.'
- : clear
- ? 'The editable target does not contain the exact requested value after filling.'
- : changed
- ? 'The editable target does not contain the complete requested text after typing.'
- : 'The editable target did not change after typing.';
- result.code = code;
- result.error = error;
- result.message = error;
- result.retryable = true;
- result.recovery =
- code === 'INPUT_FOCUS_REJECTED'
- ? 'Inspect the page again and choose the current visible editor ref.'
- : 'Inspect the page and retry with the current editable ref.';
- }
- return result;
- }
- private async insertText(
- session: SessionState,
- text: string,
- delayMs: number,
- expectedGeneration: number,
- ): Promise<boolean> {
- let dispatched = false;
- if (session.generation !== expectedGeneration) return dispatched;
- if (delayMs > 0) {
- for (const char of text) {
- if (session.generation !== expectedGeneration) return dispatched;
- await cdp(session, 'Input.insertText', { text: char });
- dispatched = true;
- if (session.generation !== expectedGeneration) return dispatched;
- await delay(delayMs, session.commandSignal);
- if (session.generation !== expectedGeneration) return dispatched;
- }
- return dispatched;
- }
- await cdp(session, 'Input.insertText', { text });
- return true;
- }
- private async dispatchVirtualEditorCharacter(session: SessionState, char: string): Promise<void> {
- const key = browserPrintableKeyDescriptor(char);
- if (!key) {
- await cdp(session, 'Input.insertText', { text: char });
- return;
- }
- await cdp(session, 'Input.dispatchKeyEvent', {
- type: 'keyDown',
- key: key.key,
- code: key.code,
- windowsVirtualKeyCode: key.windowsVirtualKeyCode,
- modifiers: key.modifiers,
- });
- await cdp(session, 'Input.dispatchKeyEvent', {
- type: 'char',
- key: key.key,
- text: char,
- unmodifiedText: char,
- });
- await cdp(session, 'Input.dispatchKeyEvent', {
- type: 'keyUp',
- key: key.key,
- code: key.code,
- windowsVirtualKeyCode: key.windowsVirtualKeyCode,
- modifiers: key.modifiers,
- });
- }
- private async activateVirtualEditor(
- session: SessionState,
- target: {
- x: number;
- y: number;
- backendNodeId: number;
- },
- ): Promise<void> {
- await this.dispatchMouse(session, target.x, target.y, 'mouseMoved');
- await delay(50, session.commandSignal);
- await this.dispatchMouse(session, target.x, target.y, 'mousePressed', {
- button: 'left',
- clickCount: 1,
- });
- await delay(50, session.commandSignal);
- await this.dispatchMouse(session, target.x, target.y, 'mouseReleased', {
- button: 'left',
- clickCount: 1,
- });
- await delay(50, session.commandSignal);
- const resolved = await cdp<{ object?: { objectId?: string } }>(session, 'DOM.resolveNode', {
- backendNodeId: target.backendNodeId,
- });
- const objectId = resolved.object?.objectId;
- if (!objectId) throw new Error('INPUT_FOCUS_REJECTED: editor host was not resolved');
- try {
- const focused = await cdp<{
- result?: { value?: { focused?: boolean; editable?: boolean; withinTarget?: boolean } };
- exceptionDetails?: { text?: string };
- }>(session, 'Runtime.callFunctionOn', {
- objectId,
- functionDeclaration: `function () {
- let active = this.ownerDocument?.activeElement || null;
- while (active?.shadowRoot?.activeElement) active = active.shadowRoot.activeElement;
- const tag = String(active?.tagName || '').toLowerCase();
- const role = String(active?.getAttribute?.('role') || '').toLowerCase();
- const inputType = active instanceof HTMLInputElement
- ? String(active.type || '').toLowerCase()
- : '';
- const editableInput = active instanceof HTMLInputElement && ![
- 'button', 'checkbox', 'file', 'hidden', 'image', 'radio', 'reset', 'submit'
- ].includes(inputType);
- const editable = editableInput || active instanceof HTMLTextAreaElement ||
- active?.isContentEditable === true || role === 'textbox' || role === 'searchbox';
- let withinTarget = false;
- let cursor = active;
- while (cursor) {
- if (cursor === this) {
- withinTarget = true;
- break;
- }
- cursor = cursor.parentNode || cursor.getRootNode?.()?.host || null;
- }
- return { focused: Boolean(active), editable, withinTarget };
- }`,
- returnByValue: true,
- awaitPromise: true,
- });
- if (focused.exceptionDetails) {
- throw new Error(focused.exceptionDetails.text || 'INPUT_FOCUS_REJECTED');
- }
- const state = focused.result?.value;
- if (!state?.focused || !state.editable || !state.withinTarget) {
- throw new Error(
- 'INPUT_FOCUS_REJECTED: activating the editor host did not focus its editable proxy',
- );
- }
- } finally {
- await cdp(session, 'Runtime.releaseObject', { objectId }).catch(() => undefined);
- }
- }
- protected async pressKey(
- session: SessionState,
- input: Record<string, unknown>,
- ): Promise<unknown> {
- const startedAt = Date.now();
- const ref = asString(input.ref);
- const hasTarget = Boolean(ref || asString(input.selector));
- const continuation =
- ref &&
- !session.elementMap.hasOpaqueRef(ref) &&
- session.keyboardContinuationTarget?.ref === ref
- ? session.keyboardContinuationTarget
- : undefined;
- session.keyboardContinuationTarget = undefined;
- const target =
- continuation ??
- (hasTarget ? await this.resolveTarget(session, input, true, false) : undefined);
- if (target && target.backendNodeId === undefined) {
- throw new Error('PRESS_KEY_TARGET_NOT_RESOLVED');
- }
- if (target?.backendNodeId !== undefined) {
- session.commandSessionId = target.commandSessionId;
- await cdp(session, 'DOM.focus', { backendNodeId: target.backendNodeId });
- }
- await this.dispatchKey(session, asString(input.key), stringArray(input.modifiers));
- await delay(ACTION_SETTLE_DELAY_MS, session.commandSignal);
- this.invalidateObservation(session);
- return {
- success: true,
- durationMs: Date.now() - startedAt,
- ...(target ? { target: { resolved: true, rect: target.rect } } : {}),
- effect: { dispatched: true },
- };
- }
- private async inspectScrollSnapshot(
- session: SessionState,
- point: { x: number; y: number } | null,
- axis: 'x' | 'y',
- ): Promise<BrowserScrollSnapshot | null> {
- const command = { point, axis };
- const raw = await evaluate<unknown>(
- session,
- `(() => {
- const command = ${JSON.stringify(command)};
- const documentScroller = document.scrollingElement || document.documentElement;
- const ownerRegistryKey = Symbol.for('mavis.browser.scroll-owner-registry');
- const ownerRegistry = globalThis[ownerRegistryKey] ||
- (globalThis[ownerRegistryKey] = { nextId: 1, ids: new WeakMap() });
- const ownerId = (element) => {
- if (element === documentScroller) return 'document';
- let id = ownerRegistry.ids.get(element);
- if (!id) {
- id = 'element:' + ownerRegistry.nextId++;
- ownerRegistry.ids.set(element, id);
- }
- return id;
- };
- const isScrollable = (element) => {
- if (!(element instanceof Element)) return false;
- const style = getComputedStyle(element);
- const overflow = command.axis === 'y' ? style.overflowY : style.overflowX;
- const allowsUserScroll = overflow === 'auto' || overflow === 'scroll' || overflow === 'overlay';
- const hasOverflow = command.axis === 'y'
- ? element.scrollHeight > element.clientHeight + ${SCROLL_BOUNDARY_EPSILON}
- : element.scrollWidth > element.clientWidth + ${SCROLL_BOUNDARY_EPSILON};
- return allowsUserScroll && hasOverflow;
- };
- const resolveScrollTarget = (point) => {
- let element = point ? document.elementFromPoint(point.x, point.y) : documentScroller;
- if (element instanceof HTMLIFrameElement) return null;
- if (point) {
- while (element && element !== documentScroller && !isScrollable(element)) {
- element = element.parentElement;
- }
- }
- if (!element || (element !== documentScroller && !isScrollable(element))) {
- element = documentScroller;
- }
- return element || null;
- };
- const element = resolveScrollTarget(command.point);
- if (!element) {
- return {
- ownerId: 'unknown',
- kind: 'unknown',
- scrollable: false,
- scrollTop: 0,
- scrollLeft: 0,
- scrollHeight: 0,
- scrollWidth: 0,
- clientHeight: 0,
- clientWidth: 0,
- };
- }
- const hasScrollableExtent = command.axis === 'y'
- ? element.scrollHeight > element.clientHeight + ${SCROLL_BOUNDARY_EPSILON}
- : element.scrollWidth > element.clientWidth + ${SCROLL_BOUNDARY_EPSILON};
- return {
- ownerId: ownerId(element),
- kind: element === documentScroller ? 'document' : 'element',
- scrollable: element === documentScroller ? hasScrollableExtent : isScrollable(element),
- scrollTop: Number(element.scrollTop || 0),
- scrollLeft: Number(element.scrollLeft || 0),
- scrollHeight: Number(element.scrollHeight || 0),
- scrollWidth: Number(element.scrollWidth || 0),
- clientHeight: Number(element.clientHeight || 0),
- clientWidth: Number(element.clientWidth || 0),
- };
- })()`,
- );
- return normalizeScrollSnapshot(raw, axis);
- }
- protected async scroll(session: SessionState, input: Record<string, unknown>): Promise<unknown> {
- const direction = asString(input.direction, 'down');
- const axis = direction === 'left' || direction === 'right' ? 'x' : 'y';
- const viewport = await this.readCssViewport(session);
- const distance = Math.max(1, asNumber(input.distance, Math.round(viewport.height * 0.8)));
- const deltaX = direction === 'left' ? -distance : direction === 'right' ? distance : 0;
- const deltaY = direction === 'up' ? -distance : direction === 'down' ? distance : 0;
- const hasExplicitTarget = ['ref', 'selector', 'position', 'normalized_position'].some((key) =>
- Object.prototype.hasOwnProperty.call(input, key),
- );
- const target = hasExplicitTarget ? await this.resolveTarget(session, input, false) : undefined;
- if (
- target &&
- (!Number.isFinite(target.x) ||
- !Number.isFinite(target.y) ||
- target.x < 0 ||
- target.y < 0 ||
- target.x > viewport.width ||
- target.y > viewport.height)
- ) {
- return {
- success: false,
- code: 'NO_SCROLL_EFFECT',
- error: 'Scroll target is outside the current viewport.',
- retryable: true,
- recovery:
- 'Inspect the current viewport and choose a visible scroll container, ref, selector, or normalized position.',
- effect: {
- observed: false,
- moved: false,
- actualDeltaX: 0,
- actualDeltaY: 0,
- atStart: false,
- atEnd: false,
- },
- };
- }
- const observationPoint = target
- ? { x: target.contextX ?? target.x, y: target.contextY ?? target.y }
- : null;
- const before = await this.inspectScrollSnapshot(session, observationPoint, axis);
- const beforeObserved = before !== null && before.kind !== 'unknown';
- const beforePosition = axis === 'y' ? (before?.scrollTop ?? 0) : (before?.scrollLeft ?? 0);
- const beforeExtent = axis === 'y' ? (before?.scrollHeight ?? 0) : (before?.scrollWidth ?? 0);
- const beforeViewport = axis === 'y' ? (before?.clientHeight ?? 0) : (before?.clientWidth ?? 0);
- const beforeAtRequestedBoundary =
- beforeObserved &&
- Boolean(before?.scrollable) &&
- (direction === 'down' || direction === 'right'
- ? beforePosition + beforeViewport >= beforeExtent - SCROLL_BOUNDARY_EPSILON
- : beforePosition <= SCROLL_BOUNDARY_EPSILON);
- let after = before;
- let usedUnobservedWheelFallback = false;
- if (!beforeAtRequestedBoundary) {
- if (target) {
- usedUnobservedWheelFallback =
- before?.kind === 'unknown' || (beforeObserved && !before?.scrollable);
- await session.beforePointerAction?.(
- { action: 'scroll', point: { x: target.x, y: target.y } },
- session.commandSignal,
- );
- await this.dispatchMouse(session, target.x, target.y, 'mouseMoved');
- await this.dispatchMouse(session, target.x, target.y, 'mouseWheel', { deltaX, deltaY });
- } else {
- // An untargeted scroll owns the document contract. Dispatching a wheel
- // at the viewport center can instead move an unrelated nested
- // container, so use the document scrolling primitive just like the
- // mature Electron path.
- await evaluate(session, `window.scrollBy(${deltaX}, ${deltaY})`);
- }
- await delay(ACTION_SETTLE_DELAY_MS, session.commandSignal);
- after = await this.inspectScrollSnapshot(session, observationPoint, axis);
- }
- const observed =
- before !== null &&
- after !== null &&
- !usedUnobservedWheelFallback &&
- before.kind !== 'unknown' &&
- before.ownerId === after.ownerId;
- const actualDeltaX = observed && before && after ? after.scrollLeft - before.scrollLeft : 0;
- const actualDeltaY = observed && before && after ? after.scrollTop - before.scrollTop : 0;
- const moved =
- observed &&
- ((deltaX !== 0 &&
- Math.sign(actualDeltaX) === Math.sign(deltaX) &&
- Math.abs(actualDeltaX) >= SCROLL_BOUNDARY_EPSILON) ||
- (deltaY !== 0 &&
- Math.sign(actualDeltaY) === Math.sign(deltaY) &&
- Math.abs(actualDeltaY) >= SCROLL_BOUNDARY_EPSILON));
- const finalSnapshot = after ?? before;
- const relevantPosition =
- axis === 'y' ? (finalSnapshot?.scrollTop ?? 0) : (finalSnapshot?.scrollLeft ?? 0);
- const relevantExtent =
- axis === 'y' ? (finalSnapshot?.scrollHeight ?? 0) : (finalSnapshot?.scrollWidth ?? 0);
- const relevantViewport =
- axis === 'y' ? (finalSnapshot?.clientHeight ?? 0) : (finalSnapshot?.clientWidth ?? 0);
- // Keep the existing public effect semantics: only the boundary in the
- // requested direction is reported by this action result.
- const atStart =
- observed &&
- (direction === 'up' || direction === 'left') &&
- relevantPosition <= SCROLL_BOUNDARY_EPSILON;
- const atEnd =
- observed &&
- (direction === 'down' || direction === 'right') &&
- relevantPosition + relevantViewport >= relevantExtent - SCROLL_BOUNDARY_EPSILON;
- const requestedBoundaryReached = direction === 'up' || direction === 'left' ? atStart : atEnd;
- this.invalidateObservation(session);
- const effect = { observed, moved, actualDeltaX, actualDeltaY, atStart, atEnd };
- if (!observed) {
- return {
- success: false,
- code: 'SCROLL_EFFECT_UNVERIFIED',
- error: 'Scroll input was dispatched, but its page effect could not be verified.',
- retryable: true,
- recovery:
- 'Inspect the page once and choose a newly observed scroll target or confirm the intended state before continuing.',
- effect,
- };
- }
- const finalExplicitTargetIsNotScrollable = target !== undefined && !finalSnapshot?.scrollable;
- if (!moved && (finalExplicitTargetIsNotScrollable || !requestedBoundaryReached)) {
- return {
- success: false,
- code: 'NO_SCROLL_EFFECT',
- error: 'Scroll action produced no observable movement.',
- retryable: true,
- recovery:
- 'Inspect the page and choose a different scroll target, selector, ref, or normalized position.',
- effect,
- };
- }
- return {
- success: true,
- effect,
- };
- }
- protected async waitFor(session: SessionState, input: Record<string, unknown>): Promise<unknown> {
- const kind = asString(input.kind);
- const timeoutMs = Math.max(0, Math.floor(asNumber(input.timeout, DEFAULT_WAIT_TIMEOUT_MS)));
- if (kind === 'timeout') {
- await delay(timeoutMs, session.commandSignal);
- return { success: true, kind: 'timeout', waitedMs: timeoutMs };
- }
- const selector = asString(input.selector);
- const text = asString(input.text);
- const url = asString(input.url);
- const load = input.load === true || kind === 'load';
- if (kind === 'selector' && !selector) {
- throw new Error('WAIT_REQUIRES_SELECTOR');
- }
- if (kind === 'text' && !text) throw new Error('WAIT_REQUIRES_TEXT');
- if (kind === 'url' && !url) throw new Error('WAIT_REQUIRES_URL');
- if (!selector && !text && !url && !load) throw new Error('WAIT_REQUIRES_CONDITION');
- const deadline = Date.now() + timeoutMs;
- while (Date.now() <= deadline) {
- const matches = await evaluate<boolean>(session, waitExpression(input));
- if (matches)
- return { success: true, waitedMs: timeoutMs - Math.max(0, deadline - Date.now()) };
- await delay(50, session.commandSignal);
- }
- return { success: false, code: 'ACTION_TIMEOUT', error: 'Browser wait condition timed out.' };
- }
- protected async screenshot(
- session: SessionState,
- input: Record<string, unknown>,
- ): Promise<unknown> {
- if (session.captureScreenshot) {
- if (!session.commandSignal || session.commandTimeoutMs === undefined) {
- throw new Error('SCREENSHOT_CAPTURE_CONTEXT_UNAVAILABLE');
- }
- return session.captureScreenshot(input, {
- signal: session.commandSignal,
- timeoutMs: session.commandTimeoutMs,
- });
- }
- return captureBrowserScreenshot(session, input, () => this.readPageSummary(session));
- }
- }
- function isSupportedBrowserUrl(url: string): boolean {
- return /^(?:https?|file|data|about):/iu.test(url.trim());
- }
|