Skip to content

Commit

Permalink
fix(console): respect repeat count in webkit
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman committed Apr 29, 2020
1 parent 6c94f60 commit a5cf03a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
32 changes: 31 additions & 1 deletion src/webkit/wkPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import * as jpeg from 'jpeg-js';
import * as png from 'pngjs';
import { NotConnectedError } from '../errors';
import { logError } from '../logger';
import { ConsoleMessageLocation } from '../console';
import { JSHandle } from '../javascript';

const UTILITY_WORLD_NAME = '__playwright_utility_world__';
const BINDING_CALL_MESSAGE = '__playwright_binding_call__';
Expand All @@ -64,6 +66,7 @@ export class WKPage implements PageDelegate {
private _firstNonInitialNavigationCommittedPromise: Promise<void>;
private _firstNonInitialNavigationCommittedFulfill = () => {};
private _firstNonInitialNavigationCommittedReject = (e: Error) => {};
private _lastConsoleMessage: { derivedType: string, text: string, handles: JSHandle[]; count: number, location: ConsoleMessageLocation; } | null = null;

constructor(browserContext: WKBrowserContext, pageProxySession: WKSession, opener: WKPage | null) {
this._pageProxySession = pageProxySession;
Expand Down Expand Up @@ -320,6 +323,7 @@ export class WKPage implements PageDelegate {
helper.addEventListener(this._session, 'Page.didRequestOpenWindow', event => this._onDidRequestOpenWindow(event)),
helper.addEventListener(this._session, 'Runtime.executionContextCreated', event => this._onExecutionContextCreated(event.context)),
helper.addEventListener(this._session, 'Console.messageAdded', event => this._onConsoleMessage(event)),
helper.addEventListener(this._session, 'Console.messageRepeatCountUpdated', event => this._onConsoleRepeatCountUpdated(event)),
helper.addEventListener(this._pageProxySession, 'Dialog.javascriptDialogOpening', event => this._onDialog(event)),
helper.addEventListener(this._session, 'Page.fileChooserOpened', event => this._onFileChooserOpened(event)),
helper.addEventListener(this._session, 'Network.requestWillBeSent', e => this._onRequestWillBeSent(this._session, e)),
Expand Down Expand Up @@ -483,7 +487,33 @@ export class WKPage implements PageDelegate {
}
return context._createHandle(p);
});
this._page._addConsoleMessage(derivedType, handles, { url, lineNumber: (lineNumber || 1) - 1, columnNumber: (columnNumber || 1) - 1 }, handles.length ? undefined : text);
this._lastConsoleMessage = {
derivedType,
text,
handles,
count: 0,
location: {
url,
lineNumber: (lineNumber || 1) - 1,
columnNumber: (columnNumber || 1) - 1,
}
};
this._onConsoleRepeatCountUpdated({ count: 1});
}

_onConsoleRepeatCountUpdated(event: Protocol.Console.messageRepeatCountUpdatedPayload) {
if (this._lastConsoleMessage) {
const {
derivedType,
text,
handles,
count,
location
} = this._lastConsoleMessage;
for (let i = count; i < event.count; ++i)
this._page._addConsoleMessage(derivedType, handles, location, handles.length ? undefined : text);
this._lastConsoleMessage.count = event.count;
}
}

_onDialog(event: Protocol.Dialog.javascriptDialogOpeningPayload) {
Expand Down
2 changes: 1 addition & 1 deletion src/webkit/wkWorkers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,6 @@ export class WKWorkers {
const handles = (parameters || []).map(p => {
return worker._existingExecutionContext!._createHandle(p);
});
this._page._addConsoleMessage(derivedType, handles, { url, lineNumber: (lineNumber || 1) - 1, columnNumber: (columnNumber || 1) - 1 }, handles.length ? undefined : text);
this._page._addConsoleMessage(derivedType, handles, { url, lineNumber: (lineNumber || 1) - 1, columnNumber: (columnNumber || 1) - 1 });
}
}
6 changes: 6 additions & 0 deletions test/page.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ describe('Page.Events.Console', function() {
expect(await message.args()[1].jsonValue()).toEqual(5);
expect(await message.args()[2].jsonValue()).toEqual({foo: 'bar'});
});
it('should emit same log twice', async({page, server}) => {
const messages = [];
page.on('console', m => messages.push(m.text()));
await page.evaluate(() => { for (let i = 0; i < 2; ++i ) console.log('hello'); } );
expect(messages).toEqual(['hello', 'hello']);
});
it('should work for different console API calls', async({page, server}) => {
const messages = [];
page.on('console', msg => messages.push(msg));
Expand Down

0 comments on commit a5cf03a

Please sign in to comment.