-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
codeGenerator.ts
171 lines (155 loc) · 5.45 KB
/
codeGenerator.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
/**
* 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 { EventEmitter } from 'events';
import type { BrowserContextOptions, LaunchOptions } from '../../../..';
import { Frame } from '../../frames';
import { LanguageGenerator, LanguageGeneratorOptions } from './language';
import { Action, Signal } from './recorderActions';
import { describeFrame } from './utils';
export type ActionInContext = {
pageAlias: string;
frameName?: string;
frameUrl: string;
isMainFrame: boolean;
action: Action;
committed?: boolean;
}
export class CodeGenerator extends EventEmitter {
private _currentAction: ActionInContext | null = null;
private _lastAction: ActionInContext | null = null;
private _actions: ActionInContext[] = [];
private _enabled: boolean;
private _options: LanguageGeneratorOptions;
constructor(browserName: string, generateHeaders: boolean, launchOptions: LaunchOptions, contextOptions: BrowserContextOptions, deviceName: string | undefined, saveStorage: string | undefined) {
super();
launchOptions = { headless: false, ...launchOptions };
this._enabled = generateHeaders;
this._options = { browserName, generateHeaders, launchOptions, contextOptions, deviceName, saveStorage };
this.restart();
}
restart() {
this._currentAction = null;
this._lastAction = null;
this._actions = [];
}
setEnabled(enabled: boolean) {
this._enabled = enabled;
}
addAction(action: ActionInContext) {
if (!this._enabled)
return;
this.willPerformAction(action);
this.didPerformAction(action);
}
willPerformAction(action: ActionInContext) {
if (!this._enabled)
return;
this._currentAction = action;
}
performedActionFailed(action: ActionInContext) {
if (!this._enabled)
return;
if (this._currentAction === action)
this._currentAction = null;
}
didPerformAction(actionInContext: ActionInContext) {
if (!this._enabled)
return;
const { action, pageAlias } = actionInContext;
let eraseLastAction = false;
if (this._lastAction && this._lastAction.pageAlias === pageAlias) {
const { action: lastAction } = this._lastAction;
// We augment last action based on the type.
if (this._lastAction && action.name === 'fill' && lastAction.name === 'fill') {
if (action.selector === lastAction.selector)
eraseLastAction = true;
}
if (lastAction && action.name === 'click' && lastAction.name === 'click') {
if (action.selector === lastAction.selector && action.clickCount > lastAction.clickCount)
eraseLastAction = true;
}
if (lastAction && action.name === 'navigate' && lastAction.name === 'navigate') {
if (action.url === lastAction.url) {
// Already at a target URL.
this._currentAction = null;
return;
}
}
for (const name of ['check', 'uncheck']) {
// Check and uncheck erase click.
if (lastAction && action.name === name && lastAction.name === 'click') {
if ((action as any).selector === (lastAction as any).selector)
eraseLastAction = true;
}
}
}
this._lastAction = actionInContext;
this._currentAction = null;
if (eraseLastAction)
this._actions.pop();
this._actions.push(actionInContext);
this.emit('change');
}
commitLastAction() {
if (!this._enabled)
return;
const action = this._lastAction;
if (action)
action.committed = true;
}
signal(pageAlias: string, frame: Frame, signal: Signal) {
if (!this._enabled)
return;
// Signal either arrives while action is being performed or shortly after.
if (this._currentAction) {
this._currentAction.action.signals.push(signal);
return;
}
if (this._lastAction && !this._lastAction.committed) {
const signals = this._lastAction.action.signals;
if (signal.name === 'navigation' && signals.length && signals[signals.length - 1].name === 'download')
return;
if (signal.name === 'download' && signals.length && signals[signals.length - 1].name === 'navigation')
signals.length = signals.length - 1;
signal.isAsync = true;
this._lastAction.action.signals.push(signal);
this.emit('change');
return;
}
if (signal.name === 'navigation') {
this.addAction({
pageAlias,
...describeFrame(frame),
committed: true,
action: {
name: 'navigate',
url: frame.url(),
signals: [],
},
});
}
}
generateText(languageGenerator: LanguageGenerator) {
const text = [];
if (this._options.generateHeaders)
text.push(languageGenerator.generateHeader(this._options));
for (const action of this._actions)
text.push(languageGenerator.generateAction(action));
if (this._options.generateHeaders)
text.push(languageGenerator.generateFooter(this._options.saveStorage));
return text.join('\n');
}
}