@elastic/synthetics 1.0.0-beta.2 → 1.0.0-beta.22
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/CHANGELOG.md +235 -0
- package/NOTICE.txt +250 -0
- package/README.md +2 -1
- package/dist/cli.js +87 -56
- package/dist/cli.js.map +1 -1
- package/dist/common_types.d.ts +116 -14
- package/dist/common_types.d.ts.map +1 -1
- package/dist/config.d.ts +2 -5
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +5 -5
- package/dist/config.js.map +1 -1
- package/dist/core/browser-service.js +5 -5
- package/dist/core/browser-service.js.map +1 -1
- package/dist/core/expect.d.ts +177 -0
- package/dist/core/expect.d.ts.map +1 -0
- package/dist/core/expect.js +33 -0
- package/dist/core/expect.js.map +1 -0
- package/dist/core/gatherer.d.ts +4 -7
- package/dist/core/gatherer.d.ts.map +1 -1
- package/dist/core/gatherer.js +39 -12
- package/dist/core/gatherer.js.map +1 -1
- package/dist/core/index.d.ts +5 -5
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +2 -2
- 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 +3 -16
- package/dist/core/logger.js.map +1 -1
- package/dist/core/runner.d.ts +54 -45
- package/dist/core/runner.d.ts.map +1 -1
- package/dist/core/runner.js +181 -71
- package/dist/core/runner.js.map +1 -1
- package/dist/dsl/journey.d.ts +13 -4
- package/dist/dsl/journey.d.ts.map +1 -1
- package/dist/dsl/journey.js +40 -1
- package/dist/dsl/journey.js.map +1 -1
- package/dist/formatter/javascript.d.ts +73 -0
- package/dist/formatter/javascript.d.ts.map +1 -0
- package/dist/formatter/javascript.js +274 -0
- package/dist/formatter/javascript.js.map +1 -0
- package/dist/helpers.d.ts +33 -12
- package/dist/helpers.d.ts.map +1 -1
- package/dist/helpers.js +108 -54
- package/dist/helpers.js.map +1 -1
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -7
- package/dist/index.js.map +1 -1
- package/dist/parse_args.d.ts +5 -2
- package/dist/parse_args.d.ts.map +1 -1
- package/dist/parse_args.js +59 -16
- package/dist/parse_args.js.map +1 -1
- package/dist/plugins/browser-console.d.ts +5 -8
- package/dist/plugins/browser-console.d.ts.map +1 -1
- package/dist/plugins/browser-console.js +27 -8
- package/dist/plugins/browser-console.js.map +1 -1
- package/dist/plugins/network.d.ts +23 -20
- package/dist/plugins/network.d.ts.map +1 -1
- package/dist/plugins/network.js +149 -124
- package/dist/plugins/network.js.map +1 -1
- package/dist/plugins/performance.d.ts +5 -5
- package/dist/plugins/performance.d.ts.map +1 -1
- package/dist/plugins/performance.js +5 -5
- package/dist/plugins/performance.js.map +1 -1
- package/dist/plugins/plugin-manager.d.ts +11 -11
- package/dist/plugins/plugin-manager.d.ts.map +1 -1
- package/dist/plugins/plugin-manager.js +40 -24
- package/dist/plugins/plugin-manager.js.map +1 -1
- package/dist/plugins/tracing.d.ts +10 -25
- package/dist/plugins/tracing.d.ts.map +1 -1
- package/dist/plugins/tracing.js +40 -26
- package/dist/plugins/tracing.js.map +1 -1
- package/dist/reporters/base.d.ts +0 -1
- package/dist/reporters/base.d.ts.map +1 -1
- package/dist/reporters/base.js +34 -32
- package/dist/reporters/base.js.map +1 -1
- package/dist/reporters/json.d.ts +46 -59
- package/dist/reporters/json.d.ts.map +1 -1
- package/dist/reporters/json.js +188 -70
- package/dist/reporters/json.js.map +1 -1
- package/dist/reporters/junit.d.ts.map +1 -1
- package/dist/reporters/junit.js +7 -7
- package/dist/reporters/junit.js.map +1 -1
- package/dist/sdk/lh-trace-processor.d.ts +304 -0
- package/dist/sdk/lh-trace-processor.d.ts.map +1 -0
- package/dist/sdk/lh-trace-processor.js +832 -0
- package/dist/sdk/lh-trace-processor.js.map +1 -0
- package/dist/sdk/trace-metrics.d.ts +61 -0
- package/dist/sdk/trace-metrics.d.ts.map +1 -0
- package/dist/sdk/trace-metrics.js +252 -0
- package/dist/sdk/trace-metrics.js.map +1 -0
- package/dist/sdk/trace-processor.d.ts +106 -0
- package/dist/sdk/trace-processor.d.ts.map +1 -0
- package/dist/sdk/trace-processor.js +74 -0
- package/dist/sdk/trace-processor.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1575
- package/package.json +27 -16
- package/src/cli.ts +68 -45
- package/src/common_types.ts +138 -14
- package/src/config.ts +2 -5
- package/src/core/expect.ts +187 -0
- package/src/core/gatherer.ts +48 -24
- package/src/core/index.ts +6 -6
- package/src/core/logger.ts +1 -12
- package/src/core/runner.ts +259 -112
- package/src/dsl/journey.ts +28 -6
- package/src/formatter/javascript.ts +330 -0
- package/src/helpers.ts +114 -24
- package/src/index.ts +7 -6
- package/src/parse_args.ts +97 -22
- package/src/plugins/browser-console.ts +30 -13
- package/src/plugins/network.ts +175 -159
- package/src/plugins/performance.ts +7 -7
- package/src/plugins/plugin-manager.ts +47 -36
- package/src/plugins/tracing.ts +43 -54
- package/src/reporters/base.ts +29 -23
- package/src/reporters/json.ts +259 -80
- package/src/reporters/junit.ts +1 -1
- package/src/sdk/lh-trace-processor.ts +1033 -0
- package/src/sdk/trace-metrics.ts +287 -0
- package/src/sdk/trace-processor.ts +147 -0
|
@@ -0,0 +1,330 @@
|
|
|
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-chromium/lib/server/supplements/recorder/javascript';
|
|
30
|
+
|
|
31
|
+
export type ActionInContext = {
|
|
32
|
+
pageAlias: string;
|
|
33
|
+
frameName?: string;
|
|
34
|
+
frameUrl: string;
|
|
35
|
+
isMainFrame: boolean;
|
|
36
|
+
action: Action;
|
|
37
|
+
committed?: boolean;
|
|
38
|
+
modified?: boolean;
|
|
39
|
+
title?: string;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
type Action = {
|
|
43
|
+
name: string;
|
|
44
|
+
selector?: string;
|
|
45
|
+
url?: string;
|
|
46
|
+
key?: string;
|
|
47
|
+
signals: Signal[];
|
|
48
|
+
modifiers?: number;
|
|
49
|
+
button?: 'left' | 'middle' | 'right';
|
|
50
|
+
clickCount?: number;
|
|
51
|
+
text?: string;
|
|
52
|
+
value?: string;
|
|
53
|
+
isAssert?: boolean;
|
|
54
|
+
command?: string;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
type Signal = {
|
|
58
|
+
name: string;
|
|
59
|
+
url?: string;
|
|
60
|
+
isAsync?: boolean;
|
|
61
|
+
popupAlias?: string;
|
|
62
|
+
downloadAlias?: string;
|
|
63
|
+
dialogAlias?: string;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export class SyntheticsGenerator extends JavaScriptLanguageGenerator {
|
|
67
|
+
private previousContext: ActionInContext;
|
|
68
|
+
private insideStep: boolean;
|
|
69
|
+
|
|
70
|
+
constructor(private isSuite: boolean) {
|
|
71
|
+
super(true);
|
|
72
|
+
this.insideStep = false;
|
|
73
|
+
this.previousContext = undefined;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
generateAction(actionInContext: ActionInContext) {
|
|
77
|
+
const { action, pageAlias, title } = actionInContext;
|
|
78
|
+
if (action.name === 'openPage') {
|
|
79
|
+
return '';
|
|
80
|
+
}
|
|
81
|
+
// Don't cleanup page object managed by Synthetics
|
|
82
|
+
const isCleanUp = action.name === 'closePage' && pageAlias === 'page';
|
|
83
|
+
if (isCleanUp) {
|
|
84
|
+
return '';
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
let formatter = new JavaScriptFormatter(this.isSuite ? 2 : 0);
|
|
88
|
+
// Check if it's a new step
|
|
89
|
+
const isNewStep = this.isNewStep(actionInContext);
|
|
90
|
+
if (isNewStep) {
|
|
91
|
+
if (this.insideStep) {
|
|
92
|
+
formatter.add(this.generateStepEnd());
|
|
93
|
+
}
|
|
94
|
+
formatter.add(this.generateStepStart(title || actionTitle(action)));
|
|
95
|
+
} else {
|
|
96
|
+
formatter = new JavaScriptFormatter(this.isSuite ? 4 : 2);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const subject = actionInContext.isMainFrame
|
|
100
|
+
? pageAlias
|
|
101
|
+
: actionInContext.frameName
|
|
102
|
+
? `${pageAlias}.frame(${formatObject({
|
|
103
|
+
name: actionInContext.frameName,
|
|
104
|
+
})})`
|
|
105
|
+
: `${pageAlias}.frame(${formatObject({
|
|
106
|
+
url: actionInContext.frameUrl,
|
|
107
|
+
})})`;
|
|
108
|
+
|
|
109
|
+
const signals = toSignalMap(action);
|
|
110
|
+
|
|
111
|
+
if (signals.dialog) {
|
|
112
|
+
formatter.add(` ${pageAlias}.once('dialog', dialog => {
|
|
113
|
+
console.log(\`Dialog message: $\{dialog.message()}\`);
|
|
114
|
+
dialog.dismiss().catch(() => {});
|
|
115
|
+
});`);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const emitPromiseAll =
|
|
119
|
+
signals.waitForNavigation || signals.popup || signals.download;
|
|
120
|
+
if (emitPromiseAll) {
|
|
121
|
+
// Generate either await Promise.all([]) or
|
|
122
|
+
// const [popup1] = await Promise.all([]).
|
|
123
|
+
let leftHandSide = '';
|
|
124
|
+
if (signals.popup)
|
|
125
|
+
leftHandSide = `const [${signals.popup.popupAlias}] = `;
|
|
126
|
+
else if (signals.download) leftHandSide = `const [download] = `;
|
|
127
|
+
formatter.add(`${leftHandSide}await Promise.all([`);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Popup signals.
|
|
131
|
+
if (signals.popup) formatter.add(`${pageAlias}.waitForEvent('popup'),`);
|
|
132
|
+
|
|
133
|
+
// Navigation signal.
|
|
134
|
+
if (signals.waitForNavigation)
|
|
135
|
+
formatter.add(
|
|
136
|
+
`${pageAlias}.waitForNavigation(/*{ url: ${quote(
|
|
137
|
+
signals.waitForNavigation.url
|
|
138
|
+
)} }*/),`
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
// Download signals.
|
|
142
|
+
if (signals.download)
|
|
143
|
+
formatter.add(`${pageAlias}.waitForEvent('download'),`);
|
|
144
|
+
|
|
145
|
+
const prefix =
|
|
146
|
+
signals.popup || signals.waitForNavigation || signals.download
|
|
147
|
+
? ''
|
|
148
|
+
: 'await ';
|
|
149
|
+
const actionCall = super._generateActionCall(action);
|
|
150
|
+
// Add assertion from Synthetics.
|
|
151
|
+
const isAssert = action.name === 'assert' && action.isAssert;
|
|
152
|
+
|
|
153
|
+
if (!isAssert) {
|
|
154
|
+
const suffix = signals.waitForNavigation || emitPromiseAll ? '' : ';';
|
|
155
|
+
formatter.add(`${prefix}${subject}.${actionCall}${suffix}`);
|
|
156
|
+
|
|
157
|
+
if (emitPromiseAll) {
|
|
158
|
+
formatter.add(`]);`);
|
|
159
|
+
} else if (signals.assertNavigation) {
|
|
160
|
+
formatter.add(
|
|
161
|
+
` expect(${pageAlias}.url()).toBe(${quote(
|
|
162
|
+
signals.assertNavigation.url
|
|
163
|
+
)});`
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
} else if (action.command) {
|
|
167
|
+
formatter.add(toAssertCall(pageAlias, action));
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
this.previousContext = actionInContext;
|
|
171
|
+
return formatter.format();
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
isNewStep(actioninContext: ActionInContext) {
|
|
175
|
+
const { action, frameUrl } = actioninContext;
|
|
176
|
+
if (action.name === 'navigate') {
|
|
177
|
+
return true;
|
|
178
|
+
} else if (action.name === 'click') {
|
|
179
|
+
return (
|
|
180
|
+
this.previousContext?.frameUrl === frameUrl && action.signals.length > 0
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
generateStepStart(name) {
|
|
187
|
+
this.insideStep = true;
|
|
188
|
+
return `step(${quote(name)}, async () => {`;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
generateStepEnd() {
|
|
192
|
+
if (!this.insideStep) {
|
|
193
|
+
return '';
|
|
194
|
+
}
|
|
195
|
+
this.insideStep = false;
|
|
196
|
+
const formatter = new JavaScriptFormatter(this.isSuite ? 2 : 0);
|
|
197
|
+
formatter.add(`});`);
|
|
198
|
+
return formatter.format();
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
generateHeader() {
|
|
202
|
+
const formatter = new JavaScriptFormatter(0);
|
|
203
|
+
formatter.add(`
|
|
204
|
+
const { journey, step, expect } = require('@elastic/synthetics');
|
|
205
|
+
|
|
206
|
+
journey('Recorded journey', async ({ page, context }) => {`);
|
|
207
|
+
return formatter.format();
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
generateFooter() {
|
|
211
|
+
return `});`;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
generateText(actions: Array<ActionInContext>) {
|
|
215
|
+
const text = [];
|
|
216
|
+
if (this.isSuite) {
|
|
217
|
+
text.push(this.generateHeader());
|
|
218
|
+
}
|
|
219
|
+
for (let i = 0; i < actions.length; i++) {
|
|
220
|
+
text.push(this.generateAction(actions[i]));
|
|
221
|
+
if (i === actions.length - 1) text.push(this.generateStepEnd());
|
|
222
|
+
}
|
|
223
|
+
if (this.isSuite) {
|
|
224
|
+
text.push(this.generateFooter());
|
|
225
|
+
}
|
|
226
|
+
return text.filter(t => Boolean(t)).join('\n');
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// TODO: Replace once Playwright releases new version
|
|
231
|
+
export function quote(text: string, char = "'") {
|
|
232
|
+
const stringified = JSON.stringify(text);
|
|
233
|
+
const escapedText = stringified
|
|
234
|
+
.substring(1, stringified.length - 1)
|
|
235
|
+
.replace(/\\"/g, '"');
|
|
236
|
+
if (char === "'") return char + escapedText.replace(/[']/g, "\\'") + char;
|
|
237
|
+
if (char === '"') return char + escapedText.replace(/["]/g, '\\"') + char;
|
|
238
|
+
if (char === '`') return char + escapedText.replace(/[`]/g, '`') + char;
|
|
239
|
+
throw new Error('Invalid escape char');
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function toSignalMap(action) {
|
|
243
|
+
let waitForNavigation;
|
|
244
|
+
let assertNavigation;
|
|
245
|
+
let popup;
|
|
246
|
+
let download;
|
|
247
|
+
let dialog;
|
|
248
|
+
for (const signal of action.signals) {
|
|
249
|
+
if (signal.name === 'navigation' && signal.isAsync)
|
|
250
|
+
waitForNavigation = signal;
|
|
251
|
+
else if (signal.name === 'navigation' && !signal.isAsync)
|
|
252
|
+
assertNavigation = signal;
|
|
253
|
+
else if (signal.name === 'popup') popup = signal;
|
|
254
|
+
else if (signal.name === 'download') download = signal;
|
|
255
|
+
else if (signal.name === 'dialog') dialog = signal;
|
|
256
|
+
}
|
|
257
|
+
return {
|
|
258
|
+
waitForNavigation,
|
|
259
|
+
assertNavigation,
|
|
260
|
+
popup,
|
|
261
|
+
download,
|
|
262
|
+
dialog,
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function toAssertCall(pageAlias, action) {
|
|
267
|
+
const { command, selector, value } = action;
|
|
268
|
+
switch (command) {
|
|
269
|
+
case 'textContent':
|
|
270
|
+
case 'innerText':
|
|
271
|
+
return `expect(await ${pageAlias}.${command}(${quote(
|
|
272
|
+
selector
|
|
273
|
+
)})).toMatch(${quote(value)});`;
|
|
274
|
+
case 'isVisible':
|
|
275
|
+
case 'isHidden':
|
|
276
|
+
case 'isChecked':
|
|
277
|
+
case 'isEditable':
|
|
278
|
+
case 'isEnabled':
|
|
279
|
+
case 'isDisabled':
|
|
280
|
+
return `expect(await ${pageAlias}.${command}(${quote(
|
|
281
|
+
selector
|
|
282
|
+
)})).toBeTruthy();`;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function formatObject(value, indent = ' ') {
|
|
287
|
+
if (typeof value === 'string') return quote(value);
|
|
288
|
+
if (Array.isArray(value))
|
|
289
|
+
return `[${value.map(o => formatObject(o)).join(', ')}]`;
|
|
290
|
+
if (typeof value === 'object') {
|
|
291
|
+
const keys = Object.keys(value);
|
|
292
|
+
if (!keys.length) return '{}';
|
|
293
|
+
const tokens = [];
|
|
294
|
+
for (const key of keys) tokens.push(`${key}: ${formatObject(value[key])}`);
|
|
295
|
+
return `{\n${indent}${tokens.join(`,\n${indent}`)}\n}`;
|
|
296
|
+
}
|
|
297
|
+
return String(value);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function actionTitle(action) {
|
|
301
|
+
switch (action.name) {
|
|
302
|
+
case 'openPage':
|
|
303
|
+
return `Open new page`;
|
|
304
|
+
case 'closePage':
|
|
305
|
+
return `Close page`;
|
|
306
|
+
case 'check':
|
|
307
|
+
return `Check ${action.selector}`;
|
|
308
|
+
case 'uncheck':
|
|
309
|
+
return `Uncheck ${action.selector}`;
|
|
310
|
+
case 'click': {
|
|
311
|
+
if (action.clickCount === 1) return `Click ${action.selector}`;
|
|
312
|
+
if (action.clickCount === 2) return `Double click ${action.selector}`;
|
|
313
|
+
if (action.clickCount === 3) return `Triple click ${action.selector}`;
|
|
314
|
+
return `${action.clickCount}× click`;
|
|
315
|
+
}
|
|
316
|
+
case 'fill':
|
|
317
|
+
return `Fill ${action.selector}`;
|
|
318
|
+
case 'setInputFiles':
|
|
319
|
+
if (action.files.length === 0) return `Clear selected files`;
|
|
320
|
+
else return `Upload ${action.files.join(', ')}`;
|
|
321
|
+
case 'navigate':
|
|
322
|
+
return `Go to ${action.url}`;
|
|
323
|
+
case 'press':
|
|
324
|
+
return (
|
|
325
|
+
`Press ${action.key}` + (action.modifiers ? ' with modifiers' : '')
|
|
326
|
+
);
|
|
327
|
+
case 'select':
|
|
328
|
+
return `Select ${action.options.join(', ')}`;
|
|
329
|
+
}
|
|
330
|
+
}
|
package/src/helpers.ts
CHANGED
|
@@ -26,12 +26,18 @@
|
|
|
26
26
|
import { red, green, yellow, cyan } from 'kleur/colors';
|
|
27
27
|
import os from 'os';
|
|
28
28
|
import { resolve, join, dirname } from 'path';
|
|
29
|
-
import fs
|
|
29
|
+
import fs from 'fs';
|
|
30
30
|
import { promisify } from 'util';
|
|
31
31
|
import { performance } from 'perf_hooks';
|
|
32
|
+
import { HooksArgs, HooksCallback, NetworkConditions } from './common_types';
|
|
33
|
+
|
|
34
|
+
const lstatAsync = promisify(fs.lstat);
|
|
35
|
+
const readdirAsync = promisify(fs.readdir);
|
|
36
|
+
|
|
37
|
+
export const readFileAsync = promisify(fs.readFile);
|
|
38
|
+
export const writeFileAsync = promisify(fs.writeFile);
|
|
39
|
+
export const mkdirAsync = promisify(fs.mkdir);
|
|
32
40
|
|
|
33
|
-
const statAsync = promisify(fs.lstat);
|
|
34
|
-
const readAsync = promisify(fs.readdir);
|
|
35
41
|
const SEPARATOR = '\n';
|
|
36
42
|
|
|
37
43
|
export function noop() {}
|
|
@@ -40,37 +46,47 @@ export function indent(lines: string, tab = ' ') {
|
|
|
40
46
|
return lines.replace(/^/gm, tab);
|
|
41
47
|
}
|
|
42
48
|
|
|
49
|
+
/**
|
|
50
|
+
* Disable unicode symbols for windows, the underlying
|
|
51
|
+
* FS stream has a known issue in windows
|
|
52
|
+
*/
|
|
53
|
+
const NO_UTF8_SUPPORT = process.platform === 'win32';
|
|
43
54
|
export const symbols = {
|
|
44
|
-
warning: yellow('⚠'),
|
|
55
|
+
warning: yellow(NO_UTF8_SUPPORT ? '!' : '⚠'),
|
|
45
56
|
skipped: cyan('-'),
|
|
46
|
-
succeeded: green('✓'),
|
|
47
|
-
failed: red('✖'),
|
|
57
|
+
succeeded: green(NO_UTF8_SUPPORT ? 'ok' : '✓'),
|
|
58
|
+
failed: red(NO_UTF8_SUPPORT ? 'x' : '✖'),
|
|
48
59
|
};
|
|
49
60
|
|
|
61
|
+
export function generateUniqueId() {
|
|
62
|
+
return `${Date.now() + Math.floor(Math.random() * 1e13)}`;
|
|
63
|
+
}
|
|
64
|
+
|
|
50
65
|
export function generateTempPath() {
|
|
51
|
-
return join(os.tmpdir(), `synthetics-${
|
|
66
|
+
return join(os.tmpdir(), `synthetics-${generateUniqueId()}`);
|
|
52
67
|
}
|
|
53
68
|
|
|
54
69
|
/**
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
70
|
+
* Get Monotonically increasing time in seconds since
|
|
71
|
+
* an arbitrary point in the past.
|
|
72
|
+
*
|
|
73
|
+
* We internally use the monotonically increasing clock timing
|
|
74
|
+
* similar to the chrome devtools protocol network events for
|
|
75
|
+
* journey,step start/end fields to make querying in the UI easier
|
|
59
76
|
*/
|
|
60
|
-
export function
|
|
77
|
+
export function monotonicTimeInSeconds() {
|
|
61
78
|
const hrTime = process.hrtime(); // [seconds, nanoseconds]
|
|
62
79
|
return hrTime[0] * 1 + hrTime[1] / 1e9;
|
|
63
80
|
}
|
|
64
81
|
|
|
65
82
|
/**
|
|
66
|
-
* Converts the trace events timestamp field from
|
|
67
|
-
*
|
|
68
|
-
* the internal timestamp similar to other event types (journey, step, etc)
|
|
83
|
+
* Converts the trace events timestamp field from microsecond
|
|
84
|
+
* resolution to monotonic seconds timestamp similar to other event types (journey, step, etc)
|
|
69
85
|
* Reference - https://github.com/samccone/chrome-trace-event/blob/d45bc8af3b5c53a3adfa2c5fc107b4fae054f579/lib/trace-event.ts#L21-L22
|
|
70
86
|
*
|
|
71
87
|
* Tested and verified on both Darwin and Linux
|
|
72
88
|
*/
|
|
73
|
-
export function
|
|
89
|
+
export function microSecsToSeconds(ts: number) {
|
|
74
90
|
return ts / 1e6;
|
|
75
91
|
}
|
|
76
92
|
|
|
@@ -90,10 +106,13 @@ export function now() {
|
|
|
90
106
|
}
|
|
91
107
|
|
|
92
108
|
/**
|
|
93
|
-
* Execute all the callbacks in parallel using Promise.all
|
|
109
|
+
* Execute all the hooks callbacks in parallel using Promise.all
|
|
94
110
|
*/
|
|
95
|
-
export async function runParallel(
|
|
96
|
-
|
|
111
|
+
export async function runParallel(
|
|
112
|
+
callbacks: Array<HooksCallback>,
|
|
113
|
+
args: HooksArgs
|
|
114
|
+
) {
|
|
115
|
+
const promises = callbacks.map(cb => cb(args));
|
|
97
116
|
return await Promise.all(promises);
|
|
98
117
|
}
|
|
99
118
|
|
|
@@ -105,12 +124,12 @@ export function isDepInstalled(dep) {
|
|
|
105
124
|
}
|
|
106
125
|
}
|
|
107
126
|
|
|
108
|
-
export
|
|
109
|
-
return (
|
|
127
|
+
export function isDirectory(path) {
|
|
128
|
+
return fs.existsSync(path) && fs.statSync(path).isDirectory();
|
|
110
129
|
}
|
|
111
130
|
|
|
112
131
|
export function isFile(filePath) {
|
|
113
|
-
return fs.existsSync(filePath) && statSync(filePath).isFile();
|
|
132
|
+
return fs.existsSync(filePath) && fs.statSync(filePath).isFile();
|
|
114
133
|
}
|
|
115
134
|
|
|
116
135
|
/**
|
|
@@ -150,11 +169,11 @@ export async function totalist(
|
|
|
150
169
|
pre = ''
|
|
151
170
|
) {
|
|
152
171
|
dir = resolve('.', dir);
|
|
153
|
-
await
|
|
172
|
+
await readdirAsync(dir).then(arr => {
|
|
154
173
|
return Promise.all(
|
|
155
174
|
arr.map(str => {
|
|
156
175
|
const abs = join(dir, str);
|
|
157
|
-
return
|
|
176
|
+
return lstatAsync(abs).then(stats =>
|
|
158
177
|
stats.isDirectory()
|
|
159
178
|
? totalist(abs, callback, join(pre, str))
|
|
160
179
|
: callback(join(pre, str), abs)
|
|
@@ -225,3 +244,74 @@ export function formatError(error: Error) {
|
|
|
225
244
|
stack: rewriteErrorStack(stack, indexes),
|
|
226
245
|
};
|
|
227
246
|
}
|
|
247
|
+
|
|
248
|
+
const cwd = process.cwd();
|
|
249
|
+
/**
|
|
250
|
+
* Synthetics cache path that is based on the process id to make sure
|
|
251
|
+
* each process does not modify the caching layer used by other process
|
|
252
|
+
* once we move to executing journeys in parallel
|
|
253
|
+
*/
|
|
254
|
+
export const CACHE_PATH = join(cwd, '.synthetics', process.pid.toString());
|
|
255
|
+
|
|
256
|
+
export function getDurationInUs(duration: number) {
|
|
257
|
+
return Math.trunc(duration * 1e6);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export function megabitsToBytes(megabytes: number) {
|
|
261
|
+
return (megabytes * 1024 * 1024) / 8;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export function bytesToMegabits(bytes: number) {
|
|
265
|
+
return (bytes / 1024 / 1024) * 8;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
export const DEFAULT_NETWORK_CONDITIONS: NetworkConditions = {
|
|
269
|
+
downloadThroughput: megabitsToBytes(5), // Devtools CDP expects format to be in bytes/second
|
|
270
|
+
uploadThroughput: megabitsToBytes(3), // Devtools CDP expects format to be in bytes/second
|
|
271
|
+
latency: 20, // milliseconds,
|
|
272
|
+
offline: false,
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
// Tranforms CDP dev tools format back to cli args format
|
|
276
|
+
export function formatNetworkConditionsArgs(
|
|
277
|
+
networkConditions: NetworkConditions
|
|
278
|
+
) {
|
|
279
|
+
const d = bytesToMegabits(networkConditions.downloadThroughput);
|
|
280
|
+
const u = bytesToMegabits(networkConditions.uploadThroughput);
|
|
281
|
+
const l = networkConditions.latency;
|
|
282
|
+
return `${d}d/${u}u/${l}l`;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export const DEFAULT_NETWORK_CONDITIONS_ARG = formatNetworkConditionsArgs(
|
|
286
|
+
DEFAULT_NETWORK_CONDITIONS
|
|
287
|
+
);
|
|
288
|
+
|
|
289
|
+
export function parseNetworkConditions(args: string): NetworkConditions {
|
|
290
|
+
const uploadToken = 'u';
|
|
291
|
+
const downloadToken = 'd';
|
|
292
|
+
const latencyToken = 'l';
|
|
293
|
+
const networkConditions = {
|
|
294
|
+
...DEFAULT_NETWORK_CONDITIONS,
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
const conditions = args.split('/');
|
|
298
|
+
|
|
299
|
+
conditions.forEach(condition => {
|
|
300
|
+
const value = condition.slice(0, condition.length - 1);
|
|
301
|
+
const token = condition.slice(-1);
|
|
302
|
+
|
|
303
|
+
switch (token) {
|
|
304
|
+
case uploadToken:
|
|
305
|
+
networkConditions.uploadThroughput = megabitsToBytes(Number(value));
|
|
306
|
+
break;
|
|
307
|
+
case downloadToken:
|
|
308
|
+
networkConditions.downloadThroughput = megabitsToBytes(Number(value));
|
|
309
|
+
break;
|
|
310
|
+
case latencyToken:
|
|
311
|
+
networkConditions.latency = Number(value);
|
|
312
|
+
break;
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
return networkConditions;
|
|
317
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -25,7 +25,6 @@
|
|
|
25
25
|
|
|
26
26
|
import { runner } from './core';
|
|
27
27
|
import { RunOptions } from './core/runner';
|
|
28
|
-
import { setLogger } from './core/logger';
|
|
29
28
|
import sourceMapSupport from 'source-map-support';
|
|
30
29
|
|
|
31
30
|
export async function run(options: RunOptions) {
|
|
@@ -35,11 +34,6 @@ export async function run(options: RunOptions) {
|
|
|
35
34
|
sourceMapSupport.install({
|
|
36
35
|
environment: 'node',
|
|
37
36
|
});
|
|
38
|
-
/**
|
|
39
|
-
* set up logger with appropriate file descriptor
|
|
40
|
-
* to capture all the DEBUG logs when run through heartbeat
|
|
41
|
-
*/
|
|
42
|
-
setLogger(options.outfd);
|
|
43
37
|
|
|
44
38
|
try {
|
|
45
39
|
return await runner.run(options);
|
|
@@ -49,7 +43,11 @@ export async function run(options: RunOptions) {
|
|
|
49
43
|
}
|
|
50
44
|
}
|
|
51
45
|
|
|
46
|
+
/**
|
|
47
|
+
* Export all core module functions
|
|
48
|
+
*/
|
|
52
49
|
export { beforeAll, afterAll, journey, step, before, after } from './core';
|
|
50
|
+
export { expect } from './core/expect';
|
|
53
51
|
/**
|
|
54
52
|
* Export all the driver related types to be consumed
|
|
55
53
|
* and used by suites
|
|
@@ -66,3 +64,6 @@ export type {
|
|
|
66
64
|
*/
|
|
67
65
|
export type { default as Runner } from './core/runner';
|
|
68
66
|
export type { Reporter, ReporterOptions } from './reporters';
|
|
67
|
+
|
|
68
|
+
export type { SyntheticsConfig } from './common_types';
|
|
69
|
+
export type { ActionInContext } from './formatter/javascript';
|