Skip to content

Commit

Permalink
fix: add parameter to close/crash/disconnected etc events (#5098)
Browse files Browse the repository at this point in the history
  • Loading branch information
yury-s committed Jan 22, 2021
1 parent 018727d commit a4eb121
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 55 deletions.
1 change: 1 addition & 0 deletions docs/src/api/class-browser.md
Expand Up @@ -46,6 +46,7 @@ with sync_playwright() as playwright:
```

## event: Browser.disconnected
- type: <[Browser]>

Emitted when Browser gets disconnected from the browser application. This might happen because of one of the following:
* Browser application is closed or crashed.
Expand Down
1 change: 1 addition & 0 deletions docs/src/api/class-browsercontext.md
Expand Up @@ -40,6 +40,7 @@ context.close()
```

## event: BrowserContext.close
- type: <[BrowserContext]>

Emitted when Browser context gets closed. This might happen because of one of the following:
* Browser context is closed.
Expand Down
4 changes: 4 additions & 0 deletions docs/src/api/class-page.md
Expand Up @@ -89,6 +89,7 @@ page.remove_listener("request", log_request)
```

## event: Page.close
- type: <[Page]>

Emitted when the page closes.

Expand Down Expand Up @@ -129,6 +130,7 @@ page.evaluate("console.log('hello', 5, {foo: 'bar'})")
```

## event: Page.crash
- type: <[Page]>

Emitted when the page crashes. Browser pages might crash if they try to allocate too much memory. When the page crashes,
ongoing and subsequent operations will throw.
Expand Down Expand Up @@ -173,6 +175,7 @@ Emitted when a JavaScript dialog appears, such as `alert`, `prompt`, `confirm` o
to the dialog via [`method: Dialog.accept`] or [`method: Dialog.dismiss`] methods.

## event: Page.domcontentloaded
- type: <[Page]>

Emitted when the JavaScript [`DOMContentLoaded`](https://developer.mozilla.org/en-US/docs/Web/Events/DOMContentLoaded)
event is dispatched.
Expand Down Expand Up @@ -221,6 +224,7 @@ Emitted when a frame is detached.
Emitted when a frame is navigated to a new url.

## event: Page.load
- type: <[Page]>

Emitted when the JavaScript [`load`](https://developer.mozilla.org/en-US/docs/Web/Events/load) event is dispatched.

Expand Down
1 change: 1 addition & 0 deletions docs/src/api/class-websocket.md
Expand Up @@ -3,6 +3,7 @@
The [WebSocket] class represents websocket connections in the page.

## event: WebSocket.close
- type: <[WebSocket]>

Fired when the websocket closes.

Expand Down
2 changes: 1 addition & 1 deletion src/client/browser.ts
Expand Up @@ -91,6 +91,6 @@ export class Browser extends ChannelOwner<channels.BrowserChannel, channels.Brow

_didClose() {
this._isConnected = false;
this.emit(Events.Browser.Disconnected);
this.emit(Events.Browser.Disconnected, this);
}
}
2 changes: 1 addition & 1 deletion src/client/browserContext.ts
Expand Up @@ -239,7 +239,7 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel,
async _onClose() {
if (this._browser)
this._browser._contexts.delete(this);
this.emit(Events.BrowserContext.Close);
this.emit(Events.BrowserContext.Close, this);
}

async close(): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion src/client/network.ts
Expand Up @@ -350,7 +350,7 @@ export class WebSocket extends ChannelOwner<channels.WebSocketChannel, channels.
this._channel.on('socketError', ({ error }) => this.emit(Events.WebSocket.Error, error));
this._channel.on('close', () => {
this._isClosed = true;
this.emit(Events.WebSocket.Close);
this.emit(Events.WebSocket.Close, this);
});
}

Expand Down
8 changes: 4 additions & 4 deletions src/client/page.ts
Expand Up @@ -117,12 +117,12 @@ export class Page extends ChannelOwner<channels.PageChannel, channels.PageInitia
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('domcontentloaded', () => this.emit(Events.Page.DOMContentLoaded, this));
this._channel.on('download', ({ download }) => this.emit(Events.Page.Download, Download.from(download)));
this._channel.on('fileChooser', ({ element, isMultiple }) => this.emit(Events.Page.FileChooser, new FileChooser(this, ElementHandle.from(element), isMultiple)));
this._channel.on('frameAttached', ({ frame }) => this._onFrameAttached(Frame.from(frame)));
this._channel.on('frameDetached', ({ frame }) => this._onFrameDetached(Frame.from(frame)));
this._channel.on('load', () => this.emit(Events.Page.Load));
this._channel.on('load', () => this.emit(Events.Page.Load, this));
this._channel.on('pageError', ({ error }) => this.emit(Events.Page.PageError, parseError(error)));
this._channel.on('popup', ({ page }) => this.emit(Events.Page.Popup, Page.from(page)));
this._channel.on('request', ({ request }) => this.emit(Events.Page.Request, Request.from(request)));
Expand Down Expand Up @@ -199,11 +199,11 @@ export class Page extends ChannelOwner<channels.PageChannel, channels.PageInitia
_onClose() {
this._closed = true;
this._browserContext._pages.delete(this);
this.emit(Events.Page.Close);
this.emit(Events.Page.Close, this);
}

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

context(): BrowserContext {
Expand Down
9 changes: 9 additions & 0 deletions test/browsercontext-basic.spec.ts
Expand Up @@ -141,6 +141,15 @@ it('close() should be callable twice', async ({browser}) => {
await context.close();
});

it('should pass self to close event', async ({browser}) => {
const newContext = await browser.newContext();
const [closedContext] = await Promise.all([
newContext.waitForEvent('close'),
newContext.close()
]);
expect(closedContext).toBe(newContext);
});

it('should not report frameless pages on error', async ({browser, server}) => {
const context = await browser.newContext();
const page = await context.newPage();
Expand Down
9 changes: 9 additions & 0 deletions test/browsertype-connect.spec.ts
Expand Up @@ -85,6 +85,15 @@ describe('connect', (suite, { mode }) => {
expect(disconnected2).toBe(1);
});

it('disconnected event should have browser as argument', async ({browserType, remoteServer}) => {
const browser = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint() });
const [disconnected] = await Promise.all([
new Promise(f => browser.on('disconnected', f)),
browser.close(),
]);
expect(disconnected).toBe(browser);
});

it('should handle exceptions during connect', async ({browserType, remoteServer}) => {
const __testHookBeforeCreateBrowser = () => { throw new Error('Dummy'); };
const error = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint(), __testHookBeforeCreateBrowser } as any).catch(e => e);
Expand Down
25 changes: 25 additions & 0 deletions test/page-basic.spec.ts
Expand Up @@ -41,6 +41,15 @@ it('should set the page close state', async ({context}) => {
expect(newPage.isClosed()).toBe(true);
});

it('should pass page to close event', async ({context}) => {
const newPage = await context.newPage();
const [closedPage] = await Promise.all([
newPage.waitForEvent('close'),
newPage.close()
]);
expect(closedPage).toBe(newPage);
});

it('should terminate network waiters', async ({context, server}) => {
const newPage = await context.newPage();
const results = await Promise.all([
Expand Down Expand Up @@ -106,6 +115,22 @@ it('should fire domcontentloaded when expected', async ({page, server}) => {
await navigatedPromise;
});

it('should pass self as argument to domcontentloaded event', async ({page, server}) => {
const [eventArg] = await Promise.all([
new Promise(f => page.on('domcontentloaded', f)),
page.goto('about:blank')
]);
expect(eventArg).toBe(page);
});

it('should pass self as argument to load event', async ({page, server}) => {
const [eventArg] = await Promise.all([
new Promise(f => page.on('load', f)),
page.goto('about:blank')
]);
expect(eventArg).toBe(page);
});

it('should fail with error upon disconnect', async ({page, server}) => {
let error;
const waitForPromise = page.waitForEvent('download').catch(e => error = e);
Expand Down
3 changes: 2 additions & 1 deletion test/page-event-crash.spec.ts
Expand Up @@ -36,7 +36,8 @@ describe('', (suite, { browserName, platform, mode }) => {
it('should emit crash event when page crashes', async ({page, browserName, toImpl}) => {
await page.setContent(`<div>This page should crash</div>`);
crash(page, toImpl, browserName);
await new Promise(f => page.on('crash', f));
const crashedPage = await new Promise(f => page.on('crash', f));
expect(crashedPage).toBe(page);
});

it('should throw on any action after page crashes', async ({page, browserName, toImpl}) => {
Expand Down
16 changes: 16 additions & 0 deletions test/web-socket.spec.ts
Expand Up @@ -69,6 +69,22 @@ it('should emit frame events', async ({ page, server, isFirefox }) => {
expect(log.join(':')).toBe('close:open:received<incoming>:sent<outgoing>');
});

it('should pass self as argument to close event', async ({ page, server, isFirefox }) => {
let socketClosed;
const socketClosePromise = new Promise(f => socketClosed = f);
let webSocket;
page.on('websocket', ws => {
webSocket = ws;
ws.on('close', socketClosed);
});
await page.evaluate(port => {
const ws = new WebSocket('ws://localhost:' + port + '/ws');
ws.addEventListener('open', () => ws.close());
}, server.PORT);
const eventArg = await socketClosePromise;
expect(eventArg).toBe(webSocket);
});

it('should emit binary frame events', async ({ page, server }) => {
let doneCallback;
const donePromise = new Promise(f => doneCallback = f);
Expand Down

0 comments on commit a4eb121

Please sign in to comment.