@elastic/synthetics 1.0.0-beta.8 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -4
- package/dist/cli.js +139 -165
- package/dist/cli.js.map +1 -1
- package/dist/common_types.d.ts +158 -34
- package/dist/common_types.d.ts.map +1 -1
- package/dist/config.d.ts +1 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +7 -6
- package/dist/config.js.map +1 -1
- package/dist/core/browser-service.d.ts +1 -1
- package/dist/core/browser-service.d.ts.map +1 -1
- package/dist/core/browser-service.js +7 -6
- package/dist/core/browser-service.js.map +1 -1
- package/dist/core/expect.d.ts +25 -25
- package/dist/core/expect.d.ts.map +1 -1
- package/dist/core/expect.js.map +1 -1
- package/dist/core/gatherer.d.ts +5 -8
- package/dist/core/gatherer.d.ts.map +1 -1
- package/dist/core/gatherer.js +75 -17
- package/dist/core/gatherer.js.map +1 -1
- package/dist/core/index.d.ts +5 -1
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +23 -11
- package/dist/core/index.js.map +1 -1
- package/dist/core/logger.d.ts +0 -1
- package/dist/core/logger.d.ts.map +1 -1
- package/dist/core/logger.js +10 -17
- package/dist/core/logger.js.map +1 -1
- package/dist/core/runner.d.ts +12 -63
- package/dist/core/runner.d.ts.map +1 -1
- package/dist/core/runner.js +159 -92
- package/dist/core/runner.js.map +1 -1
- package/dist/core/transform.d.ts +37 -0
- package/dist/core/transform.d.ts.map +1 -0
- package/dist/core/transform.js +148 -0
- package/dist/core/transform.js.map +1 -0
- package/dist/dsl/index.js +5 -1
- package/dist/dsl/index.js.map +1 -1
- package/dist/dsl/journey.d.ts +9 -7
- package/dist/dsl/journey.d.ts.map +1 -1
- package/dist/dsl/journey.js +32 -39
- package/dist/dsl/journey.js.map +1 -1
- package/dist/dsl/monitor.d.ts +93 -0
- package/dist/dsl/monitor.d.ts.map +1 -0
- package/dist/dsl/monitor.js +110 -0
- package/dist/dsl/monitor.js.map +1 -0
- package/dist/dsl/step.d.ts +3 -2
- package/dist/dsl/step.d.ts.map +1 -1
- package/dist/dsl/step.js +6 -1
- package/dist/dsl/step.js.map +1 -1
- package/dist/formatter/javascript.d.ts +115 -0
- package/dist/formatter/javascript.d.ts.map +1 -0
- package/dist/formatter/javascript.js +331 -0
- package/dist/formatter/javascript.js.map +1 -0
- package/dist/generator/index.d.ts +24 -0
- package/dist/generator/index.d.ts.map +1 -0
- package/dist/generator/index.js +241 -0
- package/dist/generator/index.js.map +1 -0
- package/dist/{reporters/reporter.js → generator/utils.d.ts} +5 -3
- package/dist/generator/utils.d.ts.map +1 -0
- package/dist/generator/utils.js +84 -0
- package/dist/generator/utils.js.map +1 -0
- package/dist/helpers.d.ts +36 -9
- package/dist/helpers.d.ts.map +1 -1
- package/dist/helpers.js +170 -24
- package/dist/helpers.js.map +1 -1
- package/dist/index.d.ts +5 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -26
- package/dist/index.js.map +1 -1
- package/dist/loader.d.ts +34 -0
- package/dist/loader.d.ts.map +1 -0
- package/dist/loader.js +153 -0
- package/dist/loader.js.map +1 -0
- package/dist/locations/index.d.ts +45 -0
- package/dist/locations/index.d.ts.map +1 -0
- package/dist/locations/index.js +83 -0
- package/dist/locations/index.js.map +1 -0
- package/dist/{reporters/reporter.d.ts → locations/public-locations.d.ts} +12 -8
- package/dist/locations/public-locations.d.ts.map +1 -0
- package/dist/locations/public-locations.js +41 -0
- package/dist/locations/public-locations.js.map +1 -0
- package/dist/options.d.ts +42 -0
- package/dist/options.d.ts.map +1 -0
- package/dist/options.js +146 -0
- package/dist/options.js.map +1 -0
- package/dist/plugins/browser-console.d.ts +5 -4
- package/dist/plugins/browser-console.d.ts.map +1 -1
- package/dist/plugins/browser-console.js +47 -24
- package/dist/plugins/browser-console.js.map +1 -1
- package/dist/plugins/index.js +5 -1
- package/dist/plugins/index.js.map +1 -1
- package/dist/plugins/network.d.ts +24 -31
- package/dist/plugins/network.d.ts.map +1 -1
- package/dist/plugins/network.js +226 -229
- package/dist/plugins/network.js.map +1 -1
- package/dist/plugins/performance.d.ts +4 -19
- package/dist/plugins/performance.d.ts.map +1 -1
- package/dist/plugins/performance.js +6 -5
- package/dist/plugins/performance.js.map +1 -1
- package/dist/plugins/plugin-manager.d.ts +9 -5
- package/dist/plugins/plugin-manager.d.ts.map +1 -1
- package/dist/plugins/plugin-manager.js +46 -30
- package/dist/plugins/plugin-manager.js.map +1 -1
- package/dist/plugins/tracing.d.ts +6 -6
- package/dist/plugins/tracing.d.ts.map +1 -1
- package/dist/plugins/tracing.js +13 -7
- package/dist/plugins/tracing.js.map +1 -1
- package/dist/push/bundler.d.ts +35 -0
- package/dist/push/bundler.d.ts.map +1 -0
- package/dist/push/bundler.js +131 -0
- package/dist/push/bundler.js.map +1 -0
- package/dist/push/index.d.ts +10 -0
- package/dist/push/index.d.ts.map +1 -0
- package/dist/push/index.js +235 -0
- package/dist/push/index.js.map +1 -0
- package/dist/push/kibana_api.d.ts +56 -0
- package/dist/push/kibana_api.d.ts.map +1 -0
- package/dist/push/kibana_api.js +135 -0
- package/dist/push/kibana_api.js.map +1 -0
- package/dist/push/monitor.d.ts +55 -0
- package/dist/push/monitor.d.ts.map +1 -0
- package/dist/push/monitor.js +268 -0
- package/dist/push/monitor.js.map +1 -0
- package/dist/{parse_args.d.ts → push/plugin.d.ts} +9 -6
- package/dist/push/plugin.d.ts.map +1 -0
- package/dist/push/plugin.js +66 -0
- package/dist/push/plugin.js.map +1 -0
- package/dist/push/request.d.ts +45 -0
- package/dist/push/request.d.ts.map +1 -0
- package/dist/push/request.js +109 -0
- package/dist/push/request.js.map +1 -0
- package/dist/push/utils.d.ts +31 -0
- package/dist/push/utils.d.ts.map +1 -0
- package/dist/push/utils.js +64 -0
- package/dist/push/utils.js.map +1 -0
- package/dist/reporters/base.d.ts +14 -6
- package/dist/reporters/base.d.ts.map +1 -1
- package/dist/reporters/base.js +58 -50
- package/dist/reporters/base.js.map +1 -1
- package/dist/reporters/index.d.ts +18 -8
- package/dist/reporters/index.d.ts.map +1 -1
- package/dist/reporters/index.js.map +1 -1
- package/dist/reporters/json.d.ts +17 -62
- package/dist/reporters/json.d.ts.map +1 -1
- package/dist/reporters/json.js +147 -177
- package/dist/reporters/json.js.map +1 -1
- package/dist/reporters/junit.d.ts +7 -1
- package/dist/reporters/junit.d.ts.map +1 -1
- package/dist/reporters/junit.js +90 -95
- package/dist/reporters/junit.js.map +1 -1
- package/dist/sdk/lh-trace-processor.d.ts +35 -9
- package/dist/sdk/lh-trace-processor.d.ts.map +1 -1
- package/dist/sdk/lh-trace-processor.js +114 -53
- package/dist/sdk/lh-trace-processor.js.map +1 -1
- package/dist/sdk/trace-metrics.d.ts +4 -4
- package/dist/sdk/trace-metrics.d.ts.map +1 -1
- package/dist/sdk/trace-metrics.js +3 -4
- package/dist/sdk/trace-metrics.js.map +1 -1
- package/dist/sdk/trace-processor.d.ts +11 -3
- package/dist/sdk/trace-processor.d.ts.map +1 -1
- package/dist/sdk/trace-processor.js +17 -9
- package/dist/sdk/trace-processor.js.map +1 -1
- package/package.json +38 -30
- package/src/cli.ts +223 -158
- package/src/common_types.ts +191 -34
- package/src/config.ts +1 -1
- package/src/core/expect.ts +58 -24
- package/src/core/gatherer.ts +92 -25
- package/src/core/index.ts +36 -15
- package/src/core/logger.ts +8 -12
- package/src/core/runner.ts +158 -148
- package/src/core/transform.ts +160 -0
- package/src/dsl/journey.ts +41 -21
- package/src/dsl/monitor.ts +152 -0
- package/src/dsl/step.ts +9 -2
- package/src/formatter/javascript.ts +414 -0
- package/src/generator/index.ts +308 -0
- package/src/generator/utils.ts +84 -0
- package/src/helpers.ts +182 -16
- package/src/index.ts +21 -22
- package/src/loader.ts +171 -0
- package/src/locations/index.ts +95 -0
- package/src/{reporters/reporter.ts → locations/public-locations.ts} +12 -9
- package/src/options.ts +175 -0
- package/src/plugins/browser-console.ts +33 -8
- package/src/plugins/network.ts +266 -284
- package/src/plugins/performance.ts +6 -22
- package/src/plugins/plugin-manager.ts +49 -31
- package/src/plugins/tracing.ts +11 -9
- package/src/push/bundler.ts +112 -0
- package/src/push/index.ts +291 -0
- package/src/push/kibana_api.ts +191 -0
- package/src/push/monitor.ts +307 -0
- package/src/push/plugin.ts +66 -0
- package/src/push/request.ts +141 -0
- package/src/push/utils.ts +77 -0
- package/src/reporters/base.ts +63 -52
- package/src/reporters/index.ts +26 -3
- package/src/reporters/json.ts +181 -201
- package/src/reporters/junit.ts +98 -93
- package/src/sdk/lh-trace-processor.ts +132 -68
- package/src/sdk/trace-metrics.ts +8 -4
- package/src/sdk/trace-processor.ts +28 -13
- package/templates/.github/workflows/run-synthetics.yml +27 -0
- package/templates/README.md +21 -0
- package/templates/journeys/advanced-example-helpers.ts +54 -0
- package/templates/journeys/advanced-example.journey.ts +43 -0
- package/templates/journeys/example.journey.ts +18 -0
- package/templates/lightweight/heartbeat.yml +9 -0
- package/templates/synthetics.config.ts +35 -0
- package/CHANGELOG.md +0 -181
- package/NOTICE.txt +0 -221
- package/dist/parse_args.d.ts.map +0 -1
- package/dist/parse_args.js +0 -98
- package/dist/parse_args.js.map +0 -1
- package/dist/reporters/reporter.d.ts.map +0 -1
- package/dist/reporters/reporter.js.map +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -1895
- package/src/parse_args.ts +0 -146
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2020-present, Elastic NV
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in
|
|
14
|
+
* all copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
22
|
+
* THE SOFTWARE.
|
|
23
|
+
*
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import {
|
|
27
|
+
JavaScriptLanguageGenerator,
|
|
28
|
+
JavaScriptFormatter,
|
|
29
|
+
} from 'playwright-core/lib/server/recorder/javascript';
|
|
30
|
+
|
|
31
|
+
export type Step = {
|
|
32
|
+
actions: ActionInContext[];
|
|
33
|
+
name?: string;
|
|
34
|
+
};
|
|
35
|
+
export type Steps = Step[];
|
|
36
|
+
|
|
37
|
+
// from playwright-core
|
|
38
|
+
export type FrameDescription = {
|
|
39
|
+
pageAlias: string;
|
|
40
|
+
isMainFrame?: boolean;
|
|
41
|
+
url: string;
|
|
42
|
+
name?: string;
|
|
43
|
+
selectorsChain?: string[];
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export type ActionInContext = {
|
|
47
|
+
frameName?: string;
|
|
48
|
+
action: Action;
|
|
49
|
+
committed?: boolean;
|
|
50
|
+
modified?: boolean;
|
|
51
|
+
title?: string;
|
|
52
|
+
frame: FrameDescription;
|
|
53
|
+
isOpen?: boolean;
|
|
54
|
+
isSoftDeleted?: boolean;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export type Action = {
|
|
58
|
+
name: string;
|
|
59
|
+
selector?: string;
|
|
60
|
+
url?: string;
|
|
61
|
+
key?: string;
|
|
62
|
+
signals: Signal[];
|
|
63
|
+
modifiers?: number;
|
|
64
|
+
button?: 'left' | 'middle' | 'right';
|
|
65
|
+
clickCount?: number;
|
|
66
|
+
text?: string;
|
|
67
|
+
value?: string;
|
|
68
|
+
isAssert?: boolean;
|
|
69
|
+
command?: string;
|
|
70
|
+
files?: string[];
|
|
71
|
+
options?: string[];
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export type Signal = {
|
|
75
|
+
name: string;
|
|
76
|
+
url?: string;
|
|
77
|
+
isAsync?: boolean;
|
|
78
|
+
popupAlias?: string;
|
|
79
|
+
downloadAlias?: string;
|
|
80
|
+
dialogAlias?: string;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
function toAssertCall(pageAlias: string, action: Action) {
|
|
84
|
+
const { command, selector, value } = action;
|
|
85
|
+
switch (command) {
|
|
86
|
+
case 'textContent':
|
|
87
|
+
case 'innerText':
|
|
88
|
+
return `expect(await ${pageAlias}.${command}(${quote(
|
|
89
|
+
selector ?? ''
|
|
90
|
+
)})).toMatch(${quote(value ?? '')});`;
|
|
91
|
+
case 'isVisible':
|
|
92
|
+
case 'isHidden':
|
|
93
|
+
case 'isChecked':
|
|
94
|
+
case 'isEditable':
|
|
95
|
+
case 'isEnabled':
|
|
96
|
+
case 'isDisabled':
|
|
97
|
+
return `expect(await ${pageAlias}.${command}(${quote(
|
|
98
|
+
selector ?? ''
|
|
99
|
+
)})).toBeTruthy();`;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function toSignalMap(action: Action) {
|
|
104
|
+
let waitForNavigation: Signal | undefined;
|
|
105
|
+
let assertNavigation: Signal | undefined;
|
|
106
|
+
let popup: Signal | undefined;
|
|
107
|
+
let download: Signal | undefined;
|
|
108
|
+
let dialog: Signal | undefined;
|
|
109
|
+
for (const signal of action.signals) {
|
|
110
|
+
if (signal.name === 'navigation' && signal.isAsync)
|
|
111
|
+
waitForNavigation = signal;
|
|
112
|
+
else if (signal.name === 'navigation' && !signal.isAsync)
|
|
113
|
+
assertNavigation = signal;
|
|
114
|
+
else if (signal.name === 'popup') popup = signal;
|
|
115
|
+
else if (signal.name === 'download') download = signal;
|
|
116
|
+
else if (signal.name === 'dialog') dialog = signal;
|
|
117
|
+
}
|
|
118
|
+
return {
|
|
119
|
+
waitForNavigation,
|
|
120
|
+
assertNavigation,
|
|
121
|
+
popup,
|
|
122
|
+
download,
|
|
123
|
+
dialog,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function quote(text: string, char = "'"): string {
|
|
128
|
+
const stringified = JSON.stringify(text);
|
|
129
|
+
const escapedText = stringified
|
|
130
|
+
.substring(1, stringified.length - 1)
|
|
131
|
+
.replace(/\\"/g, '"');
|
|
132
|
+
if (char === "'") return char + escapedText.replace(/[']/g, "\\'") + char;
|
|
133
|
+
if (char === '"') return char + escapedText.replace(/["]/g, '\\"') + char;
|
|
134
|
+
if (char === '`') return char + escapedText.replace(/[`]/g, '`') + char;
|
|
135
|
+
throw new Error('Invalid escape char');
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
type Formattable = string | string[] | Record<string, unknown>;
|
|
139
|
+
|
|
140
|
+
function isFormattable(value: unknown): value is Formattable {
|
|
141
|
+
return (
|
|
142
|
+
typeof value === 'string' ||
|
|
143
|
+
(Array.isArray(value) && value.every(v => typeof v === 'string')) ||
|
|
144
|
+
typeof value === 'object'
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function formatObject(value: Formattable, indent = ' '): string {
|
|
149
|
+
if (typeof value === 'string') return quote(value);
|
|
150
|
+
if (Array.isArray(value))
|
|
151
|
+
return `[${value.map(o => formatObject(o)).join(', ')}]`;
|
|
152
|
+
if (typeof value === 'object') {
|
|
153
|
+
const keys = Object.keys(value);
|
|
154
|
+
if (!keys.length) return '{}';
|
|
155
|
+
const tokens: string[] = [];
|
|
156
|
+
for (const key of keys) {
|
|
157
|
+
const child = value[key];
|
|
158
|
+
if (child === undefined || !isFormattable(child)) continue;
|
|
159
|
+
tokens.push(`${key}: ${formatObject(child)}`);
|
|
160
|
+
}
|
|
161
|
+
return `{\n${indent}${tokens.join(`,\n${indent}`)}\n}`;
|
|
162
|
+
}
|
|
163
|
+
return String(value);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Generates an appropriate title string based on the action type/data.
|
|
168
|
+
* @param action Playwright action IR
|
|
169
|
+
* @returns title string
|
|
170
|
+
*/
|
|
171
|
+
export function actionTitle(action: Action) {
|
|
172
|
+
switch (action.name) {
|
|
173
|
+
case 'openPage':
|
|
174
|
+
return `Open new page`;
|
|
175
|
+
case 'closePage':
|
|
176
|
+
return `Close page`;
|
|
177
|
+
case 'check':
|
|
178
|
+
return `Check ${action.selector}`;
|
|
179
|
+
case 'uncheck':
|
|
180
|
+
return `Uncheck ${action.selector}`;
|
|
181
|
+
case 'click': {
|
|
182
|
+
if (action.clickCount === 1) return `Click ${action.selector}`;
|
|
183
|
+
if (action.clickCount === 2) return `Double click ${action.selector}`;
|
|
184
|
+
if (action.clickCount === 3) return `Triple click ${action.selector}`;
|
|
185
|
+
return `${action.clickCount}× click`;
|
|
186
|
+
}
|
|
187
|
+
case 'fill':
|
|
188
|
+
return `Fill ${action.selector}`;
|
|
189
|
+
case 'setInputFiles':
|
|
190
|
+
if (action.files?.length === 0) return `Clear selected files`;
|
|
191
|
+
else return `Upload ${action.files?.join(', ')}`;
|
|
192
|
+
case 'navigate':
|
|
193
|
+
return `Go to ${action.url}`;
|
|
194
|
+
case 'press':
|
|
195
|
+
return (
|
|
196
|
+
`Press ${action.key}` + (action.modifiers ? ' with modifiers' : '')
|
|
197
|
+
);
|
|
198
|
+
case 'select':
|
|
199
|
+
return `Select ${action.options?.join(', ')}`;
|
|
200
|
+
case 'assert':
|
|
201
|
+
return `Assert ${action.selector} ${action.command}`;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export class SyntheticsGenerator extends JavaScriptLanguageGenerator {
|
|
206
|
+
private previousContext?: ActionInContext;
|
|
207
|
+
private insideStep: boolean;
|
|
208
|
+
private varsToHoist: string[];
|
|
209
|
+
|
|
210
|
+
constructor(private isProject: boolean) {
|
|
211
|
+
super(true);
|
|
212
|
+
this.insideStep = false;
|
|
213
|
+
this.previousContext = undefined;
|
|
214
|
+
this.varsToHoist = [];
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Generate code for an action.
|
|
219
|
+
* @param actionInContext The action to create code for.
|
|
220
|
+
* @returns the strings generated for the action.
|
|
221
|
+
*/
|
|
222
|
+
generateAction(actionInContext: ActionInContext) {
|
|
223
|
+
const { action } = actionInContext;
|
|
224
|
+
const { pageAlias } = actionInContext.frame;
|
|
225
|
+
if (action.name === 'openPage') {
|
|
226
|
+
return '';
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// Don't cleanup page object managed by Synthetics
|
|
230
|
+
const isCleanUp = action.name === 'closePage' && pageAlias === 'page';
|
|
231
|
+
if (isCleanUp) {
|
|
232
|
+
return '';
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const stepIndent = this.insideStep ? 2 : 0;
|
|
236
|
+
const offset = this.isProject ? 2 + stepIndent : 0 + stepIndent;
|
|
237
|
+
const formatter = new JavaScriptFormatter(offset);
|
|
238
|
+
|
|
239
|
+
let subject: string;
|
|
240
|
+
if (actionInContext.frame.isMainFrame) {
|
|
241
|
+
subject = pageAlias;
|
|
242
|
+
} else if (
|
|
243
|
+
actionInContext.frame.selectorsChain &&
|
|
244
|
+
action.name !== 'navigate'
|
|
245
|
+
) {
|
|
246
|
+
const locators = actionInContext.frame.selectorsChain.map(
|
|
247
|
+
selector => `.frameLocator(${quote(selector)})`
|
|
248
|
+
);
|
|
249
|
+
subject = `${pageAlias}${locators.join('')}`;
|
|
250
|
+
} else if (actionInContext.frame.name) {
|
|
251
|
+
subject = `${pageAlias}.frame(${formatObject({
|
|
252
|
+
name: actionInContext.frame.name,
|
|
253
|
+
})})`;
|
|
254
|
+
} else {
|
|
255
|
+
subject = `${pageAlias}.frame(${formatObject({
|
|
256
|
+
url: actionInContext.frame.url,
|
|
257
|
+
})})`;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const signals = toSignalMap(action);
|
|
261
|
+
|
|
262
|
+
if (signals.dialog) {
|
|
263
|
+
formatter.add(` ${pageAlias}.once('dialog', dialog => {
|
|
264
|
+
console.log(\`Dialog message: $\{dialog.message()}\`);
|
|
265
|
+
dialog.dismiss().catch(() => {});
|
|
266
|
+
});`);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
if (signals.popup)
|
|
270
|
+
formatter.add(
|
|
271
|
+
`const ${signals.popup.popupAlias}Promise = ${pageAlias}.waitForEvent('popup');`
|
|
272
|
+
);
|
|
273
|
+
if (signals.download)
|
|
274
|
+
formatter.add(
|
|
275
|
+
`const download${signals.download.downloadAlias}Promise = ${pageAlias}.waitForEvent('download');`
|
|
276
|
+
);
|
|
277
|
+
|
|
278
|
+
// Add assertion from Synthetics.
|
|
279
|
+
const isAssert = action.name === 'assert' && action.isAssert;
|
|
280
|
+
if (isAssert && action.command) {
|
|
281
|
+
formatter.add(toAssertCall(pageAlias, action));
|
|
282
|
+
} else {
|
|
283
|
+
const actionCall = super._generateActionCall(action);
|
|
284
|
+
formatter.add(`await ${subject}.${actionCall};`);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
if (signals.popup)
|
|
288
|
+
formatter.add(
|
|
289
|
+
`${signals.popup.popupAlias} = await ${signals.popup.popupAlias}Promise;`
|
|
290
|
+
);
|
|
291
|
+
if (signals.download)
|
|
292
|
+
formatter.add(
|
|
293
|
+
`download${signals.download.downloadAlias} = await download${signals.download.downloadAlias}Promise;`
|
|
294
|
+
);
|
|
295
|
+
|
|
296
|
+
this.previousContext = actionInContext;
|
|
297
|
+
return formatter.format();
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
isNewStep(actioninContext: ActionInContext) {
|
|
301
|
+
const { action, frame } = actioninContext;
|
|
302
|
+
if (action.name === 'navigate') {
|
|
303
|
+
return true;
|
|
304
|
+
} else if (action.name === 'click') {
|
|
305
|
+
return (
|
|
306
|
+
this.previousContext?.frame.url === frame.url &&
|
|
307
|
+
action.signals.length > 0
|
|
308
|
+
);
|
|
309
|
+
}
|
|
310
|
+
return false;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
generateStepStart(name: string) {
|
|
314
|
+
this.insideStep = true;
|
|
315
|
+
const formatter = new JavaScriptFormatter(this.getDefaultOffset());
|
|
316
|
+
formatter.add(`step(${quote(name)}, async () => {`);
|
|
317
|
+
return formatter.format();
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
generateStepEnd() {
|
|
321
|
+
if (!this.insideStep) {
|
|
322
|
+
return '';
|
|
323
|
+
}
|
|
324
|
+
this.insideStep = false;
|
|
325
|
+
const formatter = new JavaScriptFormatter(this.getDefaultOffset());
|
|
326
|
+
formatter.add(`});`);
|
|
327
|
+
return formatter.format();
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
generateHeader() {
|
|
331
|
+
const formatter = new JavaScriptFormatter(0);
|
|
332
|
+
formatter.add(`
|
|
333
|
+
const { journey, step, expect } = require('@elastic/synthetics');
|
|
334
|
+
|
|
335
|
+
journey('Recorded journey', async ({ page, context }) => {`);
|
|
336
|
+
return formatter.format();
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
generateFooter() {
|
|
340
|
+
return `});`;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Generates JavaScript code from a custom set of steps and nested actions.
|
|
345
|
+
*
|
|
346
|
+
* This function makes no assumptions about where steps should be created,
|
|
347
|
+
* and instead follows the step definitions the caller has defined.
|
|
348
|
+
* @param steps IR to use for code generation
|
|
349
|
+
* @returns a list of the code strings outputted by the generator
|
|
350
|
+
*/
|
|
351
|
+
generateFromSteps(steps: Steps): string {
|
|
352
|
+
const text: string[] = [];
|
|
353
|
+
if (this.isProject) {
|
|
354
|
+
text.push(this.generateHeader());
|
|
355
|
+
}
|
|
356
|
+
this.varsToHoist = this.findVarsToHoist(steps);
|
|
357
|
+
text.push(this.generateHoistedVars());
|
|
358
|
+
for (const step of steps) {
|
|
359
|
+
if (step.actions.length === 0)
|
|
360
|
+
throw Error('Cannot process an empty step');
|
|
361
|
+
const name =
|
|
362
|
+
step.name ??
|
|
363
|
+
step.actions[0].title ??
|
|
364
|
+
actionTitle(step.actions[0].action);
|
|
365
|
+
text.push(this.generateStepStart(name ?? ''));
|
|
366
|
+
|
|
367
|
+
for (const action of step.actions) {
|
|
368
|
+
const actionText = this.generateAction(action);
|
|
369
|
+
if (actionText.length) text.push(actionText);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
text.push(this.generateStepEnd());
|
|
373
|
+
}
|
|
374
|
+
if (this.isProject) {
|
|
375
|
+
text.push(this.generateFooter());
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
return text.filter(s => !!s).join('\n');
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
generateHoistedVars() {
|
|
382
|
+
const formatter = new JavaScriptFormatter(this.getDefaultOffset());
|
|
383
|
+
for (const varName of this.varsToHoist) {
|
|
384
|
+
formatter.add(`let ${varName};`);
|
|
385
|
+
}
|
|
386
|
+
return formatter.format();
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
isVarHoisted(varName: string) {
|
|
390
|
+
return this.varsToHoist.indexOf(varName) >= 0;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
getDefaultOffset() {
|
|
394
|
+
return this.isProject ? 2 : 0;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* We need to hoist any page or popup alias that appears in more than one step.
|
|
399
|
+
* @param steps the step IR to evaluate
|
|
400
|
+
* @returns an array that contains the names of all variables that need to be hoisted
|
|
401
|
+
*/
|
|
402
|
+
findVarsToHoist(steps: Steps): string[] {
|
|
403
|
+
const aliasSet = new Set<string>();
|
|
404
|
+
for (const step of steps) {
|
|
405
|
+
for (const actionContext of step.actions) {
|
|
406
|
+
actionContext.action.signals
|
|
407
|
+
.filter(({ name, popupAlias }) => name === 'popup' && popupAlias)
|
|
408
|
+
.forEach(({ popupAlias }) => aliasSet.add(popupAlias as string));
|
|
409
|
+
aliasSet.add(actionContext.frame.pageAlias);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
return Array.from(aliasSet).filter(alias => alias !== 'page');
|
|
413
|
+
}
|
|
414
|
+
}
|