2020-06-25 16:05:36 -07:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2023-07-26 14:11:26 -07:00
|
|
|
import fs from 'fs';
|
2022-09-20 18:41:51 -07:00
|
|
|
import type * as channels from '@protocol/channels';
|
2021-02-11 17:46:54 -08:00
|
|
|
import { BrowserContext, prepareBrowserContextParams } from './browserContext';
|
2022-04-06 13:57:14 -08:00
|
|
|
import type { Page } from './page';
|
2020-06-25 16:05:36 -07:00
|
|
|
import { ChannelOwner } from './channelOwner';
|
2020-07-29 17:26:59 -07:00
|
|
|
import { Events } from './events';
|
2023-03-31 08:57:07 -07:00
|
|
|
import type { LaunchOptions, BrowserContextOptions, HeadersArray } from './types';
|
2023-10-17 21:34:02 -07:00
|
|
|
import { isTargetClosedError } from './errors';
|
2022-04-06 13:57:14 -08:00
|
|
|
import type * as api from '../../types/types';
|
2021-04-02 09:47:14 +08:00
|
|
|
import { CDPSession } from './cdpSession';
|
2021-08-09 18:09:11 -07:00
|
|
|
import type { BrowserType } from './browserType';
|
2023-07-26 14:11:26 -07:00
|
|
|
import { Artifact } from './artifact';
|
|
|
|
import { mkdirIfNeeded } from '../utils';
|
2020-06-25 16:05:36 -07:00
|
|
|
|
2021-11-17 15:26:01 -08:00
|
|
|
export class Browser extends ChannelOwner<channels.BrowserChannel> implements api.Browser {
|
2020-06-25 16:05:36 -07:00
|
|
|
readonly _contexts = new Set<BrowserContext>();
|
|
|
|
private _isConnected = true;
|
2020-07-09 15:33:01 -07:00
|
|
|
private _closedPromise: Promise<void>;
|
2021-10-14 10:41:03 -07:00
|
|
|
_shouldCloseConnectionOnClose = false;
|
2023-03-16 07:03:33 -07:00
|
|
|
_browserType!: BrowserType;
|
|
|
|
_options: LaunchOptions = {};
|
2021-04-02 09:47:14 +08:00
|
|
|
readonly _name: string;
|
2023-07-26 14:11:26 -07:00
|
|
|
private _path: string | undefined;
|
2020-06-25 16:05:36 -07:00
|
|
|
|
2023-03-31 08:57:07 -07:00
|
|
|
// Used from @playwright/test fixtures.
|
|
|
|
_connectHeaders?: HeadersArray;
|
2023-10-17 15:35:41 -07:00
|
|
|
_closeReason: string | undefined;
|
2023-03-31 08:57:07 -07:00
|
|
|
|
2020-08-24 17:05:16 -07:00
|
|
|
static from(browser: channels.BrowserChannel): Browser {
|
2020-07-01 18:36:09 -07:00
|
|
|
return (browser as any)._object;
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-08-24 17:05:16 -07:00
|
|
|
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.BrowserInitializer) {
|
2020-07-27 10:21:39 -07:00
|
|
|
super(parent, type, guid, initializer);
|
2021-04-02 09:47:14 +08:00
|
|
|
this._name = initializer.name;
|
2020-08-13 13:24:49 -07:00
|
|
|
this._channel.on('close', () => this._didClose());
|
2020-07-09 15:33:01 -07:00
|
|
|
this._closedPromise = new Promise(f => this.once(Events.Browser.Disconnected, f));
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2022-06-06 18:46:08 +02:00
|
|
|
browserType(): BrowserType {
|
|
|
|
return this._browserType;
|
|
|
|
}
|
|
|
|
|
2020-07-29 17:26:59 -07:00
|
|
|
async newContext(options: BrowserContextOptions = {}): Promise<BrowserContext> {
|
2022-07-11 12:10:08 -08:00
|
|
|
return await this._innerNewContext(options, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
async _newContextForReuse(options: BrowserContextOptions = {}): Promise<BrowserContext> {
|
2023-05-05 15:12:18 -07:00
|
|
|
return await this._wrapApiCall(async () => {
|
|
|
|
for (const context of this._contexts) {
|
2023-03-16 07:03:33 -07:00
|
|
|
await this._browserType._willCloseContext(context);
|
2023-05-05 15:12:18 -07:00
|
|
|
for (const page of context.pages())
|
|
|
|
page._onClose();
|
|
|
|
context._onClose();
|
|
|
|
}
|
|
|
|
return await this._innerNewContext(options, true);
|
|
|
|
}, true);
|
2022-07-11 12:10:08 -08:00
|
|
|
}
|
|
|
|
|
2023-10-09 19:55:24 -07:00
|
|
|
async _stopPendingOperations(reason: string) {
|
|
|
|
return await this._wrapApiCall(async () => {
|
|
|
|
await this._channel.stopPendingOperations({ reason });
|
|
|
|
}, true);
|
|
|
|
}
|
|
|
|
|
2022-07-11 12:10:08 -08:00
|
|
|
async _innerNewContext(options: BrowserContextOptions = {}, forReuse: boolean): Promise<BrowserContext> {
|
2021-11-19 16:28:11 -08:00
|
|
|
options = { ...this._browserType._defaultContextOptions, ...options };
|
|
|
|
const contextOptions = await prepareBrowserContextParams(options);
|
2022-07-11 12:10:08 -08:00
|
|
|
const response = forReuse ? await this._channel.newContextForReuse(contextOptions) : await this._channel.newContext(contextOptions);
|
|
|
|
const context = BrowserContext.from(response.context);
|
2023-03-16 07:03:33 -07:00
|
|
|
await this._browserType._didCreateContext(context, contextOptions, this._options, options.logger || this._logger);
|
2021-11-19 16:28:11 -08:00
|
|
|
return context;
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
contexts(): BrowserContext[] {
|
|
|
|
return [...this._contexts];
|
|
|
|
}
|
|
|
|
|
2020-07-27 13:41:35 -07:00
|
|
|
version(): string {
|
|
|
|
return this._initializer.version;
|
|
|
|
}
|
|
|
|
|
2020-07-29 17:26:59 -07:00
|
|
|
async newPage(options: BrowserContextOptions = {}): Promise<Page> {
|
2023-04-28 08:57:43 -07:00
|
|
|
return await this._wrapApiCall(async () => {
|
|
|
|
const context = await this.newContext(options);
|
|
|
|
const page = await context.newPage();
|
|
|
|
page._ownedContext = context;
|
|
|
|
context._ownerPage = page;
|
|
|
|
return page;
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
isConnected(): boolean {
|
|
|
|
return this._isConnected;
|
|
|
|
}
|
|
|
|
|
2021-04-02 09:47:14 +08:00
|
|
|
async newBrowserCDPSession(): Promise<api.CDPSession> {
|
2021-11-19 16:28:11 -08:00
|
|
|
return CDPSession.from((await this._channel.newBrowserCDPSession()).session);
|
2021-04-02 09:47:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async startTracing(page?: Page, options: { path?: string; screenshots?: boolean; categories?: string[]; } = {}) {
|
2023-07-26 14:11:26 -07:00
|
|
|
this._path = options.path;
|
2021-11-19 16:28:11 -08:00
|
|
|
await this._channel.startTracing({ ...options, page: page ? page._channel : undefined });
|
2021-04-02 09:47:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async stopTracing(): Promise<Buffer> {
|
2023-07-26 14:11:26 -07:00
|
|
|
const artifact = Artifact.from((await this._channel.stopTracing()).artifact);
|
|
|
|
const buffer = await artifact.readIntoBuffer();
|
|
|
|
await artifact.delete();
|
|
|
|
if (this._path) {
|
|
|
|
await mkdirIfNeeded(this._path);
|
|
|
|
await fs.promises.writeFile(this._path, buffer);
|
|
|
|
this._path = undefined;
|
|
|
|
}
|
|
|
|
return buffer;
|
2021-04-02 09:47:14 +08:00
|
|
|
}
|
|
|
|
|
2023-10-16 20:32:13 -07:00
|
|
|
async close(options: { reason?: string } = {}): Promise<void> {
|
2023-10-17 15:35:41 -07:00
|
|
|
this._closeReason = options.reason;
|
2020-09-22 12:50:39 -07:00
|
|
|
try {
|
2021-11-19 16:28:11 -08:00
|
|
|
if (this._shouldCloseConnectionOnClose)
|
2023-10-12 11:05:34 -07:00
|
|
|
this._connection.close();
|
2021-11-19 16:28:11 -08:00
|
|
|
else
|
2023-10-16 20:32:13 -07:00
|
|
|
await this._channel.close(options);
|
2021-11-19 16:28:11 -08:00
|
|
|
await this._closedPromise;
|
2020-09-22 12:50:39 -07:00
|
|
|
} catch (e) {
|
2023-10-12 11:05:34 -07:00
|
|
|
if (isTargetClosedError(e))
|
2020-09-22 12:50:39 -07:00
|
|
|
return;
|
|
|
|
throw e;
|
|
|
|
}
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
2020-08-13 13:24:49 -07:00
|
|
|
|
|
|
|
_didClose() {
|
|
|
|
this._isConnected = false;
|
2021-01-22 09:58:31 -08:00
|
|
|
this.emit(Events.Browser.Disconnected, this);
|
2020-08-13 13:24:49 -07:00
|
|
|
}
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|