forked from metabrainz/musicbrainz-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
selenium.mjs
executable file
·966 lines (840 loc) · 26.1 KB
/
selenium.mjs
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
#!./bin/sucrase-node
/*
* This file is part of MusicBrainz, the open internet music database.
* Copyright (C) 2017 MetaBrainz Foundation
* Licensed under the GPL version 2, or (at your option) any later version:
* http://www.gnu.org/licenses/gpl-2.0.txt
*/
import child_process from 'child_process';
import fs from 'fs';
import http from 'http';
import path from 'path';
import defined from 'defined';
import httpProxy from 'http-proxy';
import JSON5 from 'json5';
import webdriver from 'selenium-webdriver';
import chrome from 'selenium-webdriver/chrome.js';
import firefox from 'selenium-webdriver/firefox.js';
import {Key} from 'selenium-webdriver/lib/input.js';
import logging from 'selenium-webdriver/lib/logging.js';
import until from 'selenium-webdriver/lib/until.js';
import webdriverProxy from 'selenium-webdriver/proxy.js';
import test from 'tape';
import TestCls from 'tape/lib/test.js';
import yargs from 'yargs';
import * as DBDefs from '../root/static/scripts/common/DBDefs.mjs';
import deepEqual from '../root/static/scripts/common/utility/deepEqual.js';
import escapeRegExp
from '../root/static/scripts/common/utility/escapeRegExp.mjs';
import writeCoverage from '../root/utility/writeCoverage.mjs';
/*
* We can't run all of the Selenium tests concurrently
* because all tests share a database and Chrome driver
* so disabling no-await-in-loop for this file instead
*/
/* eslint-disable no-await-in-loop */
const argv = yargs
.option('b', {
alias: 'browser',
default: 'chrome',
describe: 'browser to use (chrome, firefox)',
type: 'string',
})
.option('c', {
alias: 'coverage',
default: true,
describe: 'dump coverage data to .nyc_output/',
type: 'boolean',
})
.option('h', {
alias: 'headless',
default: true,
describe: 'run Chrome in headless mode',
type: 'boolean',
})
.option('p', {
alias: 'browser-binary-path',
default: '',
describe: 'path to the browser binary, in case it cannot be auto-detected',
type: 'string',
})
.option('s', {
alias: 'stay-open',
default: false,
describe: 'stay logged in and keep the browser open after tests complete',
type: 'boolean',
})
.usage('Usage: $0 [-hs] [file...]')
.help('help')
.argv;
function compareEditDataValues(actualValue, expectedValue) {
if (expectedValue === '$$__IGNORE__$$') {
return true;
}
/*
* Handle cases where Perl's JSON module serializes numbers in the
* edit data as strings (something we can't fix easily).
*/
if (
(typeof actualValue === 'string' ||
typeof actualValue === 'number') &&
(typeof expectedValue === 'string' ||
typeof expectedValue === 'number')
) {
return String(actualValue) === String(expectedValue);
}
// Tells `deepEqual` to perform its default comparison.
return null;
}
TestCls.prototype.deepEqual2 = function (a, b, msg, extra) {
this._assert(deepEqual(a, b, compareEditDataValues), {
actual: a,
expected: b,
extra: extra,
message: defined(msg, 'should be equivalent'),
operator: 'deepEqual2',
});
};
async function _retryTest(isOk) {
for (let i = 0; i < 3; i++) {
if (await isOk()) {
break;
} else {
await driver.sleep(3000);
}
}
}
TestCls.prototype.equalWithRetry = async function (a, b) {
let aValue;
await _retryTest(async () => {
aValue = await a();
return Object.is(aValue, b);
});
this.equal(aValue, b);
};
TestCls.prototype.okWithRetry = async function (isOk) {
let ok;
await _retryTest(async () => {
ok = await isOk();
return ok;
});
this.ok(ok);
};
function wrapChildProcessMethod(methodName) {
return (...args) => {
return new Promise(function (resolve, reject) {
let exitCode = null;
let result = null;
function done() {
result.code = exitCode;
if (result.error) {
reject(result);
} else {
resolve(result);
}
}
const child = child_process[methodName](
...args,
function (error, stdout, stderr) {
result = {error, stdout, stderr};
if (exitCode !== null) {
done();
}
},
);
child.on('exit', function (code) {
exitCode = code;
if (result !== null) {
done();
}
});
});
};
}
const exec = wrapChildProcessMethod('exec');
const execFile = wrapChildProcessMethod('execFile');
const proxy = httpProxy.createProxyServer({});
let pendingReqs = [];
let reqsCount = 0;
proxy.on('proxyReq', function (req) {
pendingReqs.push(req);
reqsCount++;
});
const customProxyServer = http.createServer(function (req, res) {
const host = req.headers.host;
if (host === DBDefs.WEB_SERVER) {
req.headers['mb-set-database'] = 'SELENIUM';
req.rawHeaders['mb-set-database'] = 'SELENIUM';
}
proxy.web(req, res, {target: 'http://' + host}, function (e) {
console.error(e);
});
});
const driver = (x => {
let options;
switch (argv.browser) {
case 'chrome':
x.forBrowser('chrome');
options = new chrome.Options();
options.addArguments(
'disable-dev-shm-usage',
'no-sandbox',
'proxy-server=http://localhost:5051',
);
if (argv.browserBinaryPath) {
options.setChromeBinaryPath(argv.browserBinaryPath);
}
x.setChromeOptions(options);
break;
case 'firefox':
x.forBrowser('firefox');
options = new firefox.Options();
options.setPreference('dom.disable_beforeunload', false);
options.setPreference('network.proxy.allow_hijacking_localhost', true);
if (argv.browserBinaryPath) {
options.setBinary(argv.browserBinaryPath);
}
x.setFirefoxOptions(options);
break;
default:
throw new Error('Unsupported browser: ' + argv.browser);
}
x.setProxy(webdriverProxy.manual({http: 'localhost:5051'}));
if (argv.headless) {
options.headless();
}
return x.build();
})(new webdriver.Builder());
function quit() {
proxy.close();
customProxyServer.close();
return driver.quit().catch(console.error);
}
async function unhandledRejection(err) {
console.error(err);
if (!argv.stayOpen) {
await quit();
process.exit(1);
}
}
process.on('unhandledRejection', unhandledRejection);
function makeLocator(locatorStr) {
const splitAt = locatorStr.indexOf('=');
let using = locatorStr.slice(0, splitAt);
const value = locatorStr.slice(splitAt + 1);
if (using === 'link') {
using = 'linkText';
}
return webdriver.By[using](value);
}
function findElement(locatorStr) {
return driver.wait(
until.elementLocated(makeLocator(locatorStr)),
30000, // 30 seconds
);
}
async function getElementText(locatorStr) {
// The Selenium IDE converts tabs and newlines to normal spaces.
return (await findElement(locatorStr).getText()).replace(/\s/g, ' ').trim();
}
async function setChecked(element, wantChecked) {
const checked = await element.isSelected();
if (checked !== wantChecked) {
await element.click();
}
}
async function selectOption(select, optionLocator) {
await select.click();
const splitAt = optionLocator.indexOf('=');
const prefix = optionLocator.slice(0, splitAt);
let value = optionLocator.slice(splitAt + 1);
let option;
switch (prefix) {
case 'label':
if (value.startsWith('regexp:')) {
value = new RegExp(value.slice(7));
} else {
value = new RegExp('^\s*' + escapeRegExp(value) + '\s*$');
}
option = await select.findElement(async function () {
const options =
await select.findElements(webdriver.By.tagName('option'));
for (const option of options) {
if (value.test(await option.getText())) {
return option;
}
}
return null;
});
break;
default:
throw 'Unsupported select prefix: ' + prefix;
}
if (!option) {
throw 'Option not found: ' + optionLocator;
}
return option.click();
}
const KEY_CODES = {
'${KEY_BKSP}': Key.BACK_SPACE,
'${KEY_DOWN}': Key.ARROW_DOWN,
'${KEY_END}': Key.END,
'${KEY_ENTER}': Key.ENTER,
'${KEY_ESC}': Key.ESCAPE,
'${KEY_HOME}': Key.HOME,
'${KEY_SHIFT}': Key.SHIFT,
'${KEY_TAB}': Key.TAB,
'${KEY_UP}': Key.ARROW_UP,
'${MBS_ROOT}': DBDefs.MB_SERVER_ROOT.replace(/\/$/, ''),
};
function getPageErrors() {
return driver.executeScript('return ((window.MB || {}).js_errors || [])');
}
let coverageObjectIndex = 0;
function writeSeleniumCoverage(coverageString) {
writeCoverage(`selenium-${coverageObjectIndex++}`, coverageString);
}
async function writePreviousSeleniumCoverage() {
/*
* `previousCoverage` means for the previous window.
*
* We only want to write the __coverage__ object to disk before the page
* is about to change, for the obvious reason that it's not complete until
* then, but also because retrieving the large (> 1MB) __coverage__ object
* from the driver is slow, so we only want to do that when absolutely
* necessary.
*/
const previousCoverage = await driver.executeScript(
`if (!window.__seen__) {
window.__seen__ = true;
window.addEventListener('beforeunload', function () {
sessionStorage.setItem(
'__previous_coverage__',
JSON.stringify(window.__coverage__),
);
});
return sessionStorage.getItem('__previous_coverage__');
}
return null;`,
);
if (previousCoverage) {
writeSeleniumCoverage(previousCoverage);
}
}
function timePrefix(str) {
return `[${(new Date()).toISOString().slice(-13)}] ${str}`;
}
async function checkSirQueues(t) {
let failedCount = 0;
let retryCount = 0;
let indexCount = 0;
let deleteCount = 0;
let prevIndexCount = 0;
let prevDeleteCount = 0;
while (
(prevIndexCount + prevDeleteCount) === 0 ||
// Continue if the queues are actually decreasing.
(indexCount - prevIndexCount) < 0 ||
(deleteCount - prevDeleteCount) < 0
) {
prevIndexCount = indexCount;
prevDeleteCount = deleteCount;
const result = await exec(
"sudo -n rabbitmqctl list_queues -p '/sir-test' -q --formatter=json",
);
const sirQueues = JSON.parse(result.stdout);
const messageCounts = sirQueues.reduce((map, queue) => {
map.set(queue.name, queue.messages);
return map;
}, new Map());
failedCount = messageCounts.get('search.failed') || 0;
retryCount = messageCounts.get('search.retry') || 0;
indexCount = messageCounts.get('search.index') || 0;
deleteCount = messageCounts.get('search.delete') || 0;
if (failedCount || retryCount) {
throw new Error(
'non-empty sir queues: ' +
`search.failed (${failedCount}), search.retry (${retryCount})`,
);
}
if (indexCount || deleteCount) {
t.comment(timePrefix(
'waiting for non-empty sir queues: ' +
`search.index (${indexCount}), search.delete (${deleteCount})`,
));
await driver.sleep(5000);
} else {
return;
}
}
throw new Error(
'non-empty sir queues: ' +
`search.index (${indexCount}), search.delete (${deleteCount})`,
);
}
async function getSeleniumDbTupStats() {
const result = await execFile(
path.resolve(DBDefs.MB_SERVER_ROOT, 'script/get_selenium_tup_stats.sh'),
);
return JSON.parse(result.stdout);
}
async function handleCommandAndWait({command, target, value}, t) {
const newCommand = command.replace(/AndWait$/, '');
const html = await findElement('css=html');
await handleCommand({command: newCommand, target, value}, t);
return driver.wait(until.stalenessOf(html), 30000);
}
async function handleCommand({command, target, value}, t) {
if (/AndWait$/.test(command)) {
return handleCommandAndWait.apply(null, arguments);
}
// Wait for all pending network requests before running the next command.
await driver.wait(function () {
pendingReqs = pendingReqs.filter(req => !(req.aborted || req.finished));
return pendingReqs.length === 0;
});
t.comment(timePrefix(
command +
' target=' + JSON.stringify(target) +
' value=' + JSON.stringify(value),
));
if (
typeof value === 'string' &&
value.startsWith('$$_EVAL_$$')
) {
const valueScript = value.slice(10);
// eslint-disable-next-line require-atomic-updates
value = await driver.executeScript(`return (${valueScript})`);
}
let element;
switch (command) {
case 'assertArtworkJson':
const artworkJson = JSON.parse(await driver.executeAsyncScript(`
var callback = arguments[arguments.length - 1];
fetch('http://localhost:8081/release/${target}')
.then(x => x.text().then(callback));
`));
t.deepEqual2(artworkJson, value);
break;
case 'assertAttribute':
const splitAt = target.indexOf('@');
const locator = target.slice(0, splitAt);
const attribute = target.slice(splitAt + 1);
element = await findElement(locator);
await t.equalWithRetry(
async () => element.getAttribute(attribute),
value,
);
break;
case 'assertElementPresent':
await t.okWithRetry(async () => {
const elements = await driver.findElements(makeLocator(target));
return elements.length > 0;
});
break;
case 'assertEval':
await t.equalWithRetry(
async () => driver.executeScript(`return String(${target})`),
value,
);
break;
case 'assertEditData':
const actualEditData = JSON.parse(await driver.executeAsyncScript(`
var callback = arguments[arguments.length - 1];
fetch('/edit/${target}/data', {
credentials: 'same-origin',
method: 'GET',
headers: new Headers({'Accept': 'application/json'}),
}).then(x => x.text().then(callback));
`));
t.deepEqual2(actualEditData, value);
break;
case 'assertLocationMatches':
await t.okWithRetry(async () => {
return new RegExp(target).test(await driver.getCurrentUrl());
});
break;
case 'assertText':
await t.equalWithRetry(
async () => getElementText(target),
value.trim(),
);
break;
case 'assertTextMatches':
await t.okWithRetry(async () => {
return new RegExp(value).test(await getElementText(target));
});
break;
case 'assertTitle':
await t.equalWithRetry(
async () => driver.getTitle(),
target,
);
break;
case 'assertValue':
await t.equalWithRetry(
async () => findElement(target).getAttribute('value'),
value,
);
break;
case 'check':
return setChecked(findElement(target), true);
case 'click':
element = await findElement(target);
await driver.executeScript('arguments[0].scrollIntoView()', element);
await element.click();
break;
case 'fireEvent':
await driver.executeScript(
`arguments[0].dispatchEvent(new Event('${value}'))`,
await findElement(target),
);
break;
case 'focus':
await driver.executeScript(
'arguments[0].focus()',
await findElement(target),
);
break;
case 'handleAlert':
await driver.switchTo().alert()[target]();
break;
case 'mouseOver':
await driver.actions()
.move({origin: await findElement(target)})
.perform();
break;
case 'open':
await driver.get('http://' + DBDefs.WEB_SERVER + target);
break;
case 'pause':
await driver.sleep(target);
break;
case 'runScript':
await driver.executeScript(target);
break;
case 'select':
await selectOption(await findElement(target), value);
break;
case 'sendKeys':
// eslint-disable-next-line require-atomic-updates
value = value.split(/(\$\{[A-Z_]+\})/)
.filter(x => x)
.map(x => KEY_CODES[x] || x);
element = await findElement(target);
await element.sendKeys.apply(element, value);
break;
case 'type':
element = await findElement(target);
/*
* XXX *Both* of the next two lines are needed to clear the input
* in some cases. (Just one or the other won't suffice.) It's not
* known what module is at fault, but this combination is
* confirmed to misbehave:
*
* Chrome 70.0.3538.110
* ChromeDriver 2.44.609545
* chrome-remote-interface 0.27.0
* selenium-webdriver 3.6.0
*/
await element.clear();
await driver.executeScript(
'window.MB.setInputValueForReact(arguments[0], "")',
element,
);
await element.sendKeys(value);
break;
case 'uncheck':
await setChecked(findElement(target), false);
break;
case 'waitUntilUrlIs':
await driver.wait(until.urlIs(
'http://' + DBDefs.WEB_SERVER + target,
), 30000);
break;
default:
throw 'Unsupported command: ' + command;
}
return null;
}
/* eslint-disable sort-keys */
const seleniumTests = [
{name: 'Create_Account.json5'},
{name: 'MBS-2604.json5', login: true},
{name: 'MBS-5387.json5', login: true},
{name: 'MBS-7456.json5', login: true},
{name: 'MBS-7646.json5'},
{name: 'MBS-8952.json5', login: true},
{name: 'MBS-9396.json5', login: true},
{name: 'MBS-9548.json5'},
{name: 'MBS-9669.json5'},
{name: 'MBS-9941.json5', login: true},
{name: 'MBS-10188.json5', login: true, sql: 'mbs-10188.sql'},
{name: 'MBS-10510.json5', login: true, sql: 'mbs-10510.sql'},
{name: 'MBS-11529.json5', login: true},
{name: 'MBS-11730.json5', login: true},
{name: 'MBS-11735.json5', login: true},
{name: 'MBS-12641.json5', login: true},
{name: 'MBS-12859.json5', login: true},
{name: 'MBS-12874.json5', login: true},
{name: 'MBS-12885.json5', login: true},
{name: 'MBS-12904.json5', login: true},
{
name: 'MBS-12911.json5',
login: true,
sql: 'vision_creation_newsun.sql',
},
{name: 'MBS-12921.json5', login: true},
{name: 'MBS-12922.json5', login: true},
{
name: 'MBS-13015.json5',
login: true,
sql: 'mbs-13015.sql',
},
{name: 'MBS-13392.json5', login: true},
{name: 'Artist_Credit_Editor.json5', login: true},
{name: 'Autocomplete2.json5'},
{name: 'CAA.json5', login: true},
{name: 'External_Links_Editor.json5', login: true},
{name: 'Work_Editor.json5', login: true},
{name: 'Redirect_Merged_Entities.json5', login: true},
{name: 'admin/Edit_Banner.json5', login: true},
{name: 'Relationship_Seeding.json5', login: true},
{name: 'release-editor/The_Downward_Spiral.json5', login: true},
{
name: 'release-editor/Duplicate_Selection.json5',
login: true,
sql: 'whatever_it_takes.sql',
},
{
name: 'release-editor/Seeding.json5',
login: true,
sql: 'vision_creation_newsun.sql',
},
{name: 'release-editor/MBS-4555.json5', login: false},
{name: 'release-editor/MBS-10221.json5', login: true},
{name: 'release-editor/MBS-10359.json5', login: true},
{name: 'release-editor/MBS-11015.json5', login: true},
{name: 'release-editor/MBS-11114.json5', login: true},
{name: 'release-editor/MBS-11156.json5', login: true},
{
login: false,
name: 'release-editor/MBS-12077.json5',
sql: 'vision_creation_newsun.sql',
},
{name: 'release-editor/MBS-13207.json5', login: true},
{name: 'release-editor/MBS-13273.json5', login: true},
{
name: 'Check_Duplicates.json5',
login: true,
sql: 'duplicate_checker.sql',
},
{name: 'CD_Lookup.json5', login: true},
{name: 'CD_Stub_Search.json5', sql: 'cdstub_raw.sql'},
{name: 'FilterForm.json5', sql: 'filtering.sql'},
{
name: 'Release_Relationship_Editor.json5',
login: true,
sql: 'vision_creation_newsun.sql',
},
{
name: 'Series_Relationship_Editor.json5',
login: true,
},
{
name: 'Artist_Edit_Form.json5',
login: true,
},
{
name: 'Event_Edit_Form.json5',
login: true,
},
{
name: 'Genre_Edit_Form.json5',
login: true,
},
{
name: 'Recording_Edit_Form.json5',
login: true,
},
{
name: 'Vote_And_Note_Submission.json5',
login: true,
sql: 'vote_and_note_selenium.sql',
},
];
/* eslint-enable sort-keys */
const testPath =
name => path.resolve(DBDefs.MB_SERVER_ROOT, 't/selenium', name);
seleniumTests.forEach(x => {
x.path = testPath(x.name);
});
function getPlan(file) {
const document = JSON5.parse(fs.readFileSync(file));
const commands = document.commands;
let plan = 0;
for (let i = 0; i < commands.length; i++) {
const row = commands[i];
if (/^assert/.test(row.command)) {
plan++;
}
row.file = file;
}
document.plan = plan;
return document;
}
async function runCommands(commands, t) {
await driver.manage().window().setRect({height: 768, width: 1024});
for (let i = 0; i < commands.length; i++) {
const reqsCountBeforeCommand = reqsCount;
await handleCommand(commands[i], t);
/*
* Wait for sir queues to empty before proceeding. rabbitmqctl
* list_queues is extremely slow (taking more than 500 ms to run on my
* local machine), so we try to avoid this for commands that don't have
* side-effects, like assertions. If no new requests have been made, we
* also consider the command to be side-effect free.
*/
if (
process.env.SIR_DIR &&
!/^assert/.test(commands[i].command) &&
reqsCount > reqsCountBeforeCommand
) {
await checkSirQueues(t);
}
const nextCommand = i < (commands.length - 1) ? commands[i + 1] : null;
/*
* If there's an alert open on the page, we can't execute any scripts;
* they'll die with an UnexpectedAlertOpenError. Since these must be
* handled explicitly with the `handleAlert` command, we check if that's
* the next command before proceeding.
*/
if (!nextCommand || nextCommand.command !== 'handleAlert') {
if (argv.coverage) {
await writePreviousSeleniumCoverage();
}
// Die if there are any JS errors on the page since the previous command
const errors = await getPageErrors();
if (errors.length) {
throw new Error(
'Errors were found on the page ' +
'since executing the previous command:\n' +
errors.join('\n\n'),
);
}
// The CATALYST_DEBUG views interfere with our tests. Remove them.
await driver.executeScript(`
node = document.getElementById('plDebug');
if (node) node.remove();
`);
}
}
}
(async function runTests() {
const TEST_TIMEOUT = 480000; // 8 minutes
async function cleanSeleniumDb(t, extraSql) {
const startTime = new Date();
await execFile(
path.resolve(DBDefs.MB_SERVER_ROOT, 'script/reset_selenium_env.sh'),
extraSql
? [path.resolve(DBDefs.MB_SERVER_ROOT, 't/sql', extraSql)]
: [],
);
const finishTime = new Date();
const elapsedTime = (finishTime - startTime) / 1000;
t.comment(timePrefix(
`cleanSeleniumDb(): took ${elapsedTime} seconds`,
));
}
const loginPlan = getPlan(testPath('Log_In.json5'));
const logoutPlan = getPlan(testPath('Log_Out.json5'));
const testsPathsToRun = argv._.map(x => path.resolve(x));
const testsToRun = testsPathsToRun.length
? seleniumTests.filter(x => testsPathsToRun.includes(x.path))
: seleniumTests;
customProxyServer.listen(5051);
let shouldCleanSeleniumDb = true;
await testsToRun.reduce(function (accum, stest, index) {
const {commands, plan, title} = getPlan(stest.path);
const isLastTest = index === testsToRun.length - 1;
const testOptions = {objectPrintDepth: 10, timeout: TEST_TIMEOUT};
return new Promise(function (resolve) {
test(title, testOptions, function (t) {
t.plan(plan);
const timeout = setTimeout(resolve, TEST_TIMEOUT);
accum.then(async function () {
const hasExtraSql = typeof stest.sql === 'string';
if (hasExtraSql || shouldCleanSeleniumDb) {
await cleanSeleniumDb(t, stest.sql);
}
const startTime = new Date();
let didDatabaseChange = hasExtraSql;
const startTupStats = didDatabaseChange
? null
: (await getSeleniumDbTupStats());
try {
if (stest.login) {
await runCommands(loginPlan.commands, t);
}
await runCommands(commands, t);
if (!(isLastTest && argv.stayOpen)) {
if (stest.login) {
await runCommands(logoutPlan.commands, t);
}
}
} catch (error) {
t.fail(
'caught exception: ' +
(error && error.stack ? error.stack : error.toString()),
);
throw error;
} finally {
await driver.manage().logs().get(logging.Type.BROWSER)
.then(function (entries) {
entries.forEach(function (entry) {
t.comment(
'[browser console log] ' +
`[${entry.level.name}] ${entry.message}`,
);
});
});
const finishTime = new Date();
const elapsedTime = (finishTime - startTime) / 1000;
t.comment(timePrefix(
`${title}: took ${elapsedTime} seconds`,
));
if (!didDatabaseChange) {
const finishTupStats = await getSeleniumDbTupStats();
didDatabaseChange = (
finishTupStats.tup_inserted > startTupStats.tup_inserted ||
finishTupStats.tup_updated > startTupStats.tup_updated ||
finishTupStats.tup_deleted > startTupStats.tup_deleted
);
}
/*
* Tests always run serially, one after another.
* The require-atomic-updates violation appears to be a false-
* positive from eslint.
*/
// eslint-disable-next-line require-atomic-updates
shouldCleanSeleniumDb = didDatabaseChange;
}
t.end();
clearTimeout(timeout);
resolve();
});
});
});
}, Promise.resolve());
if (argv.coverage) {
const remainingCoverage = await driver.executeScript(
'return JSON.stringify(window.__coverage__)',
);
if (remainingCoverage) {
writeSeleniumCoverage(remainingCoverage);
}
}
if (!argv.stayOpen) {
await quit();
}
}());