mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
feat(console): dispatch JSHandles as console arguments (#975)
This patch starts dispatching JSHandle instances as console arguments. BREAKING CHANGE: this changes the API of the ConsoleMessage. Fixes #324.
This commit is contained in:
parent
8d1d9fec65
commit
e6af6e19d6
@ -1183,7 +1183,7 @@ Dialog's type, can be one of `alert`, `beforeunload`, `confirm` or `prompt`.
|
|||||||
[ConsoleMessage] objects are dispatched by page via the ['console'](#event-console) event.
|
[ConsoleMessage] objects are dispatched by page via the ['console'](#event-console) event.
|
||||||
|
|
||||||
#### consoleMessage.args
|
#### consoleMessage.args
|
||||||
- <[Array]<[string]>>
|
- <[Array]<[JSHandle]>>
|
||||||
|
|
||||||
#### consoleMessage.text
|
#### consoleMessage.text
|
||||||
- <[string]>
|
- <[string]>
|
||||||
|
@ -331,7 +331,7 @@ class Page extends EventEmitter {
|
|||||||
event.args.map(arg => helper.releaseObject(this._client, arg));
|
event.args.map(arg => helper.releaseObject(this._client, arg));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const values = await Promise.all(event.args.map(arg => helper.serializeRemoteObject(this._client, arg)));
|
const values = event.args.map(arg => this._frameManager.createJSHandle(event.executionContextId, arg));
|
||||||
const text = values.join(' ');
|
const text = values.join(' ');
|
||||||
const message = new ConsoleMessage(event.type, text, values);
|
const message = new ConsoleMessage(event.type, text, values);
|
||||||
this.emit(Page.Events.Console, message);
|
this.emit(Page.Events.Console, message);
|
||||||
|
@ -83,31 +83,6 @@ class Helper {
|
|||||||
return remoteObject.value;
|
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 {!Session} client
|
||||||
* @param {!Object} remoteObject
|
* @param {!Object} remoteObject
|
||||||
|
10
test/test.js
10
test/test.js
@ -695,9 +695,11 @@ describe('Page', function() {
|
|||||||
page.evaluate(() => console.log('hello', 5, {foo: 'bar'})),
|
page.evaluate(() => console.log('hello', 5, {foo: 'bar'})),
|
||||||
waitForEvents(page, 'console')
|
waitForEvents(page, 'console')
|
||||||
]);
|
]);
|
||||||
expect(message.text).toEqual('hello 5 [object Object]');
|
expect(message.text).toEqual('hello 5 JSHandle@object');
|
||||||
expect(message.type).toEqual('log');
|
expect(message.type).toEqual('log');
|
||||||
expect(message.args).toEqual(['hello', 5, {foo: 'bar'}]);
|
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'});
|
||||||
}));
|
}));
|
||||||
it('should work for different console API calls', SX(async function() {
|
it('should work for different console API calls', SX(async function() {
|
||||||
const messages = [];
|
const messages = [];
|
||||||
@ -725,7 +727,7 @@ describe('Page', function() {
|
|||||||
'calling console.dir',
|
'calling console.dir',
|
||||||
'calling console.warn',
|
'calling console.warn',
|
||||||
'calling console.error',
|
'calling console.error',
|
||||||
'Promise',
|
'JSHandle@promise',
|
||||||
]);
|
]);
|
||||||
}));
|
}));
|
||||||
it('should not fail for window object', SX(async function() {
|
it('should not fail for window object', SX(async function() {
|
||||||
@ -735,7 +737,7 @@ describe('Page', function() {
|
|||||||
page.evaluate(() => console.error(window)),
|
page.evaluate(() => console.error(window)),
|
||||||
waitForEvents(page, 'console')
|
waitForEvents(page, 'console')
|
||||||
]);
|
]);
|
||||||
expect(message.text).toBe('Window');
|
expect(message.text).toBe('JSHandle@object');
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user