Skip to content

Commit

Permalink
fix: ignore console messages from destroyed execution contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
aslushnikov committed Jan 30, 2019
1 parent f2c968f commit f9df876
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,17 @@ class Page extends EventEmitter {
* @param {!Protocol.Runtime.consoleAPICalledPayload} event
*/
async _onConsoleAPI(event) {
if (event.executionContextId === 0) {
// DevTools protocol stores 1000 last console messages. These messages are
// reported even for removed execution contexts - in this case, they are marked with
// executionContextId = 0 and upon enabling Runtime agent.
// Ignore these messages since:
// - there's no execution context we can use to operate with message arguments
// - these messages are reported before Puppeteer clients can subscribe to the 'console' page event.
//
// @see https://github.com/GoogleChrome/puppeteer/issues/3865
return;
}
const context = this._frameManager.executionContextById(event.executionContextId);
const values = event.args.map(arg => createJSHandle(context, arg));
this._addConsoleMessage(event.type, values, event.stackTrace);
Expand Down
18 changes: 18 additions & 0 deletions test/page.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,24 @@ module.exports.addTests = function({testRunner, expect, headless}) {
columnNumber: 14,
});
});
// @see https://github.com/GoogleChrome/puppeteer/issues/3865
fit('should not throw when there are console messages in detached iframes', async({browser, page, server}) => {
await page.goto(server.EMPTY_PAGE);
await page.evaluate(async () => {
// 1. Create a popup that Puppeteer is not connected to.
var win = window.open(window.location.href, "Title", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=780,height=200,top=0,left=0");
await new Promise(x => win.onload = x);
// 2. In this popup, create an iframe that console.logs a message.
win.document.body.innerHTML = `<iframe src="/consolelog.html"></iframe>`;
const frame = win.document.querySelector('iframe');
await new Promise(x => frame.onload = x);
// 3. After that, remove the iframe.
frame.remove();
});
const popupTarget = page.browserContext().targets().find(target => target !== page.target());
// 4. Connect to the popup and make sure it doesn't throw.
const popup = await popupTarget.page();
});
});

describe('Page.Events.DOMContentLoaded', function() {
Expand Down

0 comments on commit f9df876

Please sign in to comment.