-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
recorderApp.ts
183 lines (161 loc) · 6.41 KB
/
recorderApp.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* 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 fs from 'fs';
import path from 'path';
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';
import { internalCallMetadata } from '../../instrumentation';
import type { CallLog, EventData, Mode, Source } from './recorderTypes';
import { BrowserContext } from '../../browserContext';
import { isUnderTest } from '../../../utils/utils';
const readFileAsync = util.promisify(fs.readFile);
declare global {
interface Window {
playwrightSetFile: (file: string) => void;
playwrightSetMode: (mode: Mode) => void;
playwrightSetPaused: (paused: boolean) => void;
playwrightSetSources: (sources: Source[]) => void;
playwrightSetSelector: (selector: string, focus?: boolean) => void;
playwrightUpdateLogs: (callLogs: CallLog[]) => void;
dispatch(data: EventData): Promise<void>;
}
}
export class RecorderApp extends EventEmitter {
private _page: Page;
readonly wsEndpoint: string | undefined;
constructor(page: Page, wsEndpoint: string | undefined) {
super();
this.setMaxListeners(0);
this._page = page;
this.wsEndpoint = wsEndpoint;
}
async close() {
await this._page.context().close(internalCallMetadata());
}
private async _init() {
const icon = await readFileAsync(require.resolve('../../../web/recorder/app_icon.png'));
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);
const file = require.resolve('../../../web/recorder/' + uri);
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();
});
await this._page.exposeBinding('dispatch', false, (_, data: any) => this.emit('event', data));
this._page.once('close', () => {
this.emit('close');
this._page.context().close(internalCallMetadata()).catch(e => console.error(e));
});
const mainFrame = this._page.mainFrame();
await mainFrame.goto(internalCallMetadata(), 'https://playwright/index.html');
}
static async open(inspectedContext: BrowserContext): Promise<RecorderApp> {
const recorderPlaywright = createPlaywright(true);
const args = [
'--app=data:text/html,',
'--window-size=600,600',
'--window-position=1280,10',
];
if (isUnderTest())
args.push(`--remote-debugging-port=0`);
const context = await recorderPlaywright.chromium.launchPersistentContext(internalCallMetadata(), '', {
sdkLanguage: inspectedContext._options.sdkLanguage,
args,
noDefaultViewport: true,
headless: !!process.env.PWCLI_HEADLESS_FOR_TEST || (isUnderTest() && !inspectedContext._browser.options.headful),
useWebSocket: isUnderTest()
});
const controller = new ProgressController(internalCallMetadata(), context._browser);
await controller.run(async progress => {
await context._browser._defaultContext!._loadDefaultContextAsIs(progress);
});
const [page] = context.pages();
const result = new RecorderApp(page, context._browser.options.wsEndpoint);
await result._init();
return result;
}
async setMode(mode: 'none' | 'recording' | 'inspecting'): Promise<void> {
await this._page.mainFrame()._evaluateExpression(((mode: Mode) => {
window.playwrightSetMode(mode);
}).toString(), true, mode, 'main').catch(() => {});
}
async setFile(file: string): Promise<void> {
await this._page.mainFrame()._evaluateExpression(((file: string) => {
window.playwrightSetFile(file);
}).toString(), true, file, 'main').catch(() => {});
}
async setPaused(paused: boolean): Promise<void> {
await this._page.mainFrame()._evaluateExpression(((paused: boolean) => {
window.playwrightSetPaused(paused);
}).toString(), true, paused, 'main').catch(() => {});
}
async setSources(sources: Source[]): Promise<void> {
await this._page.mainFrame()._evaluateExpression(((sources: Source[]) => {
window.playwrightSetSources(sources);
}).toString(), true, sources, 'main').catch(() => {});
// Testing harness for runCLI mode.
{
if (process.env.PWCLI_EXIT_FOR_TEST && sources.length) {
process.stdout.write('\n-------------8<-------------\n');
process.stdout.write(sources[0].text);
process.stdout.write('\n-------------8<-------------\n');
}
}
}
async setSelector(selector: string, focus?: boolean): Promise<void> {
await this._page.mainFrame()._evaluateExpression(((arg: any) => {
window.playwrightSetSelector(arg.selector, arg.focus);
}).toString(), true, { selector, focus }, 'main').catch(() => {});
}
async updateCallLogs(callLogs: CallLog[]): Promise<void> {
await this._page.mainFrame()._evaluateExpression(((callLogs: CallLog[]) => {
window.playwrightUpdateLogs(callLogs);
}).toString(), true, callLogs, 'main').catch(() => {});
}
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',
};