-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
205 lines (180 loc) · 6.31 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
const requireg = require('requireg');
const { Timer } = require('@kronoslive/codeceptjs-utils');
const Ajv = require('ajv');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const codeceptjsPath = path.resolve(global.codecept_dir, './node_modules/codeceptjs');
const codeceptjs = requireg(codeceptjsPath);
const config = codeceptjs.config.get();
const { skipLongData } = require('@kronoslive/codeceptjs-utils');
const { ajvSortCheck } = require('@kronoslive/codeceptjs-utils');
const { walkSync } = require('@kronoslive/codeceptjs-utils');
const allErrors = (config.mocha
&& config.mocha.reporterOptions['codeceptjs/lib/reporter/cli']
&& config.mocha.reporterOptions['codeceptjs/lib/reporter/cli'].options)
? config.mocha.reporterOptions['codeceptjs/lib/reporter/cli'].options.verbose : false;
const ajv = new Ajv({ verbose: true, allErrors, v5: true });
require('ajv-merge-patch')(ajv);
let mochawesome;
/**
* Хэлпер с различными утилитами (например методы для работы с ajv).
*/
class Utils extends Helper {
/**
*
* @param {object} config
*/
constructor(config) {
super(config);
this._validateConfig(config);
}
/**
*
* @param {object} config
* @private
*/
_validateConfig(config) {
this.options = {
waitForTimeout: 3000, // ms
waitForInterval: 500, // ms
};
this.schemasLoad = false;
Object.assign(this.options, config);
}
/**
*
* @returns {boolean}
* @private
*/
_beforeSuite() {
mochawesome = this.helpers.Mochawesome;
if (!this.schemasLoad) this._loadJSONSchemas();
return true;
}
_finishTest() {
const htmlPath = `${path.join(
global.codecept_dir,
`${config.mocha.reporterOptions.mochawesome.options.reportDir}`,
)}/${config.mocha.reporterOptions.mochawesome.options.reportFilename}.html`;
if (fs.existsSync(htmlPath)) {
const data = fs.readFileSync(htmlPath, 'utf-8');
const newValue = data.replace(
'<head>',
`<head><script type="text/javascript">
setTimeout(() => {if (top.location!= self.location) {
window.parent.document.getElementById("tab-content-of-results") ?
window.parent.document.getElementById("tab-content-of-results").getElementsByTagName("iframe")[0].style.height = '20000px' :
window.parent.document.getElementById("tab-content-of-test_results").getElementsByTagName("iframe")[0].style.height = '20000px';
}}
, 3000); </script>`,
);
fs.writeFileSync(htmlPath, newValue, 'utf-8');
}
return true;
}
_failed() {
}
/**
* Validate that latest ws response message has specified JSON schema.
* @param {string} schema - path to JSON schema file. is relative to schemas folder
* @param {array} params
* @param {*} data - specify data that should be validated
*
* ```
* I.seeResponseHasValidJsonSchema('authPassed.json', {something: "value"});
* ```
*/
seeDataHasValidJsonSchema(schema, params = [], data) {
// eslint-disable-next-line global-require, import/no-dynamic-require
let schemaJson = require(path.join(global.codecept_dir, `./schemas/${schema}`));
if (typeof schemaJson === 'function') {
schemaJson = schemaJson.apply(this, params);
}
const valid = ajv.validate(schemaJson, data);
if (!valid) {
mochawesome.addMochawesomeContext({
title: `Json schema for ${schema}`,
value: JSON.stringify(schemaJson, null, 2),
});
const report = ajv.errors;
report.forEach((err, index) => {
report[index].data = skipLongData(report[index].data);
delete report[index].parentSchema;
delete report[index].schema;
if (err.keyword === '$merge') {
report[index] = {
dataPath: err.dataPath,
keyword: err.keyword,
schemaPath: err.schemaPath,
message: 'should pass $merge keyword validation. Skip Data Show',
};
}
});
mochawesome.addMochawesomeContext({
title: 'Json schema errors',
value: report,
});
}
assert.ok(valid, 'Find JSON schema errors. See additional context for more details');
}
/**
* Load custom keywords and all json schemas from schemas folder
* @private
*/
_loadJSONSchemas() {
if (fs.existsSync(path.join(global.codecept_dir, './schemas/'))) {
let files = walkSync(path.join(global.codecept_dir, './schemas/'));
Object.keys(ajvSortCheck).forEach((keyword) => {
ajv.addKeyword(keyword, ajvSortCheck[keyword]);
});
files = files.filter((file) => file.indexOf('.json') + 5 === file.length);
files.forEach((file) => {
// eslint-disable-next-line global-require, import/no-dynamic-require
ajv.addSchema(require(path.join(global.codecept_dir, `./schemas/${file}`)), file);
});
}
this.schemasLoad = true;
}
/**
* Метод ожидает выполнение условия в установленный таймаут. Проверяем выполнение условие через интервал.
* @param {function} condition
* @param {number} timeout
* @param {string} timeoutMsg
* @param {number} interval
* @returns {*}
*/
waitUntil(condition, timeout, timeoutMsg = 'timeout', interval) {
if (typeof timeout !== 'number') {
timeout = this.options.waitForTimeout;
}
if (typeof interval !== 'number') {
interval = this.options.waitForInterval;
}
let fn;
if (typeof condition === 'function') {
fn = condition.bind(this);
} else {
fn = () => Promise.resolve(condition);
}
const timer = new Timer(interval, timeout, fn, true, true);
return timer.catch((e) => {
if (e.message === 'timeout' && typeof timeoutMsg === 'string') {
throw new Error(timeoutMsg);
}
if (e.type === 'NoSuchElement') {
throw new Error(e.message);
}
throw new Error(`Promise was rejected with the following reason: ${e.message}`);
});
}
wait(milliseconds = 1000) {
const promise = new Promise((resolve) => {
setTimeout(() => {
resolve(true);
}, milliseconds);
});
return promise;
}
}
module.exports = Utils;