diff --git a/lib/FrameManager.js b/lib/FrameManager.js index cb5345316de..d6a4bf18b1a 100644 --- a/lib/FrameManager.js +++ b/lib/FrameManager.js @@ -221,11 +221,21 @@ class FrameManager extends EventEmitter { subtree: true }); }); - await this._client.send('Runtime.evaluate', { + let contextId = undefined; + if (!frame.isMainFrame()) { + contextId = this._frameIdToExecutionContextId.get(frame._id); + console.assert(contextId, 'Frame does not have default context to evaluate in!'); + } + let { exceptionDetails } = await this._client.send('Runtime.evaluate', { expression: helper.evaluationString(code, selector), + contextId, awaitPromise: true, - returnByValue: true, + returnByValue: false, }); + if (exceptionDetails) { + let message = await helper.getExceptionMessage(this._client, exceptionDetails); + throw new Error('Evaluation failed: ' + message); + } } } diff --git a/test/test.js b/test/test.js index 81f2403ae03..82e28cb67de 100644 --- a/test/test.js +++ b/test/test.js @@ -197,6 +197,33 @@ describe('Puppeteer', function() { await page.evaluate(addElement, 'div'); expect(added).toBe(true); })); + + it('should run in specified frame', SX(async function() { + await FrameUtils.attachFrame(page, 'frame1', EMPTY_PAGE); + await FrameUtils.attachFrame(page, 'frame2', EMPTY_PAGE); + let frame1 = page.frames()[1]; + let frame2 = page.frames()[2]; + let added = false; + frame2.waitFor('div').then(() => added = true); + expect(added).toBe(false); + await frame1.evaluate(addElement, 'div'); + expect(added).toBe(false); + await frame2.evaluate(addElement, 'div'); + expect(added).toBe(true); + })); + + it('should throw if evaluation failed', SX(async function() { + await page.evaluateOnInitialized(function() { + document.querySelector = null; + }); + await page.navigate(EMPTY_PAGE); + try { + await page.waitFor('*'); + fail('Failed waitFor did not throw.'); + } catch (e) { + expect(e.message).toBe('Evaluation failed: document.querySelector is not a function'); + } + })); }); it('Page Events: ConsoleMessage', SX(async function() {