page.evaluate takes a string in addition to function (#135)

This patch improves on page.evaluate to accept a string.
The string can have a trailing '//# sourceURL=' comment which would
name the evaluation to make stacks beautiful.

In order to make sourceURL comments possible, this patch:
- removes wrapping of the client function into `Promise.resolve()`
- stops passing `awaitPromise` parameter to `Runtime.evaluate`
- starts to await promise via the `Runtime.awaitPromise` if the return type of the evaluation
  is promise

closes #118
This commit is contained in:
JoelEinbinder
2017-07-27 12:23:41 -07:00
committed by Andrey Lushnikov
parent 8870aaee17
commit bbde8fd1c2
6 changed files with 62 additions and 36 deletions

View File

@@ -16,11 +16,15 @@
class Helper {
/**
* @param {function()} fun
* @param {function()|string} fun
* @param {!Array<*>} args
* @return {string}
*/
static evaluationString(fun, ...args) {
if (typeof fun === 'string') {
console.assert(args.length === 0, 'Cannot evaluate a string with arguments');
return fun;
}
return `(${fun})(${args.map(x => JSON.stringify(x)).join(',')})`;
}
@@ -48,6 +52,16 @@ class Helper {
* @return {!Promise<!Object>}
*/
static async serializeRemoteObject(client, remoteObject) {
if (remoteObject.subtype === 'promise') {
let response = (await client.send('Runtime.awaitPromise', {
promiseObjectId: remoteObject.objectId,
returnByValue: false
}));
Helper.releaseObject(client, remoteObject);
if (response.exceptionDetails)
throw new Error('Evaluation failed: ' + Helper.getExceptionMessage(response.exceptionDetails));
remoteObject = response.result;
}
if (remoteObject.unserializableValue) {
switch (remoteObject.unserializableValue) {
case '-0':