fix(JSHandle.toString): clearer description for primitives (#993)

This patch:
- updates JSHandle.toString to make a nicer description for primitives
- excludes JSHandle.toString from documentation to avoid its abuse

References #382
This commit is contained in:
Andrey Lushnikov 2017-10-10 10:54:20 -07:00 committed by GitHub
parent c3fb367148
commit 079db90066
5 changed files with 15 additions and 9 deletions

View File

@ -134,7 +134,6 @@
+ [jsHandle.getProperties()](#jshandlegetproperties)
+ [jsHandle.getProperty(propertyName)](#jshandlegetpropertypropertyname)
+ [jsHandle.jsonValue()](#jshandlejsonvalue)
+ [jsHandle.toString()](#jshandletostring)
* [class: ElementHandle](#class-elementhandle)
+ [elementHandle.asElement()](#elementhandleaselement)
+ [elementHandle.boundingBox()](#elementhandleboundingbox)
@ -1509,9 +1508,6 @@ Returns a JSON representation of the object. The JSON is generated by running [`
> **NOTE** The method will throw if the referenced object is not stringifiable.
#### jsHandle.toString()
- returns: <[string]>
### class: ElementHandle
> **NOTE** Class [ElementHandle] extends [JSHandle].

View File

@ -184,7 +184,7 @@ class JSHandle {
const type = this._remoteObject.subtype || this._remoteObject.type;
return 'JSHandle@' + type;
}
return helper.valueFromRemoteObject(this._remoteObject) + '';
return 'JSHandle:' + helper.valueFromRemoteObject(this._remoteObject);
}
}

View File

@ -330,8 +330,15 @@ class Page extends EventEmitter {
return;
}
const values = event.args.map(arg => this._frameManager.createJSHandle(event.executionContextId, arg));
const text = values.join(' ');
const message = new ConsoleMessage(event.type, text, values);
const textTokens = [];
for (let i = 0; i < event.args.length; ++i) {
const remoteObject = event.args[i];
if (remoteObject.objectId)
textTokens.push(values[i].toString());
else
textTokens.push(helper.valueFromRemoteObject(remoteObject));
}
const message = new ConsoleMessage(event.type, textTokens.join(' '), values);
this.emit(Page.Events.Console, message);
}

View File

@ -408,8 +408,10 @@ describe('Page', function() {
describe('JSHandle.toString', function() {
it('should work for primitives', SX(async function() {
const aHandle = await page.evaluateHandle(() => 2);
expect(aHandle.toString()).toBe('2');
const numberHandle = await page.evaluateHandle(() => 2);
expect(numberHandle.toString()).toBe('JSHandle:2');
const stringHandle = await page.evaluateHandle(() => 'a');
expect(stringHandle.toString()).toBe('JSHandle:a');
}));
it('should work for complicated objects', SX(async function() {
const aHandle = await page.evaluateHandle(() => window);

View File

@ -37,6 +37,7 @@ const EXCLUDE_CLASSES = new Set([
const EXCLUDE_METHODS = new Set([
'Headers.fromPayload',
'Page.create',
'JSHandle.toString',
]);
/**