@testomatio/reporter 1.3.5-beta → 1.4.1-beta-1
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 +4 -4
- package/lib/adapter/codecept.js +11 -9
- package/lib/adapter/cucumber/current.js +4 -4
- package/lib/adapter/cucumber/legacy.js +3 -3
- package/lib/adapter/cypress-plugin/index.js +1 -1
- package/lib/adapter/jasmine.js +1 -1
- package/lib/adapter/jest.js +12 -17
- package/lib/adapter/mocha.js +22 -50
- package/lib/adapter/playwright.js +34 -22
- 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 +93 -27
- package/lib/constants.js +4 -6
- package/lib/fileUploader.js +106 -38
- package/lib/junit-adapter/java.js +27 -9
- package/lib/pipe/csv.js +4 -1
- package/lib/pipe/github.js +5 -16
- package/lib/pipe/gitlab.js +5 -16
- package/lib/pipe/testomatio.js +136 -34
- package/lib/reporter-functions.js +43 -0
- package/lib/reporter.js +11 -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/{logger.js → storages/logger.js} +103 -114
- package/lib/utils/pipe_utils.js +135 -0
- package/lib/{util.js → utils/utils.js} +129 -12
- package/lib/xmlReader.js +132 -44
- package/package.json +9 -7
- package/lib/_ArtifactStorageOld.js +0 -142
- package/lib/artifactStorage.js +0 -25
- package/lib/dataStorage.js +0 -244
|
@@ -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();
|