Skip to content

Commit

Permalink
feat(trace): record goto, setContent, goBack, goForward and reload (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dgozman authored Sep 15, 2020
1 parent 8bc09af commit 592bae1
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 38 deletions.
16 changes: 11 additions & 5 deletions src/dispatchers/frameDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { ElementHandleDispatcher, createHandle } from './elementHandlerDispatche
import { parseArgument, serializeResult } from './jsHandleDispatcher';
import { ResponseDispatcher, RequestDispatcher } from './networkDispatchers';
import { ActionMetadata } from '../server/instrumentation';
import { runAbortableTask } from '../server/progress';
import { ProgressController, runAbortableTask } from '../server/progress';

export class FrameDispatcher extends Dispatcher<Frame, channels.FrameInitializer> implements channels.FrameChannel {
private _frame: Frame;
Expand Down Expand Up @@ -53,8 +53,11 @@ export class FrameDispatcher extends Dispatcher<Frame, channels.FrameInitializer
});
}

async goto(params: channels.FrameGotoParams): Promise<channels.FrameGotoResult> {
return { response: lookupNullableDispatcher<ResponseDispatcher>(await this._frame.goto(params.url, params)) };
async goto(params: channels.FrameGotoParams, metadata?: channels.Metadata): Promise<channels.FrameGotoResult> {
const page = this._frame._page;
const actionMetadata: ActionMetadata = { ...metadata, type: 'goto', value: params.url, page };
const controller = new ProgressController(page._timeoutSettings.navigationTimeout(params), actionMetadata);
return { response: lookupNullableDispatcher<ResponseDispatcher>(await this._frame.goto(controller, params.url, params)) };
}

async frameElement(): Promise<channels.FrameFrameElementResult> {
Expand Down Expand Up @@ -98,8 +101,11 @@ export class FrameDispatcher extends Dispatcher<Frame, channels.FrameInitializer
return { value: await this._frame.content() };
}

async setContent(params: channels.FrameSetContentParams): Promise<void> {
await this._frame.setContent(params.html, params);
async setContent(params: channels.FrameSetContentParams, metadata?: channels.Metadata): Promise<void> {
const page = this._frame._page;
const actionMetadata: ActionMetadata = { ...metadata, type: 'setContent', value: params.html, page };
const controller = new ProgressController(page._timeoutSettings.navigationTimeout(params), actionMetadata);
return await this._frame.setContent(controller, params.html, params);
}

async addScriptTag(params: channels.FrameAddScriptTagParams): Promise<channels.FrameAddScriptTagResult> {
Expand Down
20 changes: 14 additions & 6 deletions src/dispatchers/pageDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import { ElementHandleDispatcher, createHandle } from './elementHandlerDispatche
import { FileChooser } from '../server/fileChooser';
import { CRCoverage } from '../server/chromium/crCoverage';
import { VideoDispatcher } from './videoDispatcher';
import { ActionMetadata } from '../server/instrumentation';
import { ProgressController } from '../server/progress';

export class PageDispatcher extends Dispatcher<Page, channels.PageInitializer> implements channels.PageChannel {
private _page: Page;
Expand Down Expand Up @@ -94,16 +96,22 @@ export class PageDispatcher extends Dispatcher<Page, channels.PageInitializer> i
await this._page.setExtraHTTPHeaders(params.headers);
}

async reload(params: channels.PageReloadParams): Promise<channels.PageReloadResult> {
return { response: lookupNullableDispatcher<ResponseDispatcher>(await this._page.reload(params)) };
async reload(params: channels.PageReloadParams, metadata?: channels.Metadata): Promise<channels.PageReloadResult> {
const actionMetadata: ActionMetadata = { ...metadata, type: 'reload', page: this._page };
const controller = new ProgressController(this._page._timeoutSettings.navigationTimeout(params), actionMetadata);
return { response: lookupNullableDispatcher<ResponseDispatcher>(await this._page.reload(controller, params)) };
}

async goBack(params: channels.PageGoBackParams): Promise<channels.PageGoBackResult> {
return { response: lookupNullableDispatcher<ResponseDispatcher>(await this._page.goBack(params)) };
async goBack(params: channels.PageGoBackParams, metadata?: channels.Metadata): Promise<channels.PageGoBackResult> {
const actionMetadata: ActionMetadata = { ...metadata, type: 'goBack', page: this._page };
const controller = new ProgressController(this._page._timeoutSettings.navigationTimeout(params), actionMetadata);
return { response: lookupNullableDispatcher<ResponseDispatcher>(await this._page.goBack(controller, params)) };
}

async goForward(params: channels.PageGoForwardParams): Promise<channels.PageGoForwardResult> {
return { response: lookupNullableDispatcher<ResponseDispatcher>(await this._page.goForward(params)) };
async goForward(params: channels.PageGoForwardParams, metadata?: channels.Metadata): Promise<channels.PageGoForwardResult> {
const actionMetadata: ActionMetadata = { ...metadata, type: 'goForward', page: this._page };
const controller = new ProgressController(this._page._timeoutSettings.navigationTimeout(params), actionMetadata);
return { response: lookupNullableDispatcher<ResponseDispatcher>(await this._page.goForward(controller, params)) };
}

async emulateMedia(params: channels.PageEmulateMediaParams): Promise<void> {
Expand Down
6 changes: 2 additions & 4 deletions src/server/frames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,7 @@ export class Frame extends EventEmitter {
this._detachedPromise.then(() => controller.abort(new Error('Navigating frame was detached!')));
}

async goto(url: string, options: types.GotoOptions = {}): Promise<network.Response | null> {
const controller = new ProgressController(this._page._timeoutSettings.navigationTimeout(options));
async goto(controller: ProgressController, url: string, options: types.GotoOptions = {}): Promise<network.Response | null> {
this.setupNavigationProgressController(controller);
return controller.run(async progress => {
const waitUntil = verifyLifecycle('waitUntil', options.waitUntil === undefined ? 'load' : options.waitUntil);
Expand Down Expand Up @@ -607,8 +606,7 @@ export class Frame extends EventEmitter {
});
}

async setContent(html: string, options: types.NavigateOptions = {}): Promise<void> {
const controller = new ProgressController(this._page._timeoutSettings.navigationTimeout(options));
async setContent(controller: ProgressController, html: string, options: types.NavigateOptions = {}): Promise<void> {
this.setupNavigationProgressController(controller);
return controller.run(async progress => {
const waitUntil = options.waitUntil === undefined ? 'load' : options.waitUntil;
Expand Down
4 changes: 2 additions & 2 deletions src/server/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import type { ElementHandle } from './dom';
import type { Page } from './page';

export type ActionMetadata = {
type: 'click' | 'fill' | 'dblclick' | 'hover' | 'selectOption' | 'setInputFiles' | 'type' | 'press' | 'check' | 'uncheck',
type: 'click' | 'fill' | 'dblclick' | 'hover' | 'selectOption' | 'setInputFiles' | 'type' | 'press' | 'check' | 'uncheck' | 'goto' | 'setContent' | 'goBack' | 'goForward' | 'reload',
page: Page,
target: ElementHandle | string,
target?: ElementHandle | string,
value?: string,
stack?: string,
};
Expand Down
9 changes: 3 additions & 6 deletions src/server/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,7 @@ export class Page extends EventEmitter {
this.emit(Page.Events.Console, message);
}

async reload(options: types.NavigateOptions = {}): Promise<network.Response | null> {
const controller = new ProgressController(this._timeoutSettings.navigationTimeout(options));
async reload(controller: ProgressController, options: types.NavigateOptions): Promise<network.Response | null> {
this.mainFrame().setupNavigationProgressController(controller);
const response = await controller.run(async progress => {
const waitPromise = this.mainFrame()._waitForNavigation(progress, options);
Expand All @@ -274,8 +273,7 @@ export class Page extends EventEmitter {
return response;
}

async goBack(options: types.NavigateOptions = {}): Promise<network.Response | null> {
const controller = new ProgressController(this._timeoutSettings.navigationTimeout(options));
async goBack(controller: ProgressController, options: types.NavigateOptions): Promise<network.Response | null> {
this.mainFrame().setupNavigationProgressController(controller);
const response = await controller.run(async progress => {
const waitPromise = this.mainFrame()._waitForNavigation(progress, options);
Expand All @@ -290,8 +288,7 @@ export class Page extends EventEmitter {
return response;
}

async goForward(options: types.NavigateOptions = {}): Promise<network.Response | null> {
const controller = new ProgressController(this._timeoutSettings.navigationTimeout(options));
async goForward(controller: ProgressController, options: types.NavigateOptions): Promise<network.Response | null> {
this.mainFrame().setupNavigationProgressController(controller);
const response = await controller.run(async progress => {
const waitPromise = this.mainFrame()._waitForNavigation(progress, options);
Expand Down
9 changes: 3 additions & 6 deletions src/trace/tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { ActionResult, InstrumentingAgent, instrumentingAgents, ActionMetadata }
import { Page } from '../server/page';
import { Snapshotter } from './snapshotter';
import * as types from '../server/types';
import type { ElementHandle } from '../server/dom';
import { ElementHandle } from '../server/dom';
import { helper, RegisteredListener } from '../server/helper';
import { DEFAULT_TIMEOUT } from '../utils/timeoutSettings';

Expand Down Expand Up @@ -144,6 +144,7 @@ class ContextTracer implements SnapshotterDelegate {
type: 'action',
contextId: this._contextId,
action: 'snapshot',
pageId: this._pageToId.get(page),
label: options.label || 'snapshot',
snapshot,
};
Expand All @@ -157,7 +158,7 @@ class ContextTracer implements SnapshotterDelegate {
contextId: this._contextId,
pageId: this._pageToId.get(metadata.page),
action: metadata.type,
target: await this._targetToString(metadata.target),
target: metadata.target instanceof ElementHandle ? await metadata.target._previewPromise : metadata.target,
value: metadata.value,
snapshot,
startTime: result.startTime,
Expand Down Expand Up @@ -193,10 +194,6 @@ class ContextTracer implements SnapshotterDelegate {
});
}

private async _targetToString(target: ElementHandle | string): Promise<string> {
return typeof target === 'string' ? target : await target._previewPromise;
}

private async _takeSnapshot(page: Page, timeout: number = 0): Promise<{ sha1: string, duration: number } | undefined> {
if (!timeout) {
// Never use zero timeout to avoid stalling because of snapshot.
Expand Down
18 changes: 9 additions & 9 deletions test/page-event-crash.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

import { it, expect, describe, options } from './playwright.fixtures';

function crash(pageImpl, browserName) {
function crash(page, toImpl, browserName) {
if (browserName === 'chromium')
pageImpl.mainFrame().goto('chrome://crash').catch(e => {});
page.goto('chrome://crash').catch(e => {});
else if (browserName === 'webkit')
pageImpl._delegate._session.send('Page.crash', {}).catch(e => {});
toImpl(page)._delegate._session.send('Page.crash', {}).catch(e => {});
else if (browserName === 'firefox')
pageImpl._delegate._session.send('Page.crash', {}).catch(e => {});
toImpl(page)._delegate._session.send('Page.crash', {}).catch(e => {});
}

describe('', (suite, parameters) => {
Expand All @@ -32,13 +32,13 @@ describe('', (suite, parameters) => {
}, () => {
it('should emit crash event when page crashes', async ({page, browserName, toImpl}) => {
await page.setContent(`<div>This page should crash</div>`);
crash(toImpl(page), browserName);
crash(page, toImpl, browserName);
await new Promise(f => page.on('crash', f));
});

it('should throw on any action after page crashes', async ({page, browserName, toImpl}) => {
await page.setContent(`<div>This page should crash</div>`);
crash(toImpl(page), browserName);
crash(page, toImpl, browserName);
await page.waitForEvent('crash');
const err = await page.evaluate(() => {}).then(() => null, e => e);
expect(err).toBeTruthy();
Expand All @@ -48,7 +48,7 @@ describe('', (suite, parameters) => {
it('should cancel waitForEvent when page crashes', async ({page, browserName, toImpl}) => {
await page.setContent(`<div>This page should crash</div>`);
const promise = page.waitForEvent('response').catch(e => e);
crash(toImpl(page), browserName);
crash(page, toImpl, browserName);
const error = await promise;
expect(error.message).toContain('Page crashed');
});
Expand All @@ -58,7 +58,7 @@ describe('', (suite, parameters) => {
server.setRoute('/one-style.css', () => {});
const promise = page.goto(server.PREFIX + '/one-style.html').catch(e => e);
await page.waitForNavigation({ waitUntil: 'domcontentloaded' });
crash(toImpl(page), browserName);
crash(page, toImpl, browserName);
const error = await promise;
expect(error.message).toContain('Navigation failed because page crashed');
});
Expand All @@ -68,7 +68,7 @@ describe('', (suite, parameters) => {
test.flaky(options.FIREFOX(parameters) && WIN);
}, async ({page, browserName, toImpl}) => {
await page.setContent(`<div>This page should crash</div>`);
crash(toImpl(page), browserName);
crash(page, toImpl, browserName);
await page.waitForEvent('crash');
await page.context().close();
});
Expand Down

0 comments on commit 592bae1

Please sign in to comment.