playwright/src/webkit/wkBrowser.ts

343 lines
14 KiB
TypeScript
Raw Normal View History

2019-12-19 16:53:24 -08:00
/**
* Copyright 2017 Google Inc. All rights reserved.
* Modifications 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.
*/
import { BrowserBase, BrowserOptions } from '../browser';
import { assertBrowserContextIsNotOwned, BrowserContext, BrowserContextBase, BrowserContextOptions, validateBrowserContextOptions, verifyGeolocation } from '../browserContext';
import { Events } from '../events';
import { helper, RegisteredListener } from '../helper';
2019-12-19 16:53:24 -08:00
import * as network from '../network';
import { Page, PageBinding } from '../page';
import { ConnectionTransport, SlowMoTransport } from '../transport';
import * as types from '../types';
import { Protocol } from './protocol';
import { kPageProxyMessageReceived, PageProxyMessageReceivedPayload, WKConnection, WKSession } from './wkConnection';
import { WKPage } from './wkPage';
2019-12-19 16:53:24 -08:00
const DEFAULT_USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.2 Safari/605.1.15';
export class WKBrowser extends BrowserBase {
private readonly _connection: WKConnection;
readonly _browserSession: WKSession;
readonly _defaultContext: WKBrowserContext | null = null;
readonly _contexts = new Map<string, WKBrowserContext>();
readonly _wkPages = new Map<string, WKPage>();
private readonly _eventListeners: RegisteredListener[];
static async connect(transport: ConnectionTransport, options: BrowserOptions): Promise<WKBrowser> {
const browser = new WKBrowser(SlowMoTransport.wrap(transport, options.slowMo), options);
// TODO: add Playwright.enable to test connection.
return browser;
}
constructor(transport: ConnectionTransport, options: BrowserOptions) {
super(options);
this._connection = new WKConnection(transport, options.logger, this._onDisconnect.bind(this));
this._browserSession = this._connection.browserSession;
2019-12-19 16:53:24 -08:00
if (options.persistent)
this._defaultContext = new WKBrowserContext(this, undefined, validateBrowserContextOptions({}));
2019-12-19 16:53:24 -08:00
this._eventListeners = [
helper.addEventListener(this._browserSession, 'Playwright.pageProxyCreated', this._onPageProxyCreated.bind(this)),
helper.addEventListener(this._browserSession, 'Playwright.pageProxyDestroyed', this._onPageProxyDestroyed.bind(this)),
helper.addEventListener(this._browserSession, 'Playwright.provisionalLoadFailed', event => this._onProvisionalLoadFailed(event)),
helper.addEventListener(this._browserSession, 'Playwright.downloadCreated', this._onDownloadCreated.bind(this)),
helper.addEventListener(this._browserSession, 'Playwright.downloadFilenameSuggested', this._onDownloadFilenameSuggested.bind(this)),
helper.addEventListener(this._browserSession, 'Playwright.downloadFinished', this._onDownloadFinished.bind(this)),
helper.addEventListener(this._browserSession, kPageProxyMessageReceived, this._onPageProxyMessageReceived.bind(this)),
2019-12-19 16:53:24 -08:00
];
}
_onDisconnect() {
for (const wkPage of this._wkPages.values())
wkPage.dispose();
for (const context of this._contexts.values())
context._browserClosed();
// Note: previous method uses pages to issue 'close' event on them, so we clear them after.
this._wkPages.clear();
this.emit(Events.Browser.Disconnected);
}
2019-12-19 16:53:24 -08:00
async newContext(options: BrowserContextOptions = {}): Promise<BrowserContext> {
options = validateBrowserContextOptions(options);
const { browserContextId } = await this._browserSession.send('Playwright.createContext');
options.userAgent = options.userAgent || DEFAULT_USER_AGENT;
const context = new WKBrowserContext(this, browserContextId, options);
await context._initialize();
2019-12-19 16:53:24 -08:00
this._contexts.set(browserContextId, context);
return context;
}
contexts(): BrowserContext[] {
return Array.from(this._contexts.values());
2019-12-19 16:53:24 -08:00
}
_onDownloadCreated(payload: Protocol.Playwright.downloadCreatedPayload) {
const page = this._wkPages.get(payload.pageProxyId);
if (!page)
return;
2020-04-23 20:04:19 -07:00
const frameManager = page._page._frameManager;
const frame = frameManager.frame(payload.frameId);
if (frame) {
// In some cases, e.g. blob url download, we receive only frameScheduledNavigation
// but no signals that the navigation was canceled and replaced by download. Fix it
// here by simulating cancelled provisional load which matches downloads from network.
frameManager.provisionalLoadFailed(frame, '', 'Download is starting');
}
let originPage = page._initializedPage;
// If it's a new window download, report it on the opener page.
if (!originPage) {
// Resume the page creation with an error. The page will automatically close right
// after the download begins.
page._firstNonInitialNavigationCommittedReject(new Error('Starting new page download'));
if (page._opener)
originPage = page._opener._initializedPage;
}
if (!originPage)
return;
this._downloadCreated(originPage, payload.uuid, payload.url);
}
_onDownloadFilenameSuggested(payload: Protocol.Playwright.downloadFilenameSuggestedPayload) {
this._downloadFilenameSuggested(payload.uuid, payload.suggestedFilename);
}
_onDownloadFinished(payload: Protocol.Playwright.downloadFinishedPayload) {
this._downloadFinished(payload.uuid, payload.error);
}
_onPageProxyCreated(event: Protocol.Playwright.pageProxyCreatedPayload) {
const { pageProxyInfo } = event;
const pageProxyId = pageProxyInfo.pageProxyId;
let context: WKBrowserContext | null = null;
if (pageProxyInfo.browserContextId) {
2019-12-19 16:53:24 -08:00
// FIXME: we don't know about the default context id, so assume that all targets from
// unknown contexts are created in the 'default' context which can in practice be represented
// by multiple actual contexts in WebKit. Solving this properly will require adding context
// lifecycle events.
context = this._contexts.get(pageProxyInfo.browserContextId) || null;
2019-12-19 16:53:24 -08:00
}
if (!context)
context = this._defaultContext;
if (!context)
return;
const pageProxySession = new WKSession(this._connection, pageProxyId, `The page has been closed.`, (message: any) => {
this._connection.rawSend({ ...message, pageProxyId });
});
const opener = pageProxyInfo.openerId ? this._wkPages.get(pageProxyInfo.openerId) : undefined;
const wkPage = new WKPage(context, pageProxySession, opener || null);
this._wkPages.set(pageProxyId, wkPage);
2019-12-19 16:53:24 -08:00
wkPage.pageOrError().then(async () => {
const page = wkPage._page;
context!.emit(Events.BrowserContext.Page, page);
if (!opener)
return;
await opener.pageOrError();
const openerPage = opener._page;
if (!openerPage.isClosed())
openerPage.emit(Events.Page.Popup, page);
});
2019-12-19 16:53:24 -08:00
}
_onPageProxyDestroyed(event: Protocol.Playwright.pageProxyDestroyedPayload) {
const pageProxyId = event.pageProxyId;
const wkPage = this._wkPages.get(pageProxyId);
if (!wkPage)
return;
wkPage.didClose();
wkPage.dispose();
this._wkPages.delete(pageProxyId);
2019-12-19 16:53:24 -08:00
}
_onPageProxyMessageReceived(event: PageProxyMessageReceivedPayload) {
const wkPage = this._wkPages.get(event.pageProxyId);
if (!wkPage)
return;
wkPage.dispatchMessageToSession(event.message);
}
_onProvisionalLoadFailed(event: Protocol.Playwright.provisionalLoadFailedPayload) {
const wkPage = this._wkPages.get(event.pageProxyId);
if (!wkPage)
return;
wkPage.handleProvisionalLoadFailed(event);
}
2019-12-19 16:53:24 -08:00
isConnected(): boolean {
return !this._connection.isClosed();
2019-12-19 16:53:24 -08:00
}
_disconnect() {
2019-12-19 16:53:24 -08:00
helper.removeEventListeners(this._eventListeners);
this._connection.close();
}
2019-12-19 16:53:24 -08:00
}
export class WKBrowserContext extends BrowserContextBase {
readonly _browser: WKBrowser;
readonly _browserContextId: string | undefined;
readonly _evaluateOnNewDocumentSources: string[];
constructor(browser: WKBrowser, browserContextId: string | undefined, options: BrowserContextOptions) {
super(browser, options);
this._browser = browser;
this._browserContextId = browserContextId;
this._evaluateOnNewDocumentSources = [];
}
async _initialize() {
const browserContextId = this._browserContextId;
const promises: Promise<any>[] = [
this._browser._browserSession.send('Playwright.setDownloadBehavior', {
behavior: this._options.acceptDownloads ? 'allow' : 'deny',
downloadPath: this._browser._options.downloadsPath,
browserContextId
})
];
if (this._options.ignoreHTTPSErrors)
promises.push(this._browser._browserSession.send('Playwright.setIgnoreCertificateErrors', { browserContextId, ignore: true }));
if (this._options.locale)
promises.push(this._browser._browserSession.send('Playwright.setLanguages', { browserContextId, languages: [this._options.locale] }));
if (this._options.permissions)
promises.push(this.grantPermissions(this._options.permissions));
if (this._options.geolocation)
promises.push(this.setGeolocation(this._options.geolocation));
if (this._options.offline)
promises.push(this.setOffline(this._options.offline));
if (this._options.httpCredentials)
promises.push(this.setHTTPCredentials(this._options.httpCredentials));
await Promise.all(promises);
}
_wkPages(): WKPage[] {
return Array.from(this._browser._wkPages.values()).filter(wkPage => wkPage._browserContext === this);
}
pages(): Page[] {
return this._wkPages().map(wkPage => wkPage._initializedPage).filter(pageOrNull => !!pageOrNull) as Page[];
}
async newPage(): Promise<Page> {
assertBrowserContextIsNotOwned(this);
const { pageProxyId } = await this._browser._browserSession.send('Playwright.createPage', { browserContextId: this._browserContextId });
const wkPage = this._browser._wkPages.get(pageProxyId)!;
const result = await wkPage.pageOrError();
if (result instanceof Page) {
if (result.isClosed())
throw new Error('Page has been closed.');
return result;
}
throw result;
}
async cookies(urls?: string | string[]): Promise<network.NetworkCookie[]> {
const { cookies } = await this._browser._browserSession.send('Playwright.getAllCookies', { browserContextId: this._browserContextId });
return network.filterCookies(cookies.map((c: network.NetworkCookie) => {
const copy: any = { ... c };
copy.expires = c.expires === -1 ? -1 : c.expires / 1000;
delete copy.session;
return copy as network.NetworkCookie;
}), urls);
}
async addCookies(cookies: network.SetNetworkCookieParam[]) {
const cc = network.rewriteCookies(cookies).map(c => ({
...c,
session: c.expires === -1 || c.expires === undefined,
expires: c.expires && c.expires !== -1 ? c.expires * 1000 : c.expires
})) as Protocol.Playwright.SetCookieParam[];
await this._browser._browserSession.send('Playwright.setCookies', { cookies: cc, browserContextId: this._browserContextId });
}
async clearCookies() {
await this._browser._browserSession.send('Playwright.deleteAllCookies', { browserContextId: this._browserContextId });
}
async _doGrantPermissions(origin: string, permissions: string[]) {
await Promise.all(this.pages().map(page => (page._delegate as WKPage)._grantPermissions(origin, permissions)));
}
async _doClearPermissions() {
await Promise.all(this.pages().map(page => (page._delegate as WKPage)._clearPermissions()));
}
async setGeolocation(geolocation: types.Geolocation | null): Promise<void> {
if (geolocation)
geolocation = verifyGeolocation(geolocation);
this._options.geolocation = geolocation || undefined;
const payload: any = geolocation ? { ...geolocation, timestamp: Date.now() } : undefined;
await this._browser._browserSession.send('Playwright.setGeolocationOverride', { browserContextId: this._browserContextId, geolocation: payload });
}
async setExtraHTTPHeaders(headers: network.Headers): Promise<void> {
this._options.extraHTTPHeaders = network.verifyHeaders(headers);
for (const page of this.pages())
await (page._delegate as WKPage).updateExtraHTTPHeaders();
}
async setOffline(offline: boolean): Promise<void> {
this._options.offline = offline;
for (const page of this.pages())
await (page._delegate as WKPage).updateOffline();
}
async setHTTPCredentials(httpCredentials: types.Credentials | null): Promise<void> {
this._options.httpCredentials = httpCredentials || undefined;
for (const page of this.pages())
await (page._delegate as WKPage).updateHttpCredentials();
}
async addInitScript(script: Function | string | { path?: string, content?: string }, arg?: any) {
const source = await helper.evaluationScript(script, arg);
this._evaluateOnNewDocumentSources.push(source);
for (const page of this.pages())
await (page._delegate as WKPage)._updateBootstrapScript();
}
async _doExposeBinding(binding: PageBinding) {
for (const page of this.pages())
await (page._delegate as WKPage).exposeBinding(binding);
}
async route(url: types.URLMatch, handler: network.RouteHandler): Promise<void> {
this._routes.push({ url, handler });
for (const page of this.pages())
await (page._delegate as WKPage).updateRequestInterception();
}
async unroute(url: types.URLMatch, handler?: network.RouteHandler): Promise<void> {
this._routes = this._routes.filter(route => route.url !== url || (handler && route.handler !== handler));
for (const page of this.pages())
await (page._delegate as WKPage).updateRequestInterception();
}
async close() {
if (this._closed)
return;
if (!this._browserContextId) {
// Default context is only created in 'persistent' mode and closing it should close
// the browser.
await this._browser.close();
return;
}
await this._browser._browserSession.send('Playwright.deleteContext', { browserContextId: this._browserContextId });
this._browser._contexts.delete(this._browserContextId);
await this._didCloseInternal();
}
}