-
Notifications
You must be signed in to change notification settings - Fork 9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(ObjectHandles): Implement Object Handles #943
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2c1f001
feat(ObjectHandles): Implement Object Handles
aslushnikov b95db6b
s/page.object/page.evaluateHandle/g
aslushnikov b51b601
s/frame.context()/frame.executionContext()/g
aslushnikov e65c761
s/ObjectHandle/JSHandle/g
aslushnikov 816d19e
s/asJSON/jsonValue/g
aslushnikov 9e63e65
s/objectHandle.context/objectHandle.executionContext/g
aslushnikov 44c6fe7
Introduce JSHandle.getProperty(-ies)
aslushnikov 9207cab
kill JSHandle.type()
aslushnikov e5df97b
address doclint comments
aslushnikov 7ed28d2
Update docs/api.md
aslushnikov a557255
Do not change ConsoleMessage in this patch
aslushnikov 65d1a7c
add JSHandle.toString() test
aslushnikov f506946
address comments on documentation
aslushnikov ddda822
Address Joel's comments
aslushnikov File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
/** | ||
* Copyright 2017 Google Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
const {helper} = require('./helper'); | ||
|
||
class ExecutionContext { | ||
/** | ||
* @param {!Session} client | ||
* @param {string} contextId | ||
* @param {function(*):!JSHandle} objectHandleFactory | ||
*/ | ||
constructor(client, contextId, objectHandleFactory) { | ||
this._client = client; | ||
this._contextId = contextId; | ||
this._objectHandleFactory = objectHandleFactory; | ||
} | ||
|
||
/** | ||
* @param {function()|string} pageFunction | ||
* @param {!Array<*>} args | ||
* @return {!Promise<(!Object|undefined)>} | ||
*/ | ||
async evaluate(pageFunction, ...args) { | ||
const handle = await this.evaluateHandle(pageFunction, ...args); | ||
const result = await handle.jsonValue(); | ||
await handle.dispose(); | ||
return result; | ||
} | ||
|
||
/** | ||
* @param {function()|string} pageFunction | ||
* @param {!Array<*>} args | ||
* @return {!Promise<!JSHandle>} | ||
*/ | ||
async evaluateHandle(pageFunction, ...args) { | ||
if (helper.isString(pageFunction)) { | ||
const contextId = this._contextId; | ||
const expression = pageFunction; | ||
const { exceptionDetails, result: remoteObject } = await this._client.send('Runtime.evaluate', { expression, contextId, returnByValue: false, awaitPromise: true}); | ||
if (exceptionDetails) | ||
throw new Error('Evaluation failed: ' + helper.getExceptionMessage(exceptionDetails)); | ||
return this._objectHandleFactory(remoteObject); | ||
} | ||
|
||
const { exceptionDetails, result: remoteObject } = await this._client.send('Runtime.callFunctionOn', { | ||
functionDeclaration: pageFunction.toString(), | ||
executionContextId: this._contextId, | ||
arguments: args.map(convertArgument.bind(this)), | ||
returnByValue: false, | ||
awaitPromise: true | ||
}); | ||
if (exceptionDetails) | ||
throw new Error('Evaluation failed: ' + helper.getExceptionMessage(exceptionDetails)); | ||
return this._objectHandleFactory(remoteObject); | ||
|
||
/** | ||
* @param {*} arg | ||
* @return {*} | ||
* @this {Frame} | ||
*/ | ||
function convertArgument(arg) { | ||
if (Object.is(arg, -0)) | ||
return { unserializableValue: '-0' }; | ||
if (Object.is(arg, Infinity)) | ||
return { unserializableValue: 'Infinity' }; | ||
if (Object.is(arg, -Infinity)) | ||
return { unserializableValue: '-Infinity' }; | ||
if (Object.is(arg, NaN)) | ||
return { unserializableValue: 'NaN' }; | ||
const objectHandle = arg && (arg instanceof JSHandle) ? arg : null; | ||
if (objectHandle) { | ||
if (objectHandle._context !== this) | ||
throw new Error('JSHandles can be evaluated only in the context they were created!'); | ||
if (objectHandle._disposed) | ||
throw new Error('JSHandle is disposed!'); | ||
if (objectHandle._remoteObject.unserializableValue) | ||
return { unserializableValue: objectHandle._remoteObject.unserializableValue }; | ||
if (!objectHandle._remoteObject.objectId) | ||
return { value: objectHandle._remoteObject.value }; | ||
return { objectId: objectHandle._remoteObject.objectId }; | ||
} | ||
return { value: arg }; | ||
} | ||
} | ||
} | ||
|
||
class JSHandle { | ||
/** | ||
* @param {!ExecutionContext} context | ||
* @param {!Session} client | ||
* @param {!Object} remoteObject | ||
*/ | ||
constructor(context, client, remoteObject) { | ||
this._context = context; | ||
this._client = client; | ||
this._remoteObject = remoteObject; | ||
this._disposed = false; | ||
} | ||
|
||
/** | ||
* @return {!ExecutionContext} | ||
*/ | ||
executionContext() { | ||
return this._context; | ||
} | ||
|
||
/** | ||
* @param {string} propertyName | ||
* @return {!Promise<?JSHandle>} | ||
*/ | ||
async getProperty(propertyName) { | ||
const objectHandle = await this._context.evaluateHandle((object, propertyName) => { | ||
const result = {__proto__: null}; | ||
result[propertyName] = object[propertyName]; | ||
return result; | ||
}, this, propertyName); | ||
const properties = await objectHandle.getProperties(); | ||
const result = properties.get(propertyName) || null; | ||
await objectHandle.dispose(); | ||
return result; | ||
} | ||
|
||
/** | ||
* @return {!Property<Map<string, !ObjectHandle>>} | ||
*/ | ||
async getProperties() { | ||
const response = await this._client.send('Runtime.getProperties', { | ||
objectId: this._remoteObject.objectId, | ||
ownProperties: true | ||
}); | ||
const result = new Map(); | ||
for (const property of response.result) { | ||
if (!property.enumerable) | ||
continue; | ||
result.set(property.name, this._context._objectHandleFactory(property.value)); | ||
} | ||
return result; | ||
} | ||
|
||
/** | ||
* @return {!Promise<?Object>} | ||
*/ | ||
async jsonValue() { | ||
if (this._remoteObject.objectId) { | ||
const jsonString = await this._context.evaluate(object => JSON.stringify(object), this); | ||
return JSON.parse(jsonString); | ||
} | ||
return helper.valueFromRemoteObject(this._remoteObject); | ||
} | ||
|
||
/** | ||
* @return {?ElementHandle} | ||
*/ | ||
asElement() { | ||
return null; | ||
} | ||
|
||
async dispose() { | ||
if (this._disposed) | ||
return; | ||
this._disposed = true; | ||
await helper.releaseObject(this._client, this._remoteObject); | ||
} | ||
|
||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
* @override | ||
* @return {string} | ||
*/ | ||
toString() { | ||
if (this._remoteObject.objectId) { | ||
const type = this._remoteObject.subtype || this._remoteObject.type; | ||
return 'JSHandle@' + type; | ||
} | ||
return helper.valueFromRemoteObject(this._remoteObject) + ''; | ||
} | ||
} | ||
|
||
helper.tracePublicAPI(JSHandle); | ||
module.exports = {ExecutionContext, JSHandle}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we want only enumerable and "own" properties? Feels arbitrary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-enumerable might clash with names.
ownProperties is a good call, I removed this flag and added a test.