2021-02-01 01:37:13 +01: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.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-02-11 15:36:15 +01:00
|
|
|
import fs from 'fs';
|
|
|
|
|
import path from 'path';
|
2021-02-01 01:37:13 +01:00
|
|
|
import * as util from 'util';
|
|
|
|
|
import { CRPage } from '../../chromium/crPage';
|
|
|
|
|
import { Page } from '../../page';
|
|
|
|
|
import { ProgressController } from '../../progress';
|
|
|
|
|
import { createPlaywright } from '../../playwright';
|
|
|
|
|
import { EventEmitter } from 'events';
|
2021-02-09 23:44:48 +01:00
|
|
|
import { internalCallMetadata } from '../../instrumentation';
|
2021-02-13 03:53:46 +01:00
|
|
|
import type { CallLog, EventData, Mode, Source } from './recorderTypes';
|
2021-02-12 02:46:54 +01:00
|
|
|
import { BrowserContext } from '../../browserContext';
|
2021-02-12 19:11:30 +01:00
|
|
|
import { isUnderTest } from '../../../utils/utils';
|
2021-02-01 01:37:13 +01:00
|
|
|
|
|
|
|
|
const readFileAsync = util.promisify(fs.readFile);
|
|
|
|
|
|
2021-02-05 23:24:27 +01:00
|
|
|
declare global {
|
|
|
|
|
interface Window {
|
|
|
|
|
playwrightSetMode: (mode: Mode) => void;
|
2021-02-13 03:53:46 +01:00
|
|
|
playwrightSetPaused: (paused: boolean) => void;
|
|
|
|
|
playwrightSetSources: (sources: Source[]) => void;
|
|
|
|
|
playwrightUpdateLogs: (callLogs: CallLog[]) => void;
|
2021-02-05 23:24:27 +01:00
|
|
|
dispatch(data: EventData): Promise<void>;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-01 01:37:13 +01:00
|
|
|
export class RecorderApp extends EventEmitter {
|
|
|
|
|
private _page: Page;
|
|
|
|
|
|
|
|
|
|
constructor(page: Page) {
|
|
|
|
|
super();
|
2021-02-03 22:53:09 +01:00
|
|
|
this.setMaxListeners(0);
|
2021-02-01 01:37:13 +01:00
|
|
|
this._page = page;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-05 23:24:27 +01:00
|
|
|
async close() {
|
|
|
|
|
await this._page.context().close();
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-01 01:37:13 +01:00
|
|
|
private async _init() {
|
2021-02-06 01:19:09 +01:00
|
|
|
const icon = await readFileAsync(require.resolve('../../../web/recorder/app_icon.png'));
|
2021-02-01 01:37:13 +01:00
|
|
|
const crPopup = this._page._delegate as CRPage;
|
|
|
|
|
await crPopup._mainFrameSession._client.send('Browser.setDockTile', {
|
|
|
|
|
image: icon.toString('base64')
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await this._page._setServerRequestInterceptor(async route => {
|
|
|
|
|
if (route.request().url().startsWith('https://playwright/')) {
|
|
|
|
|
const uri = route.request().url().substring('https://playwright/'.length);
|
2021-02-06 01:19:09 +01:00
|
|
|
const file = require.resolve('../../../web/recorder/' + uri);
|
2021-02-01 01:37:13 +01:00
|
|
|
const buffer = await readFileAsync(file);
|
|
|
|
|
await route.fulfill({
|
|
|
|
|
status: 200,
|
|
|
|
|
headers: [
|
|
|
|
|
{ name: 'Content-Type', value: extensionToMime[path.extname(file)] }
|
|
|
|
|
],
|
|
|
|
|
body: buffer.toString('base64'),
|
|
|
|
|
isBase64: true
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
await route.continue();
|
|
|
|
|
});
|
|
|
|
|
|
2021-02-05 23:24:27 +01:00
|
|
|
await this._page.exposeBinding('dispatch', false, (_, data: any) => this.emit('event', data));
|
2021-02-01 01:37:13 +01:00
|
|
|
|
|
|
|
|
this._page.once('close', () => {
|
|
|
|
|
this.emit('close');
|
|
|
|
|
this._page.context().close().catch(e => console.error(e));
|
|
|
|
|
});
|
|
|
|
|
|
2021-02-09 23:44:48 +01:00
|
|
|
const mainFrame = this._page.mainFrame();
|
|
|
|
|
await mainFrame.goto(internalCallMetadata(), 'https://playwright/index.html');
|
2021-02-01 01:37:13 +01:00
|
|
|
}
|
|
|
|
|
|
2021-02-12 02:46:54 +01:00
|
|
|
static async open(inspectedContext: BrowserContext): Promise<RecorderApp> {
|
2021-02-01 01:37:13 +01:00
|
|
|
const recorderPlaywright = createPlaywright(true);
|
2021-02-12 02:46:54 +01:00
|
|
|
const context = await recorderPlaywright.chromium.launchPersistentContext(internalCallMetadata(), '', {
|
|
|
|
|
sdkLanguage: inspectedContext._options.sdkLanguage,
|
2021-02-01 01:37:13 +01:00
|
|
|
args: [
|
|
|
|
|
'--app=data:text/html,',
|
2021-02-12 02:46:54 +01:00
|
|
|
'--window-size=600,600',
|
|
|
|
|
'--window-position=1280,10',
|
2021-02-01 01:37:13 +01:00
|
|
|
],
|
2021-02-12 02:46:54 +01:00
|
|
|
noDefaultViewport: true,
|
2021-02-12 19:11:30 +01:00
|
|
|
headless: isUnderTest() && !inspectedContext._browser.options.headful
|
2021-02-01 01:37:13 +01:00
|
|
|
});
|
|
|
|
|
|
2021-02-09 23:44:48 +01:00
|
|
|
const controller = new ProgressController(internalCallMetadata(), context._browser);
|
2021-02-01 01:37:13 +01:00
|
|
|
await controller.run(async progress => {
|
|
|
|
|
await context._browser._defaultContext!._loadDefaultContextAsIs(progress);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const [page] = context.pages();
|
|
|
|
|
const result = new RecorderApp(page);
|
|
|
|
|
await result._init();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-05 23:24:27 +01:00
|
|
|
async setMode(mode: 'none' | 'recording' | 'inspecting'): Promise<void> {
|
|
|
|
|
await this._page.mainFrame()._evaluateExpression(((mode: Mode) => {
|
|
|
|
|
window.playwrightSetMode(mode);
|
|
|
|
|
}).toString(), true, mode, 'main').catch(() => {});
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-13 03:53:46 +01:00
|
|
|
async setPaused(paused: boolean): Promise<void> {
|
|
|
|
|
await this._page.mainFrame()._evaluateExpression(((paused: boolean) => {
|
|
|
|
|
window.playwrightSetPaused(paused);
|
|
|
|
|
}).toString(), true, paused, 'main').catch(() => {});
|
2021-02-05 23:24:27 +01:00
|
|
|
}
|
|
|
|
|
|
2021-02-13 03:53:46 +01:00
|
|
|
async setSources(sources: Source[]): Promise<void> {
|
|
|
|
|
await this._page.mainFrame()._evaluateExpression(((sources: Source[]) => {
|
|
|
|
|
window.playwrightSetSources(sources);
|
|
|
|
|
}).toString(), true, sources, 'main').catch(() => {});
|
2021-02-12 02:46:54 +01:00
|
|
|
|
|
|
|
|
// Testing harness for runCLI mode.
|
|
|
|
|
{
|
|
|
|
|
if (process.env.PWCLI_EXIT_FOR_TEST) {
|
|
|
|
|
process.stdout.write('\n-------------8<-------------\n');
|
2021-02-13 03:53:46 +01:00
|
|
|
process.stdout.write(sources[0].text);
|
2021-02-12 02:46:54 +01:00
|
|
|
process.stdout.write('\n-------------8<-------------\n');
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-01 01:37:13 +01:00
|
|
|
}
|
|
|
|
|
|
2021-02-13 03:53:46 +01:00
|
|
|
async updateCallLogs(callLogs: CallLog[]): Promise<void> {
|
|
|
|
|
await this._page.mainFrame()._evaluateExpression(((callLogs: CallLog[]) => {
|
|
|
|
|
window.playwrightUpdateLogs(callLogs);
|
|
|
|
|
}).toString(), true, callLogs, 'main').catch(() => {});
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-01 01:37:13 +01:00
|
|
|
async bringToFront() {
|
|
|
|
|
await this._page.bringToFront();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const extensionToMime: { [key: string]: string } = {
|
|
|
|
|
'.css': 'text/css',
|
|
|
|
|
'.html': 'text/html',
|
|
|
|
|
'.jpeg': 'image/jpeg',
|
|
|
|
|
'.js': 'application/javascript',
|
|
|
|
|
'.png': 'image/png',
|
|
|
|
|
'.ttf': 'font/ttf',
|
|
|
|
|
'.svg': 'image/svg+xml',
|
|
|
|
|
'.webp': 'image/webp',
|
|
|
|
|
'.woff': 'font/woff',
|
|
|
|
|
'.woff2': 'font/woff2',
|
|
|
|
|
};
|