feat(ElementHandle): introduce elementHandle.$eval (#2407)

This patch introduces `elementHandle.$eval` method.

References #2401.
This commit is contained in:
Denny Ku(kuni)
2018-05-09 10:17:59 +09:00
committed by Andrey Lushnikov
parent 1d225cfa17
commit 88b996877f
4 changed files with 52 additions and 6 deletions

View File

@@ -280,6 +280,21 @@ class ElementHandle extends JSHandle {
return result;
}
/**
* @param {string} selector
* @param {Function|String} pageFunction
* @param {!Array<*>} args
* @return {!Promise<(!Object|undefined)>}
*/
async $eval(selector, pageFunction, ...args) {
const elementHandle = await this.$(selector);
if (!elementHandle)
throw new Error(`Error: failed to find element matching selector "${selector}"`);
const result = await this.executionContext().evaluate(pageFunction, elementHandle, ...args);
await elementHandle.dispose();
return result;
}
/**
* @param {string} expression
* @return {!Promise<!Array<!ElementHandle>>}

View File

@@ -366,12 +366,9 @@ class Frame {
* @return {!Promise<(!Object|undefined)>}
*/
async $eval(selector, pageFunction, ...args) {
const elementHandle = await this.$(selector);
if (!elementHandle)
throw new Error(`Error: failed to find element matching selector "${selector}"`);
const result = await this.evaluate(pageFunction, elementHandle, ...args);
await elementHandle.dispose();
return result;
const document = await this._document();
const value = await document.$eval(selector, pageFunction, ...args);
return value;
}
/**