Skip to content

Commit

Permalink
chore(rpc): clear the page test spec
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman committed Jun 27, 2020
1 parent 6393407 commit eab5a89
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 29 deletions.
3 changes: 3 additions & 0 deletions src/rpc/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,15 @@ export interface PageChannel extends Channel {
on(event: 'bindingCall', callback: (params: BindingCallChannel) => void): this;
on(event: 'close', callback: () => void): this;
on(event: 'console', callback: (params: ConsoleMessageChannel) => void): this;
on(event: 'crash', callback: () => void): this;
on(event: 'dialog', callback: (params: DialogChannel) => void): this;
on(event: 'download', callback: (params: DownloadChannel) => void): this;
on(event: 'domcontentloaded', callback: () => void): this;
on(event: 'frameAttached', callback: (params: FrameChannel) => void): this;
on(event: 'frameDetached', callback: (params: FrameChannel) => void): this;
on(event: 'frameNavigated', callback: (params: { frame: FrameChannel, url: string, name: string }) => void): this;
on(event: 'frameNavigated', callback: (params: { frame: FrameChannel, url: string, name: string }) => void): this;
on(event: 'load', callback: () => void): this;
on(event: 'pageError', callback: (params: { error: types.Error }) => void): this;
on(event: 'popup', callback: (params: PageChannel) => void): this;
on(event: 'request', callback: (params: RequestChannel) => void): this;
Expand Down
10 changes: 7 additions & 3 deletions src/rpc/client/browserContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ import { helper } from '../../helper';
import { Browser } from './browser';
import { Connection } from '../connection';
import { Events } from '../../events';
import { TimeoutSettings } from '../../timeoutSettings';

export class BrowserContext extends ChannelOwner<BrowserContextChannel, BrowserContextInitializer> {
_pages = new Set<Page>();
private _routes: { url: types.URLMatch, handler: network.RouteHandler }[] = [];
_browser: Browser | undefined;
readonly _bindings = new Map<string, frames.FunctionWithSource>();
_timeoutSettings = new TimeoutSettings();

static from(context: BrowserContextChannel): BrowserContext {
return context._object;
Expand All @@ -45,13 +47,13 @@ export class BrowserContext extends ChannelOwner<BrowserContextChannel, BrowserC
initializer.pages.map(p => {
const page = Page.from(p);
this._pages.add(page);
page._browserContext = this;
page._setBrowserContext(this);
});
channel.on('page', page => this._onPage(Page.from(page)));
}

private _onPage(page: Page): void {
page._browserContext = this;
page._setBrowserContext(this);
this._pages.add(page);
this.emit(Events.BrowserContext.Page, page);
}
Expand All @@ -78,6 +80,7 @@ export class BrowserContext extends ChannelOwner<BrowserContextChannel, BrowserC
}

setDefaultTimeout(timeout: number) {
this._timeoutSettings.setDefaultTimeout(timeout);
this._channel.setDefaultTimeoutNoReply({ timeout });
}

Expand Down Expand Up @@ -162,7 +165,8 @@ export class BrowserContext extends ChannelOwner<BrowserContextChannel, BrowserC
}

async waitForEvent(event: string, optionsOrPredicate?: Function | (types.TimeoutOptions & { predicate?: Function })): Promise<any> {
return waitForEvent(this, event, optionsOrPredicate);
const hasTimeout = optionsOrPredicate && !(optionsOrPredicate instanceof Function);
return waitForEvent(this, event, optionsOrPredicate, this._timeoutSettings.timeout(hasTimeout ? optionsOrPredicate as any : {}));
}

async close(): Promise<void> {
Expand Down
63 changes: 54 additions & 9 deletions src/rpc/client/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ import { Accessibility } from './accessibility';
import { ConsoleMessage } from './consoleMessage';
import { Dialog } from './dialog';
import { Download } from './download';
import { TimeoutError } from '../../errors';
import { TimeoutSettings } from '../../timeoutSettings';

export class Page extends ChannelOwner<PageChannel, PageInitializer> {
readonly pdf: ((options?: types.PDFOptions) => Promise<Buffer>) | undefined;

_browserContext: BrowserContext | undefined;
private _browserContext: BrowserContext | undefined;
_ownedContext: BrowserContext | undefined;

private _mainFrame: Frame;
Expand All @@ -50,6 +52,8 @@ export class Page extends ChannelOwner<PageChannel, PageInitializer> {
readonly keyboard: Keyboard;
readonly mouse: Mouse;
readonly _bindings = new Map<string, FunctionWithSource>();
private _pendingWaitForEvents = new Map<(error: Error) => void, string>();
private _timeoutSettings = new TimeoutSettings();

static from(page: PageChannel): Page {
return page._object;
Expand All @@ -73,11 +77,14 @@ export class Page extends ChannelOwner<PageChannel, PageInitializer> {
this._channel.on('bindingCall', bindingCall => this._onBinding(BindingCall.from(bindingCall)));
this._channel.on('close', () => this._onClose());
this._channel.on('console', message => this.emit(Events.Page.Console, ConsoleMessage.from(message)));
this._channel.on('crash', () => this._onCrash());
this._channel.on('dialog', dialog => this.emit(Events.Page.Dialog, Dialog.from(dialog)));
this._channel.on('domcontentloaded', () => this.emit(Events.Page.DOMContentLoaded));
this._channel.on('download', download => this.emit(Events.Page.Download, Download.from(download)));
this._channel.on('frameAttached', frame => this._onFrameAttached(Frame.from(frame)));
this._channel.on('frameDetached', frame => this._onFrameDetached(Frame.from(frame)));
this._channel.on('frameNavigated', ({ frame, url, name }) => this._onFrameNavigated(Frame.from(frame), url, name));
this._channel.on('load', () => this.emit(Events.Page.Load));
this._channel.on('pageError', ({ error }) => this.emit(Events.Page.PageError, parseError(error)));
this._channel.on('popup', popup => this.emit(Events.Page.Popup, Page.from(popup)));
this._channel.on('request', request => this.emit(Events.Page.Request, Request.from(request)));
Expand All @@ -87,6 +94,11 @@ export class Page extends ChannelOwner<PageChannel, PageInitializer> {
this._channel.on('route', ({ route, request }) => this._onRoute(Route.from(route), Request.from(request)));
}

_setBrowserContext(context: BrowserContext) {
this._browserContext = context;
this._timeoutSettings = new TimeoutSettings(context._timeoutSettings);
}

private _onRequestFailed(request: Request, failureText: string | null) {
request._failureText = failureText;
this.emit(Events.Page.RequestFailed, request);
Expand Down Expand Up @@ -132,10 +144,28 @@ export class Page extends ChannelOwner<PageChannel, PageInitializer> {
}

private _onClose() {
this._closed = true;
this._browserContext!._pages.delete(this);
this._rejectPendingOperations(false);
this.emit(Events.Page.Close);
}

private _onCrash() {
this._rejectPendingOperations(true);
this.emit(Events.Page.Crash);
}

private _rejectPendingOperations(isCrash: boolean) {
for (const [listener, event] of this._pendingWaitForEvents) {
if (event === Events.Page.Close && !isCrash)
continue;
if (event === Events.Page.Crash && isCrash)
continue;
listener(new Error(isCrash ? 'Page crashed' : 'Page closed'));
}
this._pendingWaitForEvents.clear();
}

context(): BrowserContext {
return this._browserContext!;
}
Expand Down Expand Up @@ -168,6 +198,7 @@ export class Page extends ChannelOwner<PageChannel, PageInitializer> {
}

setDefaultTimeout(timeout: number) {
this._timeoutSettings.setDefaultTimeout(timeout);
this._channel.setDefaultTimeoutNoReply({ timeout });
}

Expand Down Expand Up @@ -280,7 +311,13 @@ export class Page extends ChannelOwner<PageChannel, PageInitializer> {
}

async waitForEvent(event: string, optionsOrPredicate: types.WaitForEventOptions = {}): Promise<any> {
return waitForEvent(this, event, optionsOrPredicate);
let reject: () => void;
const result = await Promise.race([
waitForEvent(this, event, optionsOrPredicate, this._timeoutSettings.timeout(optionsOrPredicate instanceof Function ? {} : optionsOrPredicate)),
new Promise((f, r) => { reject = r; this._pendingWaitForEvents.set(reject, event); })
]);
this._pendingWaitForEvents.delete(reject!);
return result;
}

async goBack(options?: types.NavigateOptions): Promise<Response | null> {
Expand Down Expand Up @@ -477,7 +514,7 @@ export class BindingCall extends ChannelOwner<BindingCallChannel, BindingCallIni
try {
const frame = Frame.from(this._initializer.frame);
const source = {
context: frame._page!._browserContext!,
context: frame._page!.context(),
page: frame._page!,
frame
};
Expand All @@ -488,22 +525,30 @@ export class BindingCall extends ChannelOwner<BindingCallChannel, BindingCallIni
}
}

export async function waitForEvent(emitter: EventEmitter, event: string, optionsOrPredicate: types.WaitForEventOptions = {}): Promise<any> {
// TODO: support timeout
export async function waitForEvent(emitter: EventEmitter, event: string, optionsOrPredicate: types.WaitForEventOptions = {}, defaultTimeout: number): Promise<any> {
let predicate: Function | undefined;
if (typeof optionsOrPredicate === 'function')
let timeout = defaultTimeout;
if (typeof optionsOrPredicate === 'function') {
predicate = optionsOrPredicate;
else if (optionsOrPredicate.predicate)
} else if (optionsOrPredicate.predicate) {
if (optionsOrPredicate.timeout !== undefined)
timeout = optionsOrPredicate.timeout;
predicate = optionsOrPredicate.predicate;
}
let callback: (a: any) => void;
const result = new Promise(f => callback = f);
const listener = helper.addEventListener(emitter, event, param => {
// TODO: do not detect channel by guid.
const object = param._guid ? (param as Channel)._object : param;
const object = param && param._guid ? (param as Channel)._object : param;
if (predicate && !predicate(object))
return;
callback(object);
helper.removeEventListeners([listener]);
});
return result;
if (timeout === 0)
return result;
return Promise.race([
result,
new Promise((f, r) => setTimeout(() => r(new TimeoutError('Timeout while waiting for event')), timeout))
]);
}
2 changes: 1 addition & 1 deletion src/rpc/server/browserContextDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,6 @@ export class BrowserContextDispatcher extends Dispatcher<BrowserContext, Browser
}

async close(): Promise<void> {
this._context.close();
await this._context.close();
}
}
3 changes: 3 additions & 0 deletions src/rpc/server/pageDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ export class PageDispatcher extends Dispatcher<Page, PageInitializer> implements
this._page = page;
page.on(Events.Page.Close, () => this._dispatchEvent('close'));
page.on(Events.Page.Console, message => this._dispatchEvent('console', ConsoleMessageDispatcher.from(this._scope, message)));
page.on(Events.Page.Crash, () => this._dispatchEvent('crash'));
page.on(Events.Page.DOMContentLoaded, () => this._dispatchEvent('domcontentloaded'));
page.on(Events.Page.Dialog, dialog => this._dispatchEvent('dialog', DialogDispatcher.from(this._scope, dialog)));
page.on(Events.Page.Download, dialog => this._dispatchEvent('download', DownloadDispatcher.from(this._scope, dialog)));
page.on(Events.Page.FrameAttached, frame => this._onFrameAttached(frame));
page.on(Events.Page.FrameDetached, frame => this._onFrameDetached(frame));
page.on(Events.Page.FrameNavigated, frame => this._onFrameNavigated(frame));
page.on(Events.Page.Load, () => this._dispatchEvent('load'));
page.on(Events.Page.PageError, error => this._dispatchEvent('pageError', { error: serializeError(error) }));
page.on(Events.Page.Popup, page => this._dispatchEvent('popup', PageDispatcher.from(this._scope, page)));
page.on(Events.Page.Request, request => this._dispatchEvent('request', RequestDispatcher.from(this._scope, request)));
Expand Down
26 changes: 10 additions & 16 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,6 @@ function collect(browserNames) {

global.playwright = playwright;

// Channel substitute
let connection;
let dispatcherScope;
if (process.env.PWCHANNEL) {
dispatcherScope = new DispatcherScope();
connection = new Connection();
dispatcherScope.sendMessageToClientTransport = async message => {
setImmediate(() => connection.dispatchMessageFromServer(message));
};
connection.sendMessageToServerTransport = async message => {
const result = await dispatcherScope.dispatchMessageFromClient(message);
await new Promise(f => setImmediate(f));
return result;
};
}

for (const browserName of browserNames) {
const browserType = playwright[browserName];

Expand All @@ -119,6 +103,16 @@ function collect(browserNames) {
// Channel substitute
let overridenBrowserType = browserType;
if (process.env.PWCHANNEL) {
const dispatcherScope = new DispatcherScope();
const connection = new Connection();
dispatcherScope.sendMessageToClientTransport = async message => {
setImmediate(() => connection.dispatchMessageFromServer(message));
};
connection.sendMessageToServerTransport = async message => {
const result = await dispatcherScope.dispatchMessageFromClient(message);
await new Promise(f => setImmediate(f));
return result;
};
BrowserTypeDispatcher.from(dispatcherScope, browserType);
overridenBrowserType = await connection.waitForObjectWithKnownName(browserType.name());
}
Expand Down

0 comments on commit eab5a89

Please sign in to comment.