2025-03-21 10:58:58 -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.
|
|
|
|
*/
|
|
|
|
|
2025-03-31 15:30:08 -07:00
|
|
|
import { fork } from 'child_process';
|
|
|
|
import path from 'path';
|
|
|
|
|
2025-03-21 10:58:58 -07:00
|
|
|
import * as playwright from 'playwright';
|
2025-04-01 23:47:53 +02:00
|
|
|
import yaml from 'yaml';
|
2025-03-21 10:58:58 -07:00
|
|
|
|
2025-03-30 09:05:58 -07:00
|
|
|
export type ContextOptions = {
|
2025-04-01 10:26:48 -07:00
|
|
|
browserName?: 'chromium' | 'firefox' | 'webkit';
|
2025-03-30 09:05:58 -07:00
|
|
|
userDataDir: string;
|
|
|
|
launchOptions?: playwright.LaunchOptions;
|
|
|
|
cdpEndpoint?: string;
|
|
|
|
remoteEndpoint?: string;
|
|
|
|
};
|
|
|
|
|
2025-03-21 10:58:58 -07:00
|
|
|
export class Context {
|
2025-03-30 09:05:58 -07:00
|
|
|
private _options: ContextOptions;
|
2025-03-25 13:05:28 -07:00
|
|
|
private _browser: playwright.Browser | undefined;
|
2025-03-21 10:58:58 -07:00
|
|
|
private _page: playwright.Page | undefined;
|
|
|
|
private _console: playwright.ConsoleMessage[] = [];
|
2025-03-26 15:02:45 -07:00
|
|
|
private _createPagePromise: Promise<playwright.Page> | undefined;
|
2025-03-27 20:49:57 +01:00
|
|
|
private _fileChooser: playwright.FileChooser | undefined;
|
2025-04-01 23:47:53 +02:00
|
|
|
private _lastSnapshotFrames: (playwright.Page | playwright.FrameLocator)[] = [];
|
2025-03-21 10:58:58 -07:00
|
|
|
|
2025-03-30 09:05:58 -07:00
|
|
|
constructor(options: ContextOptions) {
|
|
|
|
this._options = options;
|
2025-03-21 10:58:58 -07:00
|
|
|
}
|
|
|
|
|
2025-03-26 15:02:45 -07:00
|
|
|
async createPage(): Promise<playwright.Page> {
|
|
|
|
if (this._createPagePromise)
|
|
|
|
return this._createPagePromise;
|
|
|
|
this._createPagePromise = (async () => {
|
|
|
|
const { browser, page } = await this._createPage();
|
|
|
|
page.on('console', event => this._console.push(event));
|
|
|
|
page.on('framenavigated', frame => {
|
2025-03-21 13:16:30 -07:00
|
|
|
if (!frame.parentFrame())
|
2025-03-21 13:19:51 -07:00
|
|
|
this._console.length = 0;
|
2025-03-21 13:16:30 -07:00
|
|
|
});
|
2025-03-26 15:02:45 -07:00
|
|
|
page.on('close', () => this._onPageClose());
|
2025-03-27 20:49:57 +01:00
|
|
|
page.on('filechooser', chooser => this._fileChooser = chooser);
|
2025-03-27 09:50:37 -07:00
|
|
|
page.setDefaultNavigationTimeout(60000);
|
|
|
|
page.setDefaultTimeout(5000);
|
2025-03-26 15:02:45 -07:00
|
|
|
this._page = page;
|
|
|
|
this._browser = browser;
|
|
|
|
return page;
|
2025-03-21 10:58:58 -07:00
|
|
|
})();
|
2025-03-26 15:02:45 -07:00
|
|
|
return this._createPagePromise;
|
2025-03-21 10:58:58 -07:00
|
|
|
}
|
|
|
|
|
2025-03-26 15:02:45 -07:00
|
|
|
private _onPageClose() {
|
2025-03-25 13:05:28 -07:00
|
|
|
const browser = this._browser;
|
2025-03-26 15:02:45 -07:00
|
|
|
const page = this._page;
|
|
|
|
void page?.context()?.close().then(() => browser?.close()).catch(() => {});
|
|
|
|
|
|
|
|
this._createPagePromise = undefined;
|
2025-03-25 13:05:28 -07:00
|
|
|
this._browser = undefined;
|
|
|
|
this._page = undefined;
|
2025-03-27 20:49:57 +01:00
|
|
|
this._fileChooser = undefined;
|
2025-03-25 13:05:28 -07:00
|
|
|
this._console.length = 0;
|
|
|
|
}
|
|
|
|
|
2025-03-31 15:30:08 -07:00
|
|
|
async install(): Promise<string> {
|
2025-04-01 10:26:48 -07:00
|
|
|
const channel = this._options.launchOptions?.channel ?? this._options.browserName ?? 'chrome';
|
2025-03-31 15:30:08 -07:00
|
|
|
const cli = path.join(require.resolve('playwright/package.json'), '..', 'cli.js');
|
|
|
|
const child = fork(cli, ['install', channel], {
|
|
|
|
stdio: 'pipe',
|
|
|
|
});
|
|
|
|
const output: string[] = [];
|
|
|
|
child.stdout?.on('data', data => output.push(data.toString()));
|
|
|
|
child.stderr?.on('data', data => output.push(data.toString()));
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
child.on('close', code => {
|
|
|
|
if (code === 0)
|
|
|
|
resolve(channel);
|
|
|
|
else
|
|
|
|
reject(new Error(`Failed to install browser: ${output.join('')}`));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2025-03-27 07:27:34 -07:00
|
|
|
existingPage(): playwright.Page {
|
2025-03-26 15:02:45 -07:00
|
|
|
if (!this._page)
|
|
|
|
throw new Error('Navigate to a location to create a page');
|
|
|
|
return this._page;
|
|
|
|
}
|
|
|
|
|
|
|
|
async console(): Promise<playwright.ConsoleMessage[]> {
|
|
|
|
return this._console;
|
|
|
|
}
|
|
|
|
|
|
|
|
async close() {
|
|
|
|
if (!this._page)
|
|
|
|
return;
|
|
|
|
await this._page.close();
|
|
|
|
}
|
|
|
|
|
2025-03-27 20:49:57 +01:00
|
|
|
async submitFileChooser(paths: string[]) {
|
|
|
|
if (!this._fileChooser)
|
|
|
|
throw new Error('No file chooser visible');
|
|
|
|
await this._fileChooser.setFiles(paths);
|
|
|
|
this._fileChooser = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
hasFileChooser() {
|
|
|
|
return !!this._fileChooser;
|
|
|
|
}
|
|
|
|
|
|
|
|
clearFileChooser() {
|
|
|
|
this._fileChooser = undefined;
|
|
|
|
}
|
|
|
|
|
2025-03-26 15:02:45 -07:00
|
|
|
private async _createPage(): Promise<{ browser?: playwright.Browser, page: playwright.Page }> {
|
2025-03-30 09:05:58 -07:00
|
|
|
if (this._options.remoteEndpoint) {
|
|
|
|
const url = new URL(this._options.remoteEndpoint);
|
2025-04-01 10:26:48 -07:00
|
|
|
if (this._options.browserName)
|
|
|
|
url.searchParams.set('browser', this._options.browserName);
|
2025-03-30 09:05:58 -07:00
|
|
|
if (this._options.launchOptions)
|
|
|
|
url.searchParams.set('launch-options', JSON.stringify(this._options.launchOptions));
|
2025-04-01 10:26:48 -07:00
|
|
|
const browser = await playwright[this._options.browserName ?? 'chromium'].connect(String(url));
|
2025-03-26 15:02:45 -07:00
|
|
|
const page = await browser.newPage();
|
|
|
|
return { browser, page };
|
|
|
|
}
|
|
|
|
|
2025-03-30 09:05:58 -07:00
|
|
|
if (this._options.cdpEndpoint) {
|
|
|
|
const browser = await playwright.chromium.connectOverCDP(this._options.cdpEndpoint);
|
|
|
|
const browserContext = browser.contexts()[0];
|
|
|
|
let [page] = browserContext.pages();
|
|
|
|
if (!page)
|
|
|
|
page = await browserContext.newPage();
|
|
|
|
return { browser, page };
|
|
|
|
}
|
|
|
|
|
2025-03-31 15:30:08 -07:00
|
|
|
const context = await this._launchPersistentContext();
|
2025-03-26 15:02:45 -07:00
|
|
|
const [page] = context.pages();
|
|
|
|
return { page };
|
2025-03-21 10:58:58 -07:00
|
|
|
}
|
2025-03-27 20:22:44 +01:00
|
|
|
|
2025-03-31 15:30:08 -07:00
|
|
|
private async _launchPersistentContext(): Promise<playwright.BrowserContext> {
|
|
|
|
try {
|
2025-04-01 10:26:48 -07:00
|
|
|
const browserType = this._options.browserName ? playwright[this._options.browserName] : playwright.chromium;
|
|
|
|
return await browserType.launchPersistentContext(this._options.userDataDir, this._options.launchOptions);
|
2025-03-31 15:30:08 -07:00
|
|
|
} catch (error: any) {
|
|
|
|
if (error.message.includes('Executable doesn\'t exist'))
|
|
|
|
throw new Error(`Browser specified in your config is not installed. Either install it (likely) or change the config.`);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-27 20:22:44 +01:00
|
|
|
async allFramesSnapshot() {
|
2025-04-01 23:47:53 +02:00
|
|
|
this._lastSnapshotFrames = [];
|
|
|
|
const yaml = await this._allFramesSnapshot(this.existingPage());
|
|
|
|
return yaml.toString().trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
private async _allFramesSnapshot(frame: playwright.Page | playwright.FrameLocator): Promise<yaml.Document> {
|
|
|
|
const frameIndex = this._lastSnapshotFrames.push(frame) - 1;
|
|
|
|
const snapshotString = await frame.locator('body').ariaSnapshot({ ref: true });
|
|
|
|
const snapshot = yaml.parseDocument(snapshotString);
|
|
|
|
|
|
|
|
const visit = async (node: any): Promise<unknown> => {
|
|
|
|
if (yaml.isPair(node)) {
|
|
|
|
await Promise.all([
|
|
|
|
visit(node.key).then(k => node.key = k),
|
|
|
|
visit(node.value).then(v => node.value = v)
|
|
|
|
]);
|
|
|
|
} else if (yaml.isSeq(node) || yaml.isMap(node)) {
|
|
|
|
node.items = await Promise.all(node.items.map(visit));
|
|
|
|
} else if (yaml.isScalar(node)) {
|
|
|
|
if (typeof node.value === 'string') {
|
|
|
|
const value = node.value;
|
|
|
|
if (frameIndex > 0)
|
|
|
|
node.value = value.replace('[ref=', `[ref=f${frameIndex}`);
|
|
|
|
if (value.startsWith('iframe ')) {
|
|
|
|
const ref = value.match(/\[ref=(.*)\]/)?.[1];
|
|
|
|
if (ref) {
|
|
|
|
const childSnapshot = await this._allFramesSnapshot(frame.frameLocator(`aria-ref=${ref}`));
|
|
|
|
return snapshot.createPair(node.value, childSnapshot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
};
|
|
|
|
await visit(snapshot.contents);
|
|
|
|
return snapshot;
|
2025-03-27 20:22:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
refLocator(ref: string): playwright.Locator {
|
2025-04-01 23:47:53 +02:00
|
|
|
let frame = this._lastSnapshotFrames[0];
|
2025-03-27 20:22:44 +01:00
|
|
|
const match = ref.match(/^f(\d+)(.*)/);
|
|
|
|
if (match) {
|
|
|
|
const frameIndex = parseInt(match[1], 10);
|
|
|
|
frame = this._lastSnapshotFrames[frameIndex];
|
|
|
|
ref = match[2];
|
|
|
|
}
|
|
|
|
|
2025-04-01 23:47:53 +02:00
|
|
|
if (!frame)
|
|
|
|
throw new Error(`Frame does not exist. Provide ref from the most current snapshot.`);
|
|
|
|
|
2025-03-27 20:22:44 +01:00
|
|
|
return frame.locator(`aria-ref=${ref}`);
|
|
|
|
}
|
2025-03-21 10:58:58 -07:00
|
|
|
}
|