forked from open-xml-templating/docxtemplater
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webdriver.js
388 lines (368 loc) · 9.15 KB
/
webdriver.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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/* eslint-disable no-process-env */
/* eslint-disable no-console */
const {
BROWSER = "CHROME",
browserName,
version,
platform,
TRAVIS_JOB_NUMBER,
TRAVIS_BUILD_NUMBER,
SAUCE_USERNAME,
SAUCE_ACCESS_KEY,
} = process.env;
const chalk = require("chalk");
function exit(message) {
console.log(message);
/* eslint-disable-next-line no-process-exit */
process.exit(1);
}
let fullBrowserName = null;
const url = require("url");
const finalhandler = require("finalhandler");
const webdriverio = require("webdriverio");
const { expect } = require("chai");
const request = require("request");
const serveStatic = require("serve-static");
const port = 9000;
const http = require("http");
async function sleep(ms) {
return new Promise(function (resolve) {
setTimeout(() => resolve(), ms);
});
}
// These options are the modern, W3C ones
const sauceLabsW3COptions = {
browserName,
browserVersion: version,
platformName: platform,
"sauce:options": {
name: "docxtemplater mocha",
build: TRAVIS_BUILD_NUMBER,
tags: ["docxtemplater"],
tunnelIdentifier: TRAVIS_JOB_NUMBER,
public: true,
},
};
// These options are the legacy, JWP ones
const saucelabsJWPOptions = {
browserName,
version,
platform,
tags: ["docxtemplater"],
name: "docxtemplater mocha",
"tunnel-identifier": TRAVIS_JOB_NUMBER,
tunnelIdentifier: TRAVIS_JOB_NUMBER,
build: TRAVIS_BUILD_NUMBER,
captureHtml: true,
public: true,
};
// USE JWP instead of W3C for chrome < 75 only
const useJWP = browserName === "chrome" && +version < 75;
const browserCapability = {
CHROME: {
browserName: "chrome",
"goog:chromeOptions": {
args: [
"--headless",
// Use --disable-gpu to avoid an error from a missing Mesa
// library, as per
// https://chromium.googlesource.com/chromium/src/+/lkgr/headless/README.md
"--disable-gpu",
],
},
},
FIREFOX: {
browserName: "firefox",
"moz:firefoxOptions": {
args: ["-headless"],
},
},
SAUCELABS: useJWP ? saucelabsJWPOptions : sauceLabsW3COptions,
};
const desiredCapabilities = browserCapability[BROWSER];
fullBrowserName = BROWSER + " (local)";
if (!desiredCapabilities) {
exit("Unknown browser :" + BROWSER);
}
const second = 1000;
const minute = 60 * second;
const commonOptions = {
automationProtocol: "webdriver",
logLevel: "warn",
connectionRetryTimeout: 5 * minute,
};
let options;
if (BROWSER === "SAUCELABS") {
fullBrowserName = `${browserName} ${version} ${platform} (SAUCELABS)`;
options = {
...commonOptions,
tunnelIdentifier: TRAVIS_JOB_NUMBER,
"tunnel-identifier": TRAVIS_JOB_NUMBER,
build: TRAVIS_BUILD_NUMBER,
user: SAUCE_USERNAME,
key: SAUCE_ACCESS_KEY,
};
} else {
options = {
...commonOptions,
path: "/wd/hub/",
};
}
options.capabilities = desiredCapabilities;
console.log("Running test on " + fullBrowserName);
const serve = serveStatic(__dirname);
const server = http.createServer(function onRequest(req, res) {
serve(req, res, finalhandler(req, res));
});
function updateSaucelabsStatus(client, result, done) {
request(
{
headers: { "Content-Type": "text/json" },
url: `http://${SAUCE_USERNAME}:${SAUCE_ACCESS_KEY}@saucelabs.com/rest/v1/${SAUCE_USERNAME}/jobs/${client.sessionId}`,
method: "PUT",
body: JSON.stringify({
passed: result,
public: true,
}),
},
function (err) {
if (err) {
done(err);
return false;
}
done();
}
);
}
let logPostRequest = false;
function logE(arg) {
if (logPostRequest) {
console.log(arg);
console.log("Stacktrace is : ");
console.log(new Error());
}
}
const timeoutConnection = 180;
const failuresRegex = /.*failures: ([0-9]+).*/;
const passesRegex = /.*passes: ([0-9]+).*/;
const startTime = +new Date();
server.listen(port, async function () {
let client;
try {
client = await webdriverio.remote(options);
} catch (e) {
exit(e);
}
async function mockConsole() {
await client.execute(() => {
if (window.myLogs) {
return;
}
window.myLogs = [];
console.log = function () {
const myLog = [];
for (let i = 0, len = arguments.length; i < len; i++) {
myLog.push(arguments[i]);
}
window.myLogs.push(myLog);
};
console.error = function () {
const myLog = [];
for (let i = 0, len = arguments.length; i < len; i++) {
myLog.push(arguments[i]);
}
window.myLogs.push(myLog);
};
console.warn = function () {
const myLog = [];
for (let i = 0, len = arguments.length; i < len; i++) {
myLog.push(arguments[i]);
}
window.myLogs.push(myLog);
};
});
}
async function getConsole() {
return await client.execute(() => {
if (!window.myLogs) {
return "[]";
}
return JSON.stringify(window.myLogs);
});
}
await mockConsole();
let logIndex = 0;
const int2 = setInterval(async function () {
if (logPostRequest) {
clearInterval(int2);
return;
}
await mockConsole();
const logOutput = await getConsole();
const logs = JSON.parse(logOutput);
logs.slice(logIndex).forEach(function (log) {
console.log("BROWSERLOG:", log.join(" , "));
});
logIndex = logs.length;
}, 100);
async function waitForText(selector, timeout) {
return await client.waitUntil(
async function getText() {
logE("client.selector" + selector);
const el = await client.$(selector);
logE("el.isExisting" + selector);
if (!(await el.isExisting())) {
return false;
}
logE("el.getText" + selector);
const text = await el.getText();
if (text.length > 0) {
return true;
}
},
{
timeout,
timeoutMsg: `Expected to find text in ${selector} but did not find it`,
}
);
}
async function waitForExist(selector, timeout) {
return await client.waitUntil(
async function exists() {
logE("waitForExist.selector" + selector);
const el = await client.$(selector);
logE("waitForExist.selector" + selector);
if (await el.isExisting()) {
return true;
}
},
{
timeout,
timeoutMsg: `Expected to find ${selector} but did not find it`,
}
);
}
async function test() {
let interval;
try {
if (+new Date() - startTime > timeoutConnection * second) {
exit(
`Aborting connection to webdriver after ${timeoutConnection} seconds`
);
}
const mochaUrl = url.parse(
`http://localhost:${port}/test/mocha.html`,
true
);
delete mochaUrl.search;
if (process.env.filter) {
mochaUrl.query.grep = process.env.filter;
mochaUrl.query.invert = "true";
}
mochaUrl.query.browser = fullBrowserName;
await client.url(url.format(mochaUrl));
await waitForExist("li.test", 120000);
let index = 0;
let running = false;
interval = setInterval(async function () {
if (interval === null || running) {
return;
}
running = true;
logE("get h1,h2");
const texts = await client.$$("li h1, li h2");
if (index === texts.length) {
return;
}
for (let i = index, len = texts.length; i < len; i++) {
if (interval === null) {
return;
}
logE("get text[i]" + i);
const text = await texts[i].getText();
console.log(
text
.replace(/^(.*)\n(.*)$/g, "$2 $1")
.replace(/^(.*[^0-9])([0-9]+ms)$/g, "$1 $2")
);
}
index = texts.length;
running = false;
}, 100);
await waitForText("#status", 120000);
await client.pause(5000);
await waitForExist("li.failures a", 5000);
const text = await (await client.$("#mocha-stats")).getText();
clearInterval(interval);
setTimeout(function () {
interval = null;
}, 1000);
const passes = parseInt(text.replace(passesRegex, "$1"), 10);
const failures = parseInt(text.replace(failuresRegex, "$1"), 10);
if (failures > 0) {
await sleep(1000);
const failedSuites = await client.$$("li.test.fail");
for (let i = 0, len = failedSuites.length; i < len; i++) {
const titleElement = await await failedSuites[i].$("h2");
const title = await client.execute((parent) => {
let child = parent.firstChild;
let ret = "";
while (child) {
if (child.nodeType === Node.TEXT_NODE) {
ret += child.textContent;
}
child = child.nextSibling;
}
return ret;
}, titleElement);
const error = await (await failedSuites[i].$("pre.error")).getText();
console.log(title.replace(/./g, "="));
console.log(title);
console.log(title.replace(/./g, "="));
console.log(error);
console.log();
}
throw new Error(`${failures} failures happened on ${fullBrowserName}`);
}
expect(passes).to.be.above(0);
await sleep(1000);
console.log(
chalk.green(
`browser tests successful (${passes} passes) on ${fullBrowserName}`
)
);
if (BROWSER === "SAUCELABS") {
updateSaucelabsStatus(client, true, (e) => {
if (e) {
throw e;
}
server.close();
});
} else {
server.close();
}
logPostRequest = true;
setTimeout(function () {
client.deleteSession();
}, 5000);
} catch (e) {
clearInterval(interval);
interval = null;
logPostRequest = true;
if (e.message.indexOf("ECONNREFUSED") !== -1) {
return test();
}
if (BROWSER === "SAUCELABS") {
updateSaucelabsStatus(client, false, (err) => {
if (err) {
throw err;
}
exit(e);
});
} else {
exit(e);
}
}
}
test();
});