Skip to content

Commit

Permalink
Do not change ConsoleMessage in this patch
Browse files Browse the repository at this point in the history
  • Loading branch information
aslushnikov committed Oct 6, 2017
1 parent 7ed28d2 commit a557255
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ class Page extends EventEmitter {
event.args.map(arg => helper.releaseObject(this._client, arg));
return;
}
const values = event.args.map(arg => this._frameManager.createJSHandle(event.executionContextId, arg));
const values = await Promise.all(event.args.map(arg => helper.serializeRemoteObject(this._client, arg)));
const text = values.join(' ');
const message = new ConsoleMessage(event.type, text, values);
this.emit(Page.Events.Console, message);
Expand Down
25 changes: 25 additions & 0 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,31 @@ class Helper {
return remoteObject.value;
}

/**
* @param {!Session} client
* @param {!Object} remoteObject
* @return {!Promise<!Object>}
*/
static async serializeRemoteObject(client, remoteObject) {
if (!remoteObject.objectId)
return Helper.valueFromRemoteObject(remoteObject);
if (remoteObject.subtype === 'promise')
return remoteObject.description;
try {
const response = await client.send('Runtime.callFunctionOn', {
objectId: remoteObject.objectId,
functionDeclaration: 'function() { return this; }',
returnByValue: true,
});
return response.result.value;
} catch (e) {
// Return description for unserializable object, e.g. 'window'.
return remoteObject.description;
} finally {
Helper.releaseObject(client, remoteObject);
}
}

/**
* @param {!Session} client
* @param {!Object} remoteObject
Expand Down
10 changes: 4 additions & 6 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -655,11 +655,9 @@ describe('Page', function() {
page.evaluate(() => console.log('hello', 5, {foo: 'bar'})),
waitForEvents(page, 'console')
]);
expect(message.text).toEqual('hello 5 JSHandle@object');
expect(message.text).toEqual('hello 5 [object Object]');
expect(message.type).toEqual('log');
expect(await message.args[0].jsonValue()).toEqual('hello');
expect(await message.args[1].jsonValue()).toEqual(5);
expect(await message.args[2].jsonValue()).toEqual({foo: 'bar'});
expect(message.args).toEqual(['hello', 5, {foo: 'bar'}]);
}));
it('should work for different console API calls', SX(async function() {
const messages = [];
Expand Down Expand Up @@ -687,7 +685,7 @@ describe('Page', function() {
'calling console.dir',
'calling console.warn',
'calling console.error',
'JSHandle@promise',
'Promise',
]);
}));
it('should not fail for window object', SX(async function() {
Expand All @@ -697,7 +695,7 @@ describe('Page', function() {
page.evaluate(() => console.error(window)),
waitForEvents(page, 'console')
]);
expect(message.text).toBe('JSHandle@object');
expect(message.text).toBe('Window');
}));
});

Expand Down

0 comments on commit a557255

Please sign in to comment.