Skip to content

Commit

Permalink
fix(firefox): do not report console messages twice.
Browse files Browse the repository at this point in the history
  • Loading branch information
aslushnikov committed Apr 1, 2021
1 parent 16d98cb commit ac9f377
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
4 changes: 2 additions & 2 deletions browser_patches/firefox/BUILD_NUMBER
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
1239
Changed: ross.wollman@gmail.com Mon Mar 29 00:27:24 PDT 2021
1240
Changed: lushnikov@chromium.org Thu Apr 1 02:47:10 -05 2021
2 changes: 1 addition & 1 deletion browser_patches/firefox/juggler/content/PageAgent.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class WorkerData {
evaluate: (options) => this._workerRuntime.send('evaluate', options),
callFunction: (options) => this._workerRuntime.send('callFunction', options),
getObjectProperties: (options) => this._workerRuntime.send('getObjectProperties', options),
disposeObject: (options) =>this._workerRuntime.send('disposeObject', options),
disposeObject: (options) => this._workerRuntime.send('disposeObject', options),
}),
];
}
Expand Down
21 changes: 19 additions & 2 deletions browser_patches/firefox/juggler/protocol/PageHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ const Cu = Components.utils;
const XUL_NS = 'http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul';
const helper = new Helper();

function hashConsoleMessage(params) {
return params.location.lineNumber + ':' + params.location.columnNumber + ':' + params.location.url;
}

class WorkerHandler {
constructor(session, contentChannel, workerId) {
this._session = session;
this._contentWorker = contentChannel.connect(workerId);
this._workerConsoleMessages = new Set();
this._workerId = workerId;

const emitWrappedProtocolEvent = eventName => {
Expand All @@ -32,7 +37,10 @@ class WorkerHandler {

this._eventListeners = [
contentChannel.register(workerId, {
runtimeConsole: emitWrappedProtocolEvent('Runtime.console'),
runtimeConsole: (params) => {
this._workerConsoleMessages.add(hashConsoleMessage(params));
emitWrappedProtocolEvent('Runtime.console')(params);
},
runtimeExecutionContextCreated: emitWrappedProtocolEvent('Runtime.executionContextCreated'),
runtimeExecutionContextDestroyed: emitWrappedProtocolEvent('Runtime.executionContextDestroyed'),
}),
Expand Down Expand Up @@ -111,7 +119,16 @@ class PageHandler {
pageUncaughtError: emitProtocolEvent('Page.uncaughtError'),
pageWorkerCreated: this._onWorkerCreated.bind(this),
pageWorkerDestroyed: this._onWorkerDestroyed.bind(this),
runtimeConsole: emitProtocolEvent('Runtime.console'),
runtimeConsole: params => {
const consoleMessageHash = hashConsoleMessage(params);
for (const worker of this._workers) {
if (worker._workerConsoleMessages.has(consoleMessageHash)) {
worker._workerConsoleMessages.delete(consoleMessageHash);
return;
}
}
emitProtocolEvent('Runtime.console')(params);
},
runtimeExecutionContextCreated: emitProtocolEvent('Runtime.executionContextCreated'),
runtimeExecutionContextDestroyed: emitProtocolEvent('Runtime.executionContextDestroyed'),

Expand Down
15 changes: 15 additions & 0 deletions test/workers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ it('should report console logs', async function({page}) {
expect(page.url()).not.toContain('blob');
});

it('should not report console logs from workers twice', (test, {browserName}) => {
test.fail(browserName === 'firefox');
}, async function({page}) {
const messages = [];
page.on('console', msg => messages.push(msg.text()));
await Promise.all([
page.evaluate(() => new Worker(URL.createObjectURL(new Blob(['console.log(1); console.log(2);'], {type: 'application/javascript'})))),
page.waitForEvent('console', msg => msg.text() === '1'),
page.waitForEvent('console', msg => msg.text() === '2'),
]);
expect(messages).toEqual(['1', '2']);
// Firefox's juggler had an issue that reported worker blob urls as frame urls.
expect(page.url()).not.toContain('blob');
});

it('should have JSHandles for console logs', async function({page}) {
const logPromise = new Promise<ConsoleMessage>(x => page.on('console', x));
await page.evaluate(() => new Worker(URL.createObjectURL(new Blob(['console.log(1,2,3,this)'], {type: 'application/javascript'}))));
Expand Down

0 comments on commit ac9f377

Please sign in to comment.