forked from SAP-archive/karma-ui5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser.js
More file actions
270 lines (233 loc) · 7.09 KB
/
Copy pathbrowser.js
File metadata and controls
270 lines (233 loc) · 7.09 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
const istanbulLibCoverage = require("istanbul-lib-coverage");
require("./discovery.js");
(function(window) {
const karma = window.__karma__;
function reportSetupFailure(description, error) {
karma.info({total: 1});
karma.result({
description: description,
suite: [],
success: false,
log: error ? [error] : [],
time: 0
});
karma.complete({});
}
function setFullSize(element) {
element.style.height = "100%";
element.style.padding = "0";
element.style.margin = "0";
}
// Set karma execution frame to full size, so that the iframe can use width/height 100%
setFullSize(document.documentElement);
setFullSize(document.body);
karma.start = function() {
const config = karma.config && karma.config.ui5 || {};
const prependBase = function(path) {
return /^\/base\//.test(path) ? path : "/base/" + path;
};
const windowUtil = function(url, onload) {
const context = {
close: function() {
if (config.useIframe) {
document.body.removeChild(context._frame);
} else {
context._window.close();
}
}
};
if (config.useIframe) {
context._frame = document.createElement("iframe");
context._frame.onload = function() {
onload.call(null, context);
};
context._frame.style.height = "100%";
context._frame.style.width = "100%";
context._frame.style.border = "0";
context._frame.src = url;
document.body.appendChild(context._frame);
context.contentWindow = context._frame.contentWindow;
} else {
context._window = window.open(url);
context.contentWindow = context._window;
context._window.addEventListener("load", function() {
onload.call(null, context);
});
}
karma.setupContext(context.contentWindow);
};
if (!config.testpage) {
const aTestsuites = getTestsuites(karma.files);
if (aTestsuites.length === 0) {
karma.log("error", [
"Could not find a testsuite or no testpage defined.\n" +
"Please set a testpage in the config or via CLI.\n" +
"For more details: https://github.com/SAP/karma-ui5#testpage"]
);
// reportSetupFailure(); // TODO
return;
}
if (aTestsuites.length !== 1) {
const testsuitePaths = aTestsuites.map(function(testsuite) {
return testsuite.replace(/^\/base\//, "");
});
karma.log("error", [
"No testpage is configured but multiple testsuites have been found:\n\n" +
testsuitePaths.join("\n") +
"\n\n" +
"Please explicitly configure a \"testpage\" in your karma config or via CLI:\n" +
"https://github.com/SAP/karma-ui5#testpage"]);
// reportSetupFailure(); // TODO
return;
}
config.testpage = aTestsuites[0];
}
config.testpage = prependBase(config.testpage);
window.findTests(config.testpage).then(function(testpages) {
if (!testpages || testpages.length === 0) {
reportSetupFailure("Could not resolve any testpages!");
return;
}
runTests(testpages.map(function(testpage) {
return testpage["fullpage"];
}));
}, function(err) {
reportSetupFailure("Error resolving testsuite: " + err.fullpage, err.error);
});
function getTestsuites(files) {
return Object.keys(files).filter(function(path) {
return /\.qunit\.html$/.test(path) && /\/testsuite\./.test(path);
});
}
function addUrlParameters(testpageUrl) {
if (!config.urlParameters) {
return testpageUrl;
}
const url = new URL(testpageUrl, document.location.href);
config.urlParameters.forEach(function(urlParameter) {
url.searchParams.append(urlParameter.key, urlParameter.value);
});
// Sort params for consistency between browsers (probably caused by polyfill)
url.searchParams.sort();
return url.toString();
}
function runTests(testpages) {
let totalNumberOfTest = 0;
let coverageMap;
function mergeCoverage(coverage) {
if (!coverage) {
return;
}
if (!coverageMap) {
coverageMap = istanbulLibCoverage.createCoverageMap();
}
coverageMap.merge(coverage);
}
function runTestPage(i) {
const qunitHtmlFile = addUrlParameters(testpages[i]);
windowUtil(qunitHtmlFile, function(testWindow) {
let timer = null;
let testResult = {};
let QUnit;
let accessError = false;
try {
QUnit = testWindow.contentWindow.QUnit;
} catch (err) {
if (err.name === "TypeError" && err.message === "Permission denied") {
accessError = true;
} else {
throw err;
}
}
function testFinished() {
// Merge test page coverage into global coverage object
if (!accessError) {
mergeCoverage(testWindow.contentWindow.__coverage__);
}
// Run next test or trigger completion
if (i < testpages.length - 1) {
testWindow.close();
testWindow = null;
runTestPage(i + 1);
} else {
karma.complete({
coverage: coverageMap ? coverageMap.toJSON() : undefined
});
}
}
if (!QUnit) {
const log = [];
if (accessError || !testWindow.contentWindow.document.querySelector("script")) {
// Access Error (IE) or page doesn't have any script => probably 404
log.push("Error while loading testpage");
} else {
// QUnit couldn't be loaded
log.push("Missing QUnit framework");
}
// Report a test failure
totalNumberOfTest += 1;
karma.info({total: totalNumberOfTest});
karma.result( {
description: qunitHtmlFile,
suite: [],
success: false,
log: log,
time: 0
});
testFinished();
return;
}
if (QUnit.begin) {
QUnit.begin(function(args) {
totalNumberOfTest += args.totalTests;
karma.info({total: totalNumberOfTest});
});
}
QUnit.done(testFinished);
QUnit.testStart(function(test) {
timer = new Date().getTime();
testResult = {success: true, errors: []};
});
QUnit.log(function(details) {
if (!details.result) {
let msg = "";
if (details.message) {
msg += details.message + "\n";
}
if (typeof details.expected !== "undefined") {
msg += "Expected: " + QUnit.dump.parse(details.expected) + "\n" +
"Actual: " + QUnit.dump.parse(details.actual) + "\n";
}
if (details.source) {
msg += details.source + "\n";
}
testResult.success = false;
testResult.errors.push(msg);
}
if (config.logAssertions) {
const result = {
description: details.message,
suite: details.module && [qunitHtmlFile, details.module, details.name, details.message] || [],
success: details.result,
log: testResult.errors || [],
time: new Date().getTime() - timer
};
karma.result(result);
}
});
QUnit.testDone(function(test) {
const result = {
description: test.name,
suite: test.module && [qunitHtmlFile, test.module] || [],
success: testResult.success,
log: testResult.errors || [],
time: new Date().getTime() - timer
};
karma.result(result);
});
});
}
runTestPage(0);
}
};
})(typeof window !== "undefined" ? window : global);