Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

chore(webkit): simplify session init logic (#523)

Yury Semikhatskyfc9ddb7c3cc5
diff --git a/src/platform.ts b/src/platform.ts
index c3d67a76e7..c464f04777 100644
--- a/src/platform.ts
+++ b/src/platform.ts
@@ -1,5 +1,18 @@
-// Copyright (c) Microsoft Corporation.
-// Licensed under the MIT license.
+/**
+ * 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.
+ */
// Note: this is the only file outside of src/server which can import external dependencies.
// All dependencies must be listed in web.webpack.config.js to avoid bundling them.
diff --git a/src/webkit/wkPage.ts b/src/webkit/wkPage.ts
index c3848adf3e..140ccc71a2 100644
--- a/src/webkit/wkPage.ts
+++ b/src/webkit/wkPage.ts
@@ -46,7 +46,6 @@ export class WKPage implements PageDelegate {
private readonly _networkManager: WKNetworkManager;
private readonly _workers: WKWorkers;
private readonly _contextIdToContext: Map<number, dom.FrameExecutionContext>;
- private _isolatedWorlds: Set<string>;
private _sessionListeners: RegisteredListener[] = [];
private readonly _bootstrapScripts: string[] = [];
@@ -55,7 +54,6 @@ export class WKPage implements PageDelegate {
this.rawKeyboard = new RawKeyboardImpl(pageProxySession);
this.rawMouse = new RawMouseImpl(pageProxySession);
this._contextIdToContext = new Map();
- this._isolatedWorlds = new Set();
this._page = new Page(this, browserContext);
this._networkManager = new WKNetworkManager(this._page, pageProxySession);
this._workers = new WKWorkers(this._page);
@@ -83,7 +81,6 @@ export class WKPage implements PageDelegate {
this._addSessionListeners();
this._networkManager.setSession(session);
this._workers.setSession(session);
- this._isolatedWorlds = new Set();
// New bootstrap scripts may have been added during provisional load, push them
// again to be on the safe side.
if (this._bootstrapScripts.length)
@@ -98,7 +95,8 @@ export class WKPage implements PageDelegate {
session.send('Page.enable'),
session.send('Page.getResourceTree').then(({frameTree}) => this._handleFrameTree(frameTree)),
// Resource tree should be received before first execution context.
- session.send('Runtime.enable').then(() => this._ensureIsolatedWorld(UTILITY_WORLD_NAME)),
+ session.send('Runtime.enable'),
+ session.send('Page.createIsolatedWorld', { name: UTILITY_WORLD_NAME, source: `//# sourceURL=${EVALUATION_SCRIPT_URL}` }),
session.send('Console.enable'),
session.send('Page.setInterceptFileChooserDialog', { enabled: true }),
this._networkManager.initializeSession(session, this._page._state.interceptNetwork, this._page._state.offlineMode),
@@ -108,13 +106,13 @@ export class WKPage implements PageDelegate {
if (contextOptions.userAgent)
promises.push(session.send('Page.overrideUserAgent', { value: contextOptions.userAgent }));
if (this._page._state.mediaType || this._page._state.colorScheme)
- promises.push(this._setEmulateMedia(session, this._page._state.mediaType, this._page._state.colorScheme));
+ promises.push(WKPage._setEmulateMedia(session, this._page._state.mediaType, this._page._state.colorScheme));
if (isProvisional)
promises.push(this._setBootstrapScripts(session));
if (contextOptions.bypassCSP)
promises.push(session.send('Page.setBypassCSP', { enabled: true }));
if (this._page._state.extraHTTPHeaders !== null)
- promises.push(this._setExtraHTTPHeaders(session, this._page._state.extraHTTPHeaders));
+ promises.push(WKPage._setExtraHTTPHeaders(session, this._page._state.extraHTTPHeaders));
if (this._page._state.hasTouch)
promises.push(session.send('Page.setTouchEmulationEnabled', { enabled: true }));
await Promise.all(promises).catch(e => {
@@ -285,21 +283,11 @@ export class WKPage implements PageDelegate {
this._page._onFileChooserOpened(handle);
}
- async _ensureIsolatedWorld(name: string) {
- if (this._isolatedWorlds.has(name))
- return;
- this._isolatedWorlds.add(name);
- await this._session.send('Page.createIsolatedWorld', {
- name,
- source: `//# sourceURL=${EVALUATION_SCRIPT_URL}`
- });
- }
-
- private async _setExtraHTTPHeaders(session: WKSession, headers: network.Headers): Promise<void> {
+ private static async _setExtraHTTPHeaders(session: WKSession, headers: network.Headers): Promise<void> {
await session.send('Network.setExtraHTTPHeaders', { headers });
}
- private async _setEmulateMedia(session: WKSession, mediaType: types.MediaType | null, colorScheme: types.ColorScheme | null): Promise<void> {
+ private static async _setEmulateMedia(session: WKSession, mediaType: types.MediaType | null, colorScheme: types.ColorScheme | null): Promise<void> {
const promises = [];
promises.push(session.send('Page.setEmulatedMedia', { media: mediaType || '' }));
if (colorScheme !== null) {
@@ -314,11 +302,11 @@ export class WKPage implements PageDelegate {
}
async setExtraHTTPHeaders(headers: network.Headers): Promise<void> {
- await this._setExtraHTTPHeaders(this._session, headers);
+ await WKPage._setExtraHTTPHeaders(this._session, headers);
}
async setEmulateMedia(mediaType: types.MediaType | null, colorScheme: types.ColorScheme | null): Promise<void> {
- await this._setEmulateMedia(this._session, mediaType, colorScheme);
+ await WKPage._setEmulateMedia(this._session, mediaType, colorScheme);
}
async setViewport(viewport: types.Viewport): Promise<void> {
diff --git a/src/webkit/wkPageProxy.ts b/src/webkit/wkPageProxy.ts
index a314b1c25f..f01e1971b2 100644
--- a/src/webkit/wkPageProxy.ts
+++ b/src/webkit/wkPageProxy.ts
@@ -1,5 +1,18 @@
-// Copyright (c) Microsoft Corporation.
-// Licensed under the MIT license.
+/**
+ * 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 { BrowserContext } from '../browserContext';
diff --git a/utils/testrunner/SourceMapSupport.js b/utils/testrunner/SourceMapSupport.js
index 458ae312d7..ae8933c55e 100644
--- a/utils/testrunner/SourceMapSupport.js
+++ b/utils/testrunner/SourceMapSupport.js
@@ -1,5 +1,18 @@
-// Copyright (c) Microsoft Corporation.
-// Licensed under the MIT license.
+/**
+ * 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.
+ */
const fs = require('fs');
const path = require('path');