@testomatio/reporter 1.1.0-beta → 1.1.0-beta-3
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 +355 -0
- package/README.md +13 -9
- package/lib/adapter/codecept.js +59 -39
- package/lib/adapter/cucumber/current.js +95 -59
- package/lib/adapter/cucumber/legacy.js +25 -11
- package/lib/adapter/cypress-plugin/index.js +1 -1
- package/lib/adapter/jasmine.js +1 -1
- package/lib/adapter/jest.js +12 -11
- package/lib/adapter/mocha.js +22 -50
- package/lib/adapter/playwright.js +40 -21
- package/lib/adapter/webdriver.js +1 -1
- package/lib/bin/reportXml.js +1 -0
- package/lib/bin/startTest.js +25 -4
- package/lib/client.js +117 -52
- package/lib/constants.js +4 -6
- package/lib/junit-adapter/java.js +27 -9
- package/lib/pipe/csv.js +4 -1
- package/lib/pipe/github.js +24 -37
- package/lib/pipe/gitlab.js +5 -16
- package/lib/pipe/testomatio.js +138 -34
- package/lib/reporter-functions.js +43 -0
- package/lib/reporter.js +13 -5
- package/lib/storages/artifact-storage.js +70 -0
- package/lib/storages/data-storage.js +307 -0
- package/lib/storages/key-value-storage.js +58 -0
- package/lib/storages/logger.js +290 -0
- package/lib/utils/pipe_utils.js +135 -0
- package/lib/{util.js → utils/utils.js} +130 -10
- package/lib/xmlReader.js +132 -44
- package/package.json +9 -6
- package/lib/_ArtifactStorageOld.js +0 -141
- package/lib/artifactStorage.js +0 -25
- package/lib/dataStorage.js +0 -172
- package/lib/logger.js +0 -221
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
const debug = require('debug')('@testomatio/reporter:storage');
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const os = require('os');
|
|
5
|
+
const { join } = require('path');
|
|
6
|
+
const { TESTOMAT_TMP_STORAGE_DIR } = require('../constants');
|
|
7
|
+
const { fileSystem, jestHelpers, parseTest } = require('../utils/utils');
|
|
8
|
+
|
|
9
|
+
const getTestIdFromTestTitle = parseTest;
|
|
10
|
+
|
|
11
|
+
class DataStorage {
|
|
12
|
+
/**
|
|
13
|
+
* Creates data storage instance for specific data type.
|
|
14
|
+
* Stores data to global variable or to file depending on what is applicable for current test runner
|
|
15
|
+
* (running environment).
|
|
16
|
+
* Recommend to use composition while using this class (instead of inheritance).
|
|
17
|
+
* ! Also the class which will use data storage should be singleton (to avoid data loss).
|
|
18
|
+
* @param {*} dataType storage type (like 'log', 'artifact'); could be any string, used only to define file path
|
|
19
|
+
*/
|
|
20
|
+
constructor(dataType) {
|
|
21
|
+
this.dataType = dataType || 'data';
|
|
22
|
+
this.isFileStorage = true;
|
|
23
|
+
this.#refreshStorageType();
|
|
24
|
+
|
|
25
|
+
this.dataDirPath = path.join(TESTOMAT_TMP_STORAGE_DIR, this.dataType);
|
|
26
|
+
|
|
27
|
+
// set test title for mocha (could not be moved to adapter)
|
|
28
|
+
// it does not override existing hook, just add new one
|
|
29
|
+
try {
|
|
30
|
+
// @ts-ignore
|
|
31
|
+
if (!beforeEach) return;
|
|
32
|
+
// @ts-ignore-next-line
|
|
33
|
+
beforeEach(function () {
|
|
34
|
+
if (this.currentTest?.__mocha_id__) {
|
|
35
|
+
global.testTitle = this.currentTest.fullTitle();
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
} catch (e) {
|
|
39
|
+
// ignore
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// set test title for playwright
|
|
43
|
+
try {
|
|
44
|
+
// eslint-disable-next-line import/no-extraneous-dependencies, import/no-unresolved
|
|
45
|
+
const { test } = require('@playwright/test');
|
|
46
|
+
// eslint-disable-next-line no-empty-pattern
|
|
47
|
+
test.beforeEach(async ({}, testInfo) => {
|
|
48
|
+
global.testTitle = testInfo.title;
|
|
49
|
+
global.testomatioRunningEnvironment = 'playwright';
|
|
50
|
+
});
|
|
51
|
+
} catch (e) {
|
|
52
|
+
// ignore
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Try to define the running environment. Not 100% reliable and used as additional check
|
|
58
|
+
* @returns jest | mocha | ...
|
|
59
|
+
*/
|
|
60
|
+
getRunningEnviroment() {
|
|
61
|
+
// jest
|
|
62
|
+
if (process.env.JEST_WORKER_ID) return 'jest';
|
|
63
|
+
|
|
64
|
+
if (global.codeceptjs) return 'codeceptjs';
|
|
65
|
+
|
|
66
|
+
// 'cucumber:current', 'cucumber:legacy'
|
|
67
|
+
if (global.testomatioRunningEnvironment) return global.testomatioRunningEnvironment;
|
|
68
|
+
|
|
69
|
+
if (process.env.PLAYWRIGHT_TEST_BASE_URL || process.env.TEST_WORKER_INDEX) return 'playwright';
|
|
70
|
+
|
|
71
|
+
// mocha - can't detect
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Puts any data to storage (file or global variable).
|
|
77
|
+
* If file: stores data as text, if global variable – stores as array of data.
|
|
78
|
+
* @param {*} data anything you want to store (string, object, array, etc)
|
|
79
|
+
* @param {*} context could be testId or any context (test, suite, etc) which is used to define testId
|
|
80
|
+
* @returns
|
|
81
|
+
*/
|
|
82
|
+
putData(data, context = null) {
|
|
83
|
+
fileSystem.createDir(this.dataDirPath);
|
|
84
|
+
|
|
85
|
+
this.#refreshStorageType();
|
|
86
|
+
context = context ?? global.testTitle ?? null;
|
|
87
|
+
let testId = this.#tryToRetrieveTestId(context) || context || null;
|
|
88
|
+
|
|
89
|
+
if (this.runningEnvironment === 'codeceptjs') {
|
|
90
|
+
this.isFileStorage = false;
|
|
91
|
+
testId = testId ?? global.testomatioDataStore?.currentlyRunningTestId;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (this.runningEnvironment === 'jest') {
|
|
95
|
+
testId = testId ?? jestHelpers.getIdOfCurrentlyRunningTest();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// logs in playwright are gathered by pw framework itself
|
|
99
|
+
if (this.runningEnvironment === 'playwright' && this.dataType === 'log') return;
|
|
100
|
+
|
|
101
|
+
// get id from global store
|
|
102
|
+
if (global.testomatioDataStore && global.testomatioDataStore.currentlyRunningTestId)
|
|
103
|
+
testId = testId ?? global.testomatioDataStore?.currentlyRunningTestId;
|
|
104
|
+
|
|
105
|
+
if (!testId && global?.currentlyRunningTestTitle)
|
|
106
|
+
testId = this.#tryToRetrieveTestId(global.currentlyRunningTestTitle);
|
|
107
|
+
|
|
108
|
+
if (!testId) {
|
|
109
|
+
debug(`No test id provided for "${this.dataType}" data:`, data);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (this.isFileStorage) {
|
|
114
|
+
this.#putDataToFile(data, testId);
|
|
115
|
+
} else {
|
|
116
|
+
this.#putDataToGlobalVar(data, testId);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Returns data, stored for specific testId (or data which was stored without test id specified).
|
|
122
|
+
* This method will get data from global variable and/or from from file (previosly saved with put method).
|
|
123
|
+
*
|
|
124
|
+
* @param {*} context could be testId or any context (test title, suite, etc) which is used to define testId
|
|
125
|
+
* @returns array of data (any type), null (if no data found for test) or string (if data type is log)
|
|
126
|
+
*/
|
|
127
|
+
getData(context) {
|
|
128
|
+
this.#refreshStorageType();
|
|
129
|
+
|
|
130
|
+
let testId = null;
|
|
131
|
+
if (typeof context === 'string') {
|
|
132
|
+
testId = this.#tryToRetrieveTestId(context) || context;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (!testId) {
|
|
136
|
+
debug(`Cannot get test id from passed context:`, context);
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
let testDataFromFile = [];
|
|
141
|
+
let testDataFromGlobalVar = [];
|
|
142
|
+
|
|
143
|
+
if (global?.testomatioDataStore) {
|
|
144
|
+
testDataFromGlobalVar = this.#getDataFromGlobalVar(testId);
|
|
145
|
+
// these frameworks use global variable storage
|
|
146
|
+
// if (testDataFromGlobalVar && ['cucumber', 'codeceptjs'].includes(this.runningEnvironment)) {
|
|
147
|
+
if (testDataFromGlobalVar) {
|
|
148
|
+
// return as string if its log data
|
|
149
|
+
if (this.dataType === 'log' && testDataFromGlobalVar.length) return testDataFromGlobalVar.join('\n');
|
|
150
|
+
// return as array for other data types
|
|
151
|
+
if (testDataFromGlobalVar.length) return testDataFromGlobalVar;
|
|
152
|
+
}
|
|
153
|
+
// don't return nothing if no data in global variable
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/* condition is removed for mocha
|
|
157
|
+
mocha has created a global storage, but just in some cases, generally it is not available
|
|
158
|
+
*/
|
|
159
|
+
// if (this.isFileStorage || !testData) {
|
|
160
|
+
// testData = this.#getDataFromFile(testId);
|
|
161
|
+
// if (testData) return testData;
|
|
162
|
+
// }
|
|
163
|
+
|
|
164
|
+
testDataFromFile = this.#getDataFromFile(testId);
|
|
165
|
+
|
|
166
|
+
if (testDataFromFile.length) {
|
|
167
|
+
if (this.dataType === 'log') return testDataFromFile.join('\n');
|
|
168
|
+
return testDataFromFile;
|
|
169
|
+
}
|
|
170
|
+
debug(`No "${this.dataType}" data for test id ${testId} in both file and global variable`);
|
|
171
|
+
|
|
172
|
+
// in case no data found for test
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* This method is named as "try" because it does not guarantee that test id could be retrieved.
|
|
178
|
+
* Context could be anything (test, suite, string, etc) which is used to define testId.
|
|
179
|
+
* Or it could represent any other entity (which does not contain test id).
|
|
180
|
+
* @param {*} context
|
|
181
|
+
*/
|
|
182
|
+
#tryToRetrieveTestId(context) {
|
|
183
|
+
if (!context) return null;
|
|
184
|
+
this.#refreshStorageType();
|
|
185
|
+
|
|
186
|
+
if (this.runningEnvironment === 'playwright' || context?.title) {
|
|
187
|
+
// context is testInfo
|
|
188
|
+
const testId = getTestIdFromTestTitle(context.title);
|
|
189
|
+
if (testId) return testId;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (typeof context === 'string') {
|
|
193
|
+
const testId = getTestIdFromTestTitle(context);
|
|
194
|
+
if (testId) return testId;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Refreshes storage type (file or global variable) depending on current environment
|
|
202
|
+
* This method should be run on each attempt to put or get data from/to storage
|
|
203
|
+
* Because storage instance is created before the test runner is started. And storage type could be changed
|
|
204
|
+
*/
|
|
205
|
+
#refreshStorageType() {
|
|
206
|
+
this.runningEnvironment = this.getRunningEnviroment();
|
|
207
|
+
|
|
208
|
+
// some test frameworks do not persist global variables, thus file storage is used for them (by default)
|
|
209
|
+
if (this.runningEnvironment === 'codeceptjs') this.isFileStorage = false;
|
|
210
|
+
// playwrigth stores logs by itself; but file storage is used for other data types for playwright
|
|
211
|
+
if (this.runningEnvironment === 'playwright' && this.dataType === 'log') this.isFileStorage = false;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* @param {*} testId
|
|
216
|
+
* @returns aray of data (any type)
|
|
217
|
+
*/
|
|
218
|
+
#getDataFromGlobalVar(testId) {
|
|
219
|
+
try {
|
|
220
|
+
if (global?.testomatioDataStore[this.dataType]) {
|
|
221
|
+
const testData = global.testomatioDataStore[this.dataType][testId];
|
|
222
|
+
if (testData) debug(`"${this.dataType}" data for test id ${testId}:`, testData.join(', '));
|
|
223
|
+
return testData || [];
|
|
224
|
+
}
|
|
225
|
+
// debug(`No ${this.dataType} data for test id ${testId} in <global> storage`);
|
|
226
|
+
return [];
|
|
227
|
+
} catch (e) {
|
|
228
|
+
// there could be no data, ignore
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
*
|
|
234
|
+
* @param {*} testId
|
|
235
|
+
* @returns array of data (any type)
|
|
236
|
+
*/
|
|
237
|
+
#getDataFromFile(testId) {
|
|
238
|
+
try {
|
|
239
|
+
const filepath = join(this.dataDirPath, `${this.dataType}_${testId}`);
|
|
240
|
+
if (fs.existsSync(filepath)) {
|
|
241
|
+
const testDataAsText = fs.readFileSync(filepath, 'utf-8');
|
|
242
|
+
if (testDataAsText) debug(`"${this.dataType}" data for test id ${testId}:`, testDataAsText);
|
|
243
|
+
const testDataArr = testDataAsText?.split(os.EOL) || [];
|
|
244
|
+
return testDataArr;
|
|
245
|
+
}
|
|
246
|
+
// debug(`No ${this.dataType} data for test id ${testId} in <file> storage`);
|
|
247
|
+
return [];
|
|
248
|
+
} catch (e) {
|
|
249
|
+
// there could be no data, ignore
|
|
250
|
+
}
|
|
251
|
+
return [];
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Puts data to global variable. Unlike the file storage, stores data in array (file storage just append as string).
|
|
256
|
+
* @param {*} data
|
|
257
|
+
* @param {*} testId
|
|
258
|
+
*/
|
|
259
|
+
#putDataToGlobalVar(data, testId) {
|
|
260
|
+
debug(`Saving data to global variable for test ${testId}:`, data);
|
|
261
|
+
if (!global.testomatioDataStore) global.testomatioDataStore = {};
|
|
262
|
+
if (!global.testomatioDataStore?.[this.dataType]) global.testomatioDataStore[this.dataType] = {};
|
|
263
|
+
|
|
264
|
+
if (!global.testomatioDataStore?.[this.dataType][testId]) global.testomatioDataStore[this.dataType][testId] = [];
|
|
265
|
+
global.testomatioDataStore[this.dataType][testId].push(data);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
#putDataToFile(data, testId) {
|
|
269
|
+
if (typeof data !== 'string') data = JSON.stringify(data);
|
|
270
|
+
const filename = `${this.dataType}_${testId}`;
|
|
271
|
+
const filepath = join(this.dataDirPath, filename);
|
|
272
|
+
if (!fs.existsSync(this.dataDirPath)) fileSystem.createDir(this.dataDirPath);
|
|
273
|
+
debug('Saving data to file for test', testId, 'to', filepath, ':\n', data, '\n');
|
|
274
|
+
|
|
275
|
+
// append new line if file already exists (in this case its definitely includes some data)
|
|
276
|
+
if (fs.existsSync(filepath)) {
|
|
277
|
+
fs.appendFileSync(filepath, os.EOL + data, 'utf-8');
|
|
278
|
+
} else {
|
|
279
|
+
fs.writeFileSync(filepath, data, 'utf-8');
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
module.exports = DataStorage;
|
|
285
|
+
|
|
286
|
+
// TODO: consider using fs promises instead of writeSync/appendFileSync to
|
|
287
|
+
// prevent blocking and improve performance (probably queue usage will be required)
|
|
288
|
+
|
|
289
|
+
// TODO: rewrite client - everything regarding storing artifacts
|
|
290
|
+
// TODO: try to define adapter inside client
|
|
291
|
+
// TODO: ability to intercept multiple loggers (upd: no need)
|
|
292
|
+
|
|
293
|
+
/* does not work for Playwright
|
|
294
|
+
try {
|
|
295
|
+
const { test } = require('@playwright/test');
|
|
296
|
+
test.beforeEach(async ({}, testInfo) => {
|
|
297
|
+
global.testTitle = testInfo.title;
|
|
298
|
+
global.testomatioRunningEnvironment = 'playwright';
|
|
299
|
+
});
|
|
300
|
+
} catch (e) {
|
|
301
|
+
// ignore
|
|
302
|
+
}
|
|
303
|
+
// Playwright storage works fine only when running tests from a single file;
|
|
304
|
+
// otherwise it is not possible to define test id/title, it is overwritten
|
|
305
|
+
TODO: the only way implementation for Pw for now is to pass testInfo inside test
|
|
306
|
+
// (and process this testInfo inside "put" function)
|
|
307
|
+
*/
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
const debug = require('debug')('@testomatio/reporter:key-value-storage');
|
|
2
|
+
const DataStorage = require('./data-storage');
|
|
3
|
+
|
|
4
|
+
class KeyValueStorage {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.dataStorage = new DataStorage('keyvalue');
|
|
7
|
+
|
|
8
|
+
// singleton
|
|
9
|
+
if (!KeyValueStorage.instance) {
|
|
10
|
+
KeyValueStorage.instance = this;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Stores key-value pair and passes it to reporter
|
|
16
|
+
* @param {{key: string}} keyValue - key-value pair(s) as object
|
|
17
|
+
* @param {*} context testId or test title
|
|
18
|
+
*/
|
|
19
|
+
put(keyValue, context = null) {
|
|
20
|
+
if (!keyValue) return;
|
|
21
|
+
this.dataStorage.putData(keyValue, context);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
#isKeyValueObject(smth) {
|
|
25
|
+
return smth && typeof smth === 'object' && !Array.isArray(smth) && smth !== null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Returns key-values pairs for the test as object
|
|
30
|
+
* @param {*} context testId or test context from test runner
|
|
31
|
+
* @returns {Object} key-values pairs as object, e.g. {priority: 'high', browser: 'chrome'}
|
|
32
|
+
*/
|
|
33
|
+
get(context) {
|
|
34
|
+
if (!context) return null;
|
|
35
|
+
|
|
36
|
+
const keyValuesList = this.dataStorage.getData(context);
|
|
37
|
+
if (!keyValuesList || !keyValuesList?.length) return null;
|
|
38
|
+
|
|
39
|
+
const keyValues = {};
|
|
40
|
+
for (const keyValue of keyValuesList) {
|
|
41
|
+
if (this.#isKeyValueObject(keyValue)) {
|
|
42
|
+
Object.assign(keyValues, keyValue);
|
|
43
|
+
} else if (typeof keyValue === 'string') {
|
|
44
|
+
try {
|
|
45
|
+
Object.assign(keyValues, JSON.parse(keyValue));
|
|
46
|
+
} catch (e) {
|
|
47
|
+
debug(`Error parsing key-values for test ${context}`, keyValue);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return Object.keys(keyValues).length ? keyValues : null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
KeyValueStorage.instance = null;
|
|
57
|
+
|
|
58
|
+
module.exports = new KeyValueStorage();
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
const chalk = require('chalk');
|
|
2
|
+
const debug = require('debug')('@testomatio/reporter:logger');
|
|
3
|
+
const DataStorage = require('./data-storage');
|
|
4
|
+
|
|
5
|
+
const LOG_METHODS = ['assert', 'debug', 'error', 'info', 'log', 'trace', 'warn'];
|
|
6
|
+
const LEVELS = {
|
|
7
|
+
ALL: { severity: 1, color: '' },
|
|
8
|
+
VERBOSE: { severity: 3, color: 'grey' },
|
|
9
|
+
TRACE: { severity: 5, color: 'grey' },
|
|
10
|
+
DEBUG: { severity: 7, color: 'cyan' },
|
|
11
|
+
INFO: { severity: 9, color: 'black' },
|
|
12
|
+
LOG: { severity: 11, color: 'black' },
|
|
13
|
+
WARN: { severity: 13, color: 'yellow' },
|
|
14
|
+
ERROR: { severity: 15, color: 'red' },
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
// ! DON'T use console.log, console.warn, etc in this file, because it will lead to infinite loop
|
|
18
|
+
// use debug() instead
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Logger allows to intercept logs from any logger (console.log, tracer, pino, etc)
|
|
22
|
+
* and save in the testomatio reporter.
|
|
23
|
+
* Supports different syntaxes to satisfy any user preferences.
|
|
24
|
+
*/
|
|
25
|
+
class Logger {
|
|
26
|
+
// set default logger to be used in log, warn, error, etc methods
|
|
27
|
+
#originalUserLogger = { ...console };
|
|
28
|
+
|
|
29
|
+
#dataStorage = new DataStorage('log');
|
|
30
|
+
|
|
31
|
+
logLevel = process?.env?.LOG_LEVEL?.toUpperCase() || 'ALL';
|
|
32
|
+
|
|
33
|
+
constructor() {
|
|
34
|
+
// intercept console by default
|
|
35
|
+
this.intercept(console);
|
|
36
|
+
|
|
37
|
+
// singleton
|
|
38
|
+
if (!Logger.instance) {
|
|
39
|
+
Logger.instance = this;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Allows you to define a step inside a test. Step name is attached to the report and
|
|
45
|
+
* helps to understand the test flow.
|
|
46
|
+
* @param {*} strings
|
|
47
|
+
* @param {...any} values
|
|
48
|
+
*/
|
|
49
|
+
step(strings, ...values) {
|
|
50
|
+
let logs = '';
|
|
51
|
+
for (let i = 0; i < strings.length; i++) {
|
|
52
|
+
logs += strings[i];
|
|
53
|
+
if (i < values.length) {
|
|
54
|
+
logs += values[i];
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
logs = chalk.blue(`> ${logs}`);
|
|
58
|
+
this.#dataStorage.putData(logs);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
*
|
|
63
|
+
* @param {*} context testId or test context from test runner
|
|
64
|
+
* @returns
|
|
65
|
+
*/
|
|
66
|
+
getLogs(context) {
|
|
67
|
+
const logs = this.#dataStorage.getData(context);
|
|
68
|
+
return logs || '';
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
#stringifyLogs(...args) {
|
|
72
|
+
const logs = [];
|
|
73
|
+
// stringify everything except strings
|
|
74
|
+
for (const arg of args) {
|
|
75
|
+
// ignore empty strings
|
|
76
|
+
if (arg === '') continue;
|
|
77
|
+
if (typeof arg === 'string') {
|
|
78
|
+
logs.push(arg);
|
|
79
|
+
} else if (Array.isArray(arg)) {
|
|
80
|
+
logs.push(arg.join(' '));
|
|
81
|
+
} else {
|
|
82
|
+
try {
|
|
83
|
+
// eslint-disable-next-line no-unused-expressions
|
|
84
|
+
this.prettyObjects ? logs.push(JSON.stringify(arg, null, 2)) : logs.push(JSON.stringify(arg));
|
|
85
|
+
} catch (e) {
|
|
86
|
+
debug('Error while stringify object', e);
|
|
87
|
+
logs.push(arg);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return logs.join(' ');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Tagget template literal. Allows to use different syntaxes:
|
|
96
|
+
* 1. Tagget template: log`text ${someVar}`
|
|
97
|
+
* 2. Standard: log(`text ${someVar}`)
|
|
98
|
+
* 3. Standard with multiple arguments: log('text', someVar)
|
|
99
|
+
*/
|
|
100
|
+
templateLiteralLog(strings, ...args) {
|
|
101
|
+
if (Array.isArray(strings)) strings = strings.filter(item => item !== '').map(item => item.trim());
|
|
102
|
+
if (Array.isArray(args)) args = args.filter(item => item !== '');
|
|
103
|
+
|
|
104
|
+
let logs;
|
|
105
|
+
// this block means tagged template is used (syntax like $`text ${someVar}`)
|
|
106
|
+
if (Array.isArray(strings) && strings.length === args.length + 1) {
|
|
107
|
+
logs = strings.reduce(
|
|
108
|
+
(result, current, index) =>
|
|
109
|
+
result +
|
|
110
|
+
current +
|
|
111
|
+
// strings are splitted by args when use tagged template, thus we add arg after each string
|
|
112
|
+
// it looks like: `string1 arg1 string2 arg2 string3`
|
|
113
|
+
(args[index] !== undefined // eslint-disable-line no-nested-ternary
|
|
114
|
+
? typeof args[index] === 'string'
|
|
115
|
+
? args[index] // add arg as it is
|
|
116
|
+
: this.#stringifyLogs(args[index]) // stringify arg
|
|
117
|
+
: ''),
|
|
118
|
+
// initial accumulator value
|
|
119
|
+
'',
|
|
120
|
+
);
|
|
121
|
+
} else {
|
|
122
|
+
// this block means arguments syntax is used (syntax like $('text', someVar))
|
|
123
|
+
// in this case strings represents just a first argument
|
|
124
|
+
logs = this.#stringifyLogs(strings, ...args);
|
|
125
|
+
}
|
|
126
|
+
this.#originalUserLogger.log(logs);
|
|
127
|
+
this.#dataStorage.putData(logs);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* This function is a wrapper for each logging methods (log, warn, error etc) (not to repeat the same code)
|
|
132
|
+
* @param {*} argsArray
|
|
133
|
+
* @param {*} level
|
|
134
|
+
* @returns
|
|
135
|
+
*/
|
|
136
|
+
#logWrapper(argsArray, level) {
|
|
137
|
+
if (!argsArray.length) return;
|
|
138
|
+
|
|
139
|
+
const severity = LEVELS[level].severity;
|
|
140
|
+
if (severity < LEVELS[this.logLevel]?.severity) return;
|
|
141
|
+
|
|
142
|
+
const logs = this.#stringifyLogs(...argsArray);
|
|
143
|
+
const colorizedLogs = chalk[LEVELS[level].color](logs);
|
|
144
|
+
this.#dataStorage.putData(colorizedLogs);
|
|
145
|
+
try {
|
|
146
|
+
// level.toLowerCase() represents method name (log, warn, error, etc)
|
|
147
|
+
this.#originalUserLogger[level.toLowerCase()](colorizedLogs);
|
|
148
|
+
} catch (e) {
|
|
149
|
+
// method could be unexisting, ignore error
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
assert(...args) {
|
|
154
|
+
this.#logWrapper(args, 'ERROR');
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
debug(...args) {
|
|
158
|
+
this.#logWrapper(args, 'DEBUG');
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
error(...args) {
|
|
162
|
+
this.#logWrapper(args, 'ERROR');
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
info(...args) {
|
|
166
|
+
this.#logWrapper(args, 'INFO');
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
log(...args) {
|
|
170
|
+
this.#logWrapper(args, 'LOG');
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
trace(...args) {
|
|
174
|
+
this.#logWrapper(args, 'TRACE');
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
warn(...args) {
|
|
178
|
+
this.#logWrapper(args, 'WARN');
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Intercepts user logger messages.
|
|
183
|
+
* When call this method, Logger start to control the user logger
|
|
184
|
+
* @param {*} userLogger
|
|
185
|
+
*/
|
|
186
|
+
intercept(userLogger) {
|
|
187
|
+
/* prevent multiple console interceptions (cause of infinite loop)
|
|
188
|
+
actual only for "console", because its used as default output and is intercepted by default */
|
|
189
|
+
const isUserLoggerConsole = userLogger.toString?.().toLowerCase() === '[object console]';
|
|
190
|
+
if (isUserLoggerConsole && process.env.TESTOMATIO_LOGGER_CONSOLE_INTERCEPTED) {
|
|
191
|
+
debug(`Try to intercept console, but it is already intercepted`);
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
process.env.TESTOMATIO_LOGGER_CONSOLE_INTERCEPTED = 'true';
|
|
196
|
+
debug(isUserLoggerConsole ? 'console intercepted' : 'User logger intercepted');
|
|
197
|
+
|
|
198
|
+
// override user logger (any, e.g. console) methods to intercept log messages
|
|
199
|
+
for (const method of LOG_METHODS) {
|
|
200
|
+
/*
|
|
201
|
+
its better to create method even if it does not exist in user logger;
|
|
202
|
+
on method invocation, we will store the data anyway and catch block will prevent potential errors
|
|
203
|
+
while trying to output the message to terminal
|
|
204
|
+
*/
|
|
205
|
+
// if (!this._loggerToIntercept[method]) continue;
|
|
206
|
+
userLogger[method] = (...args) => this[method](...args);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/* Playwright
|
|
210
|
+
Playwright intercepts console messages by default. Thus when we intercept any logger and provide output by console,
|
|
211
|
+
messages are intercepted by Playwright.
|
|
212
|
+
*/
|
|
213
|
+
|
|
214
|
+
/*
|
|
215
|
+
Initial idea was to intercept any logger (tracer, pino, etc),
|
|
216
|
+
intercept message and provide output by the same logger.
|
|
217
|
+
But reality brings some problems: the same messages are intercepted multiple times
|
|
218
|
+
(because of multiple loggers are created at the same terminal process).
|
|
219
|
+
Also its difficult to understand (actually did not find the way to do it) if logger was already intercepted or not.
|
|
220
|
+
Thus, decided to intercept only console by default and provide output by default console.
|
|
221
|
+
It means, if user uses his own logger, its messages will be intercepted,
|
|
222
|
+
but the output will be provided by console.
|
|
223
|
+
TODO: try to implement the providing output to terminal by user logger
|
|
224
|
+
*/
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Allows to configure logger. Make sure you do it before the logger usage in your code.
|
|
229
|
+
*
|
|
230
|
+
* @param {Object} [config={}] - The configuration object.
|
|
231
|
+
* @param {string} [config.logLevel] - The desired log level. Valid values are 'DEBUG', 'INFO', 'WARN', and 'ERROR'.
|
|
232
|
+
* @param {boolean} [config.prettyObjects] - Specifies whether to enable pretty printing of objects.
|
|
233
|
+
* @returns {void}
|
|
234
|
+
*/
|
|
235
|
+
configure(config = {}) {
|
|
236
|
+
if (!config) return;
|
|
237
|
+
if (config.prettyObjects === false || config.prettyObjects === true) this.prettyObjects = config.prettyObjects;
|
|
238
|
+
if (config.logLevel) this.logLevel = config.logLevel.toUpperCase();
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
Logger.instance = null;
|
|
243
|
+
|
|
244
|
+
const logger = new Logger();
|
|
245
|
+
|
|
246
|
+
module.exports = logger;
|
|
247
|
+
|
|
248
|
+
// TODO: parse passed arguments as {level: 'str', message: 'str'} because some loggers use such syntax;
|
|
249
|
+
// upd: did not face such loggers, but still could be useful
|
|
250
|
+
|
|
251
|
+
/* Cypress
|
|
252
|
+
There is no listener like "after:test" in cypress, only "after:spec" is available.
|
|
253
|
+
Thus, cannot separate logs even when I gather them (because I don't know when the test is done, just know about suite).
|
|
254
|
+
Also there is no easy way to access the message from cy.log() function.
|
|
255
|
+
(Using testomatio logger – logger.log() is not convenient because Cypress chains its commands,
|
|
256
|
+
thus such command will interrupt the chain.)
|
|
257
|
+
|
|
258
|
+
(Could not implement intercepting of cy.log('message'));
|
|
259
|
+
I found the only ability to get any logs using .task('log', 'message') (this is custom, not default cypress command)
|
|
260
|
+
and then intercept it with:
|
|
261
|
+
on('task', {
|
|
262
|
+
log (message) {
|
|
263
|
+
console.log(message)
|
|
264
|
+
return null
|
|
265
|
+
}
|
|
266
|
+
})
|
|
267
|
+
|
|
268
|
+
but:
|
|
269
|
+
1) it does not solve problem with getting current running testId;
|
|
270
|
+
2) leads to warning "Warning: Multiple attempts to register the following task(s):".)
|
|
271
|
+
|
|
272
|
+
My way to get test id:
|
|
273
|
+
add cypress command to save test title to file)):
|
|
274
|
+
Cypress.Commands.add('writeTestTitleToFile', () => {
|
|
275
|
+
const testTitle = cy.state('runnable').title;
|
|
276
|
+
cy.writeFile('testomatio_test_title', testTitle);
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
Finally, in the test it will look like:
|
|
280
|
+
cy
|
|
281
|
+
.writeTestTitleToFile() // <<<
|
|
282
|
+
.task('log', 'This is a log message from the test') // <<<
|
|
283
|
+
|
|
284
|
+
.get('element)
|
|
285
|
+
.type('text')
|
|
286
|
+
.click()
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
Parallelization in Cypress is only available if using Cypress Dashboard??
|
|
290
|
+
*/
|