chore: move wsEndpoint and browser target APIs to features/ (#52)

This commit is contained in:
Pavel Feldman 2019-11-22 11:25:53 -08:00 committed by Andrey Lushnikov
parent 508c1960dd
commit b66b005b1f
6 changed files with 39 additions and 45 deletions

View File

@ -41,12 +41,10 @@
* [browser.newPage()](#browsernewpage) * [browser.newPage()](#browsernewpage)
* [browser.pages()](#browserpages) * [browser.pages()](#browserpages)
* [browser.process()](#browserprocess) * [browser.process()](#browserprocess)
* [browser.target()](#browsertarget)
* [browser.targets()](#browsertargets) * [browser.targets()](#browsertargets)
* [browser.userAgent()](#browseruseragent) * [browser.userAgent()](#browseruseragent)
* [browser.version()](#browserversion) * [browser.version()](#browserversion)
* [browser.waitForTarget(predicate[, options])](#browserwaitfortargetpredicate-options) * [browser.waitForTarget(predicate[, options])](#browserwaitfortargetpredicate-options)
* [browser.wsEndpoint()](#browserwsendpoint)
- [class: BrowserContext](#class-browsercontext) - [class: BrowserContext](#class-browsercontext)
* [event: 'targetchanged'](#event-targetchanged-1) * [event: 'targetchanged'](#event-targetchanged-1)
* [event: 'targetcreated'](#event-targetcreated-1) * [event: 'targetcreated'](#event-targetcreated-1)
@ -175,10 +173,12 @@
- [class: PDF](#class-pdf) - [class: PDF](#class-pdf)
* [pdf.generate([options])](#pdfgenerateoptions) * [pdf.generate([options])](#pdfgenerateoptions)
- [class: Chromium](#class-chromium) - [class: Chromium](#class-chromium)
* [chromium.createBrowserCDPSession()](#chromiumcreatebrowsercdpsession)
* [chromium.createCDPSession(target)](#chromiumcreatecdpsessiontarget) * [chromium.createCDPSession(target)](#chromiumcreatecdpsessiontarget)
* [chromium.serviceWorker(target)](#chromiumserviceworkertarget) * [chromium.serviceWorker(target)](#chromiumserviceworkertarget)
* [chromium.startTracing(page, [options])](#chromiumstarttracingpage-options) * [chromium.startTracing(page, [options])](#chromiumstarttracingpage-options)
* [chromium.stopTracing()](#chromiumstoptracing) * [chromium.stopTracing()](#chromiumstoptracing)
* [chromium.wsEndpoint()](#chromiumwsendpoint)
- [class: FileChooser](#class-filechooser) - [class: FileChooser](#class-filechooser)
* [fileChooser.accept(filePaths)](#filechooseracceptfilepaths) * [fileChooser.accept(filePaths)](#filechooseracceptfilepaths)
* [fileChooser.cancel()](#filechoosercancel) * [fileChooser.cancel()](#filechoosercancel)
@ -684,11 +684,6 @@ the method will return an array with all the pages in all browser contexts.
#### browser.process() #### browser.process()
- returns: <?[ChildProcess]> Spawned browser process. Returns `null` if the browser instance was created with [`playwright.connect`](#playwrightconnectoptions) method. - returns: <?[ChildProcess]> Spawned browser process. Returns `null` if the browser instance was created with [`playwright.connect`](#playwrightconnectoptions) method.
#### browser.target()
- returns: <[Target]>
A target associated with the browser.
#### browser.targets() #### browser.targets()
- returns: <[Array]<[Target]>> - returns: <[Array]<[Target]>>
@ -719,14 +714,6 @@ await page.evaluate(() => window.open('https://www.example.com/'));
const newWindowTarget = await browser.waitForTarget(target => target.url() === 'https://www.example.com/'); const newWindowTarget = await browser.waitForTarget(target => target.url() === 'https://www.example.com/');
``` ```
#### browser.wsEndpoint()
- returns: <[string]> Browser websocket url.
Browser websocket endpoint which can be used as an argument to
[playwright.connect](#playwrightconnectoptions). The format is `ws://${host}:${port}/devtools/browser/<id>`
You can find the `webSocketDebuggerUrl` from `http://${host}:${port}/json/version`. Learn more about the [devtools protocol](https://chromedevtools.github.io/devtools-protocol) and the [browser endpoint](https://chromedevtools.github.io/devtools-protocol/#how-do-i-access-the-browser-target).
### class: BrowserContext ### class: BrowserContext
* extends: [EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter) * extends: [EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter)
@ -2408,6 +2395,11 @@ await page.goto('https://www.google.com');
await page.chromium.stopTracing(); await page.chromium.stopTracing();
``` ```
#### chromium.createBrowserCDPSession()
- returns: <[Promise]<[CDPSession]>>
Creates a Chrome Devtools Protocol session attached to the browser.
#### chromium.createCDPSession(target) #### chromium.createCDPSession(target)
- `target` <[Target]> Target to return CDP connection for. - `target` <[Target]> Target to return CDP connection for.
- returns: <[Promise]<[CDPSession]>> - returns: <[Promise]<[CDPSession]>>
@ -2433,6 +2425,14 @@ Only one trace can be active at a time per browser.
#### chromium.stopTracing() #### chromium.stopTracing()
- returns: <[Promise]<[Buffer]>> Promise which resolves to buffer with trace data. - returns: <[Promise]<[Buffer]>> Promise which resolves to buffer with trace data.
#### chromium.wsEndpoint()
- returns: <[string]> Browser websocket url.
Browser websocket endpoint which can be used as an argument to
[playwright.connect](#playwrightconnectoptions). The format is `ws://${host}:${port}/devtools/browser/<id>`
You can find the `webSocketDebuggerUrl` from `http://${host}:${port}/json/version`. Learn more about the [devtools protocol](https://chromedevtools.github.io/devtools-protocol) and the [browser endpoint](https://chromedevtools.github.io/devtools-protocol/#how-do-i-access-the-browser-target).
### class: FileChooser ### class: FileChooser
[FileChooser] objects are returned via the ['page.waitForFileChooser'](#pagewaitforfilechooseroptions) method. [FileChooser] objects are returned via the ['page.waitForFileChooser'](#pagewaitforfilechooseroptions) method.

View File

@ -66,7 +66,7 @@ export class Browser extends EventEmitter {
this._defaultViewport = defaultViewport; this._defaultViewport = defaultViewport;
this._process = process; this._process = process;
this._closeCallback = closeCallback || (() => Promise.resolve()); this._closeCallback = closeCallback || (() => Promise.resolve());
this.chromium = new Chromium(this._client); this.chromium = new Chromium(this._connection, this._client);
this._defaultContext = new BrowserContext(this._client, this, null); this._defaultContext = new BrowserContext(this._client, this, null);
for (const contextId of contextIds) for (const contextId of contextIds)
@ -140,10 +140,6 @@ export class Browser extends EventEmitter {
} }
} }
wsEndpoint(): string {
return this._connection.url();
}
async newPage(): Promise<Page> { async newPage(): Promise<Page> {
return this._defaultContext.newPage(); return this._defaultContext.newPage();
} }
@ -164,10 +160,6 @@ export class Browser extends EventEmitter {
return Array.from(this._targets.values()).filter(target => target._isInitialized); return Array.from(this._targets.values()).filter(target => target._isInitialized);
} }
target(): Target {
return this.targets().find(target => target.type() === 'browser');
}
async waitForTarget(predicate: (arg0: Target) => boolean, options: { timeout?: number; } | undefined = {}): Promise<Target> { async waitForTarget(predicate: (arg0: Target) => boolean, options: { timeout?: number; } | undefined = {}): Promise<Target> {
const { const {
timeout = 30000 timeout = 30000

View File

@ -15,22 +15,28 @@
* limitations under the License. * limitations under the License.
*/ */
import { assert } from '../../helper'; import { assert } from '../../helper';
import { CDPSession } from '../Connection'; import { CDPSession, Connection } from '../Connection';
import { Page } from '../Page'; import { Page } from '../Page';
import { readProtocolStream } from '../protocolHelper'; import { readProtocolStream } from '../protocolHelper';
import { Target } from '../Target'; import { Target } from '../Target';
import { Worker } from './workers'; import { Worker } from './workers';
export class Chromium { export class Chromium {
private _connection: Connection;
private _client: CDPSession; private _client: CDPSession;
private _recording = false; private _recording = false;
private _path = ''; private _path = '';
private _tracingClient: CDPSession | undefined; private _tracingClient: CDPSession | undefined;
constructor(client: CDPSession) { constructor(connection: Connection, client: CDPSession) {
this._connection = connection;
this._client = client; this._client = client;
} }
createBrowserCDPSession(): Promise<CDPSession> {
return this._connection.createBrowserSession();
}
createCDPSession(target: Target): Promise<CDPSession> { createCDPSession(target: Target): Promise<CDPSession> {
return target._sessionFactory(); return target._sessionFactory();
} }
@ -77,4 +83,8 @@ export class Chromium {
this._recording = false; this._recording = false;
return contentPromise; return contentPromise;
} }
wsEndpoint(): string {
return this._connection.url();
}
} }

View File

@ -39,7 +39,6 @@ export class Browser extends EventEmitter {
return browser; return browser;
} }
constructor(connection: Connection, browserContextIds: Array<string>, defaultViewport: Viewport | null, process: import('child_process').ChildProcess | null, closeCallback: () => void) { constructor(connection: Connection, browserContextIds: Array<string>, defaultViewport: Viewport | null, process: import('child_process').ChildProcess | null, closeCallback: () => void) {
super(); super();
this._connection = connection; this._connection = connection;
@ -63,15 +62,10 @@ export class Browser extends EventEmitter {
]; ];
} }
wsEndpoint() {
return this._connection.url();
}
disconnect() { disconnect() {
this._connection.dispose(); this._connection.dispose();
} }
isConnected(): boolean { isConnected(): boolean {
return !this._connection._closed; return !this._connection._closed;
} }
@ -83,7 +77,6 @@ export class Browser extends EventEmitter {
return context; return context;
} }
browserContexts(): Array<BrowserContext> { browserContexts(): Array<BrowserContext> {
return [this._defaultContext, ...Array.from(this._contexts.values())]; return [this._defaultContext, ...Array.from(this._contexts.values())];
} }
@ -97,24 +90,20 @@ export class Browser extends EventEmitter {
this._contexts.delete(browserContextId); this._contexts.delete(browserContextId);
} }
async userAgent(): Promise<string> { async userAgent(): Promise<string> {
const info = await this._connection.send('Browser.getInfo'); const info = await this._connection.send('Browser.getInfo');
return info.userAgent; return info.userAgent;
} }
async version(): Promise<string> { async version(): Promise<string> {
const info = await this._connection.send('Browser.getInfo'); const info = await this._connection.send('Browser.getInfo');
return info.version; return info.version;
} }
process(): import('child_process').ChildProcess | null { process(): import('child_process').ChildProcess | null {
return this._process; return this._process;
} }
async waitForTarget(predicate: (target: Target) => boolean, options: { timeout?: number; } = {}): Promise<Target> { async waitForTarget(predicate: (target: Target) => boolean, options: { timeout?: number; } = {}): Promise<Target> {
const { const {
timeout = 30000 timeout = 30000
@ -141,12 +130,10 @@ export class Browser extends EventEmitter {
} }
} }
newPage(): Promise<Page> { newPage(): Promise<Page> {
return this._createPageInContext(this._defaultContext._browserContextId); return this._createPageInContext(this._defaultContext._browserContextId);
} }
async _createPageInContext(browserContextId: string | null): Promise<Page> { async _createPageInContext(browserContextId: string | null): Promise<Page> {
const {targetId} = await this._connection.send('Target.newPage', { const {targetId} = await this._connection.send('Target.newPage', {
browserContextId: browserContextId || undefined browserContextId: browserContextId || undefined
@ -164,10 +151,6 @@ export class Browser extends EventEmitter {
return Array.from(this._targets.values()); return Array.from(this._targets.values());
} }
target() {
return this.targets().find(target => target.type() === 'browser');
}
async _onTargetCreated({targetId, url, browserContextId, openerId, type}) { async _onTargetCreated({targetId, url, browserContextId, openerId, type}) {
const context = browserContextId ? this._contexts.get(browserContextId) : this._defaultContext; const context = browserContextId ? this._contexts.get(browserContextId) : this._defaultContext;
const target = new Target(this._connection, this, context, targetId, type, url, openerId); const target = new Target(this._connection, this, context, targetId, type, url, openerId);
@ -200,7 +183,7 @@ export class Browser extends EventEmitter {
async close() { async close() {
helper.removeEventListeners(this._eventListeners); helper.removeEventListeners(this._eventListeners);
await this._closeCallback(); this._closeCallback();
} }
} }

View File

@ -180,11 +180,19 @@ export class Browser extends EventEmitter {
newTarget._pagePromise = oldTarget._pagePromise; newTarget._pagePromise = oldTarget._pagePromise;
} }
_onTargetChanged(target) { _onTargetChanged(target: Target) {
this.emit(Events.BrowserContext.TargetChanged, target); this.emit(Events.BrowserContext.TargetChanged, target);
target.browserContext().emit(Events.BrowserContext.TargetChanged, target); target.browserContext().emit(Events.BrowserContext.TargetChanged, target);
} }
disconnect() {
throw new Error('Unsupported operation');
}
isConnected(): boolean {
return true;
}
async close() { async close() {
helper.removeEventListeners(this._eventListeners); helper.removeEventListeners(this._eventListeners);
await this._closeCallback.call(null); await this._closeCallback.call(null);

View File

@ -34,6 +34,7 @@ export const Events = {
TargetCreated: 'targetcreated', TargetCreated: 'targetcreated',
TargetDestroyed: 'targetdestroyed', TargetDestroyed: 'targetdestroyed',
TargetChanged: 'targetchanged', TargetChanged: 'targetchanged',
Disconnected: 'disconnected'
}, },
BrowserContext: { BrowserContext: {