diff --git a/src/common/AriaQueryHandler.ts b/src/common/AriaQueryHandler.ts index 03088487b4f..45400dd2c5d 100644 --- a/src/common/AriaQueryHandler.ts +++ b/src/common/AriaQueryHandler.ts @@ -17,13 +17,12 @@ import {Protocol} from 'devtools-protocol'; import {assert} from '../util/assert.js'; import {CDPSession} from './Connection.js'; +import {ElementHandle} from './ElementHandle.js'; import { IsolatedWorld, PageBinding, WaitForSelectorOptions, } from './IsolatedWorld.js'; -import {ElementHandle} from './ElementHandle.js'; -import {JSHandle} from './JSHandle.js'; import {InternalQueryHandler} from './QueryHandler.js'; async function queryAXTree( @@ -149,18 +148,6 @@ const queryAll = async ( ); }; -const queryAllArray = async ( - element: ElementHandle, - selector: string -): Promise> => { - const elementHandles = await queryAll(element, selector); - const exeCtx = element.executionContext(); - const jsHandle = exeCtx.evaluateHandle((...elements) => { - return elements; - }, ...elementHandles); - return jsHandle; -}; - /** * @internal */ @@ -168,5 +155,4 @@ export const ariaHandler: InternalQueryHandler = { queryOne, waitFor, queryAll, - queryAllArray, }; diff --git a/src/common/ElementHandle.ts b/src/common/ElementHandle.ts index cb9b5524ecf..bdb686ddaac 100644 --- a/src/common/ElementHandle.ts +++ b/src/common/ElementHandle.ts @@ -19,7 +19,7 @@ import { } from './JSHandle.js'; import {Page, ScreenshotOptions} from './Page.js'; import {getQueryHandlerAndSelector} from './QueryHandler.js'; -import {EvaluateFunc, NodeFor} from './types.js'; +import {EvaluateFunc, HandleFor, NodeFor} from './types.js'; import {KeyInput} from './USKeyboardLayout.js'; import {debugError, isString} from './util.js'; @@ -236,13 +236,24 @@ export class ElementHandle< ): Promise>> { const {updatedSelector, queryHandler} = getQueryHandlerAndSelector(selector); - assert(queryHandler.queryAllArray); - const arrayHandle = (await queryHandler.queryAllArray( + assert( + queryHandler.queryAll, + 'Cannot handle queries for a multiple element with the given selector' + ); + const handles = (await queryHandler.queryAll( this, updatedSelector - )) as JSHandle>>; - const result = await arrayHandle.evaluate(pageFunction, ...args); - await arrayHandle.dispose(); + )) as Array>>; + const elements = await this.evaluateHandle((_, ...elements) => { + return elements; + }, ...handles); + const [result] = await Promise.all([ + elements.evaluate(pageFunction, ...args), + ...handles.map(handle => { + return handle.dispose(); + }), + ]); + await elements.dispose(); return result; } diff --git a/src/common/QueryHandler.ts b/src/common/QueryHandler.ts index a62786632b6..dc73558ec43 100644 --- a/src/common/QueryHandler.ts +++ b/src/common/QueryHandler.ts @@ -15,9 +15,8 @@ */ import {ariaHandler} from './AriaQueryHandler.js'; -import {IsolatedWorld, WaitForSelectorOptions} from './IsolatedWorld.js'; import {ElementHandle} from './ElementHandle.js'; -import {JSHandle} from './JSHandle.js'; +import {IsolatedWorld, WaitForSelectorOptions} from './IsolatedWorld.js'; /** * @public @@ -55,16 +54,6 @@ export interface InternalQueryHandler { element: ElementHandle, selector: string ) => Promise>>; - /** - * Queries for multiple nodes given a selector and {@link ElementHandle}. - * Unlike {@link queryAll}, this returns a handle to a node array. - * - * Akin to {@link Window.prototype.querySelectorAll}. - */ - queryAllArray?: ( - element: ElementHandle, - selector: string - ) => Promise>; /** * Waits until a single node appears for a given selector and @@ -119,16 +108,6 @@ function internalizeCustomQueryHandler( } return result; }; - internalHandler.queryAllArray = async (element, selector) => { - const resultHandle = (await element.evaluateHandle( - queryAll, - selector - )) as JSHandle>; - const arrayHandle = await resultHandle.evaluateHandle(res => { - return Array.from(res); - }); - return arrayHandle; - }; } return internalHandler;