Implement page.$$ method (#463)

This patch implements page.$$ which runs document.querySelectorAll
in page and returns results as an array of ElementHandle instances.

Fixes #384.
This commit is contained in:
Andrey Lushnikov
2017-08-22 22:56:55 -07:00
committed by GitHub
parent 7e1f2f0609
commit 151d512ae2
4 changed files with 63 additions and 3 deletions

View File

@@ -191,10 +191,33 @@ class Frame {
const remoteObject = await this._rawEvaluate(selector => document.querySelector(selector), selector);
if (remoteObject.subtype === 'node')
return new ElementHandle(this._client, remoteObject, this._mouse);
helper.releaseObject(this._client, remoteObject);
await helper.releaseObject(this._client, remoteObject);
return null;
}
/**
* @param {string} selector
* @return {!Promise<!Array<!ElementHandle>>}
*/
async $$(selector) {
const remoteObject = await this._rawEvaluate(selector => Array.from(document.querySelectorAll(selector)), selector);
const response = await this._client.send('Runtime.getProperties', {
objectId: remoteObject.objectId,
ownProperties: true
});
const properties = response.result;
const result = [];
const releasePromises = [helper.releaseObject(this._client, remoteObject)];
for (const property of properties) {
if (property.enumerable && property.value.subtype === 'node')
result.push(new ElementHandle(this._client, property.value, this._mouse));
else
releasePromises.push(helper.releaseObject(this._client, property.value));
}
await Promise.all(releasePromises);
return result;
}
/**
* @param {function()|string} pageFunction
* @param {!Array<*>} args