fix: make exposeFunction work for frames (#1295)

This patch fixes page.exposeFunction method so that it
adds exposed binding to all existing frames.

Fixes #690
This commit is contained in:
JoelEinbinder 2017-11-07 13:26:23 -08:00 committed by Andrey Lushnikov
parent 8c9332b62e
commit 3cb0f1af34
2 changed files with 27 additions and 3 deletions

View File

@ -289,8 +289,8 @@ class Page extends EventEmitter {
this._pageBindings[name] = puppeteerFunction;
const expression = helper.evaluationString(addPageBinding, name);
await this._client.send('Page.addScriptToEvaluateOnNewDocument', { source: expression });
await this._client.send('Runtime.evaluate', { expression, returnByValue: true });
await this._client.send('Page.addScriptToEvaluateOnNewDocument', {source: expression});
await Promise.all(this.frames().map(frame => frame.evaluate(expression).catch(debugError)));
function addPageBinding(bindingName) {
window[bindingName] = async(...args) => {
@ -375,7 +375,7 @@ class Page extends EventEmitter {
const {name, seq, args} = JSON.parse(event.args[1].value);
const result = await this._pageBindings[name](...args);
const expression = helper.evaluationString(deliverResult, name, seq, result);
this._client.send('Runtime.evaluate', { expression }).catch(debugError);
this._client.send('Runtime.evaluate', { expression, contextId: event.executionContextId }).catch(debugError);
function deliverResult(name, seq, result) {
window[name]['callbacks'].get(seq)(result);

View File

@ -1188,6 +1188,30 @@ describe('Page', function() {
});
expect(result).toBe(15);
}));
it('should work on frames', SX(async function() {
await page.exposeFunction('compute', function(a, b) {
return Promise.resolve(a * b);
});
await page.goto(PREFIX + '/frames/nested-frames.html');
const frame = page.frames()[1];
const result = await frame.evaluate(async function() {
return await compute(3, 5);
});
expect(result).toBe(15);
}));
it('should work on frames before navigation', SX(async function() {
await page.goto(PREFIX + '/frames/nested-frames.html');
await page.exposeFunction('compute', function(a, b) {
return Promise.resolve(a * b);
});
const frame = page.frames()[1];
const result = await frame.evaluate(async function() {
return await compute(3, 5);
});
expect(result).toBe(15);
}));
});
describe('Page.setRequestInterception', function() {