-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhelpers.js
More file actions
715 lines (658 loc) · 18.9 KB
/
helpers.js
File metadata and controls
715 lines (658 loc) · 18.9 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
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
/* eslint-disable no-use-before-define */
// REQUIRE MODULES
const { By, until, Key } = require("selenium-webdriver");
// const sharp = require('sharp');
const path = require("path");
// REQUIRE FILES
// const config = require('../../server/config');
// const css = require('./selectors');
// testing timeout values
const slowFactor = 1.3;
const timeoutMs = 4000 * (slowFactor); // timeout per await
let TOstr = ( slowFactor * 20 ) + 's'
const timeoutTestMsStr = TOstr; // timeout per test
const baseURL = "http://oldvmt.mathematicalthinking.org";
// const nconf = config.nconf;
// const port = nconf.get('testPort');
// const host = `http://localhost:${port}`;
// const loginUrl = `${host}/#/auth/login`;
const getCurrentUrl = async function (webdriver) {
let url;
try {
url = await webdriver.getCurrentUrl();
} catch (err) {
console.log(err.message);
}
return url;
};
const isElementVisible = async function (webDriver, selector) {
let isVisible = false;
try {
const webElements = await webDriver.findElements(By.css(selector));
if (webElements.length === 1) {
isVisible = await webElements[0].isDisplayed();
}
} catch (err) {
if (err.name === "StaleElementReferenceError") {
// element is no longer in dom
return false;
}
console.log({ isElementVisibleError: err });
}
return isVisible;
};
const getWebElements = async function (webDriver, selector) {
let webElements = [];
try {
webElements = await webDriver.findElements(By.css(selector));
} catch (err) {
console.log(err.message);
}
return webElements;
};
const getXPathElements = async function (webDriver, path) {
let webElements = [];
try {
webElements = await webDriver.findElements(By.xpath(path));
} catch (err) {
console.log(err.message);
}
return webElements;
};
const getWebElementValue = async function (webDriver, selector) {
let webElement, webValue;
try {
webElement = await webDriver.findElement(By.css(selector));
webValue = await webElement.getAttribute("value");
} catch (err) {
console.log(err.message);
}
return webValue;
};
const getWebElementTooltip = async function (webDriver, selector) {
let webElement, webValue;
try {
webElement = await webDriver.findElement(By.css(selector));
webValue = await webElement.getAttribute("data-tooltip");
} catch (err) {
console.log(err.message);
}
return webValue;
};
const navigateAndWait = async function (
webDriver,
url,
selector,
timeout = timeoutMs
) {
await webDriver.get(url);
return webDriver.wait(until.elementLocated(By.css(selector)), timeout);
};
const findAndGetText = async function (
webDriver,
selector,
caseInsenstive = false
) {
let text;
try {
let webElements = await webDriver.findElements(By.css(selector));
if (webElements.length === 1) {
text = await webElements[0].getText();
}
if (caseInsenstive) {
text = text.toLowerCase();
}
} catch (err) {
console.log(err.message);
}
return text;
};
const isTextInDom = function (webDriver, text) {
return webDriver
.getPageSource()
.then((source) => {
return typeof source === "string" && source.includes(text);
})
.catch((err) => {
throw err;
});
};
const hasTooltipValue = async function (webDriver, selector, value) {
let hasValue;
try {
let dataValue = await getWebElementTooltip(webDriver, selector);
hasValue = dataValue === value ? true : false;
} catch (err) {
console.log(err.message);
}
return hasValue;
};
const findAndClickElement = async function (webDriver, selector) {
let elements = await getWebElements(webDriver, selector);
if (elements.length > 0) {
return elements[0].click();
}
return;
};
// New helper function to fix URL domain issue
const findAndDLElement = async function (webDriver, selector) {
console.log("DL Element selector: ", selector);
let elements = await getWebElementByCss(webDriver, selector);
console.log("Found elements :", elements);
if (elements.length > 0) {
let oldURL = await elements[0].getAttribute("onclick");
oldURL = String(oldURL);
// console.log('oldURL: ', oldURL);
// old URL syntax: window.location.href="http://192.168.1.110:8080/vmtChat/nativeExport.jsp?channelID=CID:1374769578339&filename=Room_3"
oldURL = oldURL.substr(22);
oldURL = oldURL.substring(0, oldURL.length - 1);
let newURL = oldURL.replace("http://192.168.1.110:8080", baseURL);
newURL = encodeURI(newURL);
console.log("Old url parsed: ", oldURL, "; Corrected URL: ", newURL);
return webDriver.get(newURL);
}
return;
};
// New helper function to fix URL domain issue, uses direct URL approach
const findAndDLbyURL = async function (webDriver, roomName, CID) {
let newURL =
baseURL +
"/vmtChat/nativeExport.jsp?channelID=" +
CID +
"&filename=" +
roomName;
newURL = encodeURI(newURL);
// console.log("Corrected URL for JNO DL: ", newURL);
try {
webDriver.get(newURL);
} catch (err) {
console.log(err.message);
}
return newURL;
};
// location.href="http://192.168.1.110:8080/vmtChat/logExport?channelID=CID:1430311635466&roomName=vmt math&reportType=1"
const findCSVAndDLbyURL = async function (webDriver, roomName, CID) {
let newURL =
baseURL +
"/vmtChat/logExport?channelID=" +
CID +
"&roomName=" +
roomName +
"&reportType=1";
newURL = encodeURI(newURL);
// console.log("Corrected URL for CSV DL: ", newURL);
try {
webDriver.get(newURL);
} catch (err) {
console.log(err.message);
}
return newURL;
};
const waitForAndClickElement = function (
webDriver,
selector,
timeout = timeoutMs
) {
return webDriver
.wait(
until.elementLocated(By.css(selector)),
timeout,
`Unable to locate element by selector: ${selector}`
)
.then((locatedEl) => {
return webDriver
.wait(
until.elementIsVisible(locatedEl),
timeout,
`Element ${selector} not visible`
)
.then((visibleEl) => {
return visibleEl.click();
});
})
.catch((err) => {
throw err;
});
};
const waitForTextInDom = function (webDriver, text, timeout = timeoutMs) {
return webDriver
.wait(
function () {
return isTextInDom(webDriver, text);
},
timeout,
`Could not find ${text} in DOM`
)
.catch((err) => {
throw err;
});
};
const waitForSelector = function (webDriver, selector, timeout = timeoutMs) {
return webDriver
.wait(until.elementLocated(By.css(selector)), timeout)
.catch((err) => {
throw err;
});
};
const waitForRemoval = async function (
webDriver,
selector,
timeout = timeoutMs
) {
try {
return await webDriver.wait(async function () {
return (await isElementVisible(webDriver, selector)) === false;
}, timeout);
} catch (err) {
if (err.name === "StaleElementReferenceError") {
// element we are waiting to be removed has already been removed
return false;
}
throw err;
}
};
const findInputAndType = async function (
webDriver,
selector,
text,
doHitEnter = false
) {
try {
let input = await getWebElements(webDriver, selector);
if (input.length > 0) {
await input[0].sendKeys(text);
if (doHitEnter) {
return input[0].sendKeys(Key.ENTER);
}
}
} catch (err) {
console.log(err.message);
}
return;
};
const checkSelectorsExist = function (webDriver, selectors) {
return Promise.all(
selectors.map((selector) => {
return isElementVisible(webDriver, selector);
})
).then((selectors) => {
return selectors.every((x) => x === true);
});
};
const createSelectors = function (filterOptions) {
let options = filterOptions.map((item) => {
return Object.values(item);
});
return [].concat.apply([], options);
};
const createFilterList = function (
isStudent,
isAdmin,
filterList,
removeChildren
) {
let filterOptions = [...filterList];
if (removeChildren) {
filterOptions.forEach((item) => {
if (item.hasOwnProperty("children")) {
delete item.children;
}
});
}
if (isAdmin) {
filterOptions.forEach((item) => {
if (item.hasOwnProperty("adminOnly")) {
delete item.adminOnly;
}
});
}
if (!isStudent && !isAdmin) {
filterOptions.forEach((item, i) => {
if (item.hasOwnProperty("adminOnly")) {
filterOptions.splice(i, 1);
}
});
}
if (isStudent) {
filterOptions = [];
}
return filterOptions;
};
const selectOption = async function (webDriver, selector, item, isByCss) {
try {
let selectList;
if (isByCss) {
selectList = await webDriver.findElement(By.css(selector));
} else {
selectList = await webDriver.findElement(By.id(selector));
}
await webDriver.sleep(150*slowFactor)
await selectList.click();
await webDriver.sleep(350*slowFactor)
let el = await selectList.findElement(By.css(`option[value="${item}"]`));
await el.click();
return el;
} catch (err) {
console.log("Select Option error! - ", err.message);
throw err;
}
};
const login = async function (webDriver, host, user = admin) {
await navigateAndWait(webDriver, host, css.topBar.login);
await findAndClickElement(webDriver, css.topBar.login);
await waitForSelector(webDriver, css.login.username);
await findInputAndType(webDriver, css.login.username, user.username);
await findInputAndType(webDriver, css.login.password, user.password);
await findAndClickElement(webDriver, css.login.submit);
return waitForSelector(webDriver, css.topBar.logout);
};
const signup = async function (
webDriver,
missingFields = [],
user = newUser,
acceptedTerms = true
) {
const inputs = css.signup.inputs;
for (let input of Object.keys(inputs)) {
if (input !== "terms" && !missingFields.includes(input)) {
try {
// eslint-disable-next-line no-await-in-loop
if (input === "organization") {
// hit enter to select org from dropdown
// eslint-disable-next-line no-await-in-loop
await findInputAndType(webDriver, inputs[input], user[input], true);
} else {
// eslint-disable-next-line no-await-in-loop
await findInputAndType(webDriver, inputs[input], user[input]);
}
} catch (err) {
console.log(err.message);
}
}
}
try {
if (acceptedTerms) {
await findAndClickElement(webDriver, inputs.terms);
}
await findAndClickElement(webDriver, css.signup.submit);
} catch (err) {
console.log(err.message);
}
};
const clearElement = async function (webDriver, element) {
let ele;
try {
let elements = await getWebElements(webDriver, element);
ele = elements[0];
await ele.clear();
} catch (err) {
console.log(err.message);
}
};
const waitForUrlMatch = async function (webDriver, regex, timeout = timeoutMs) {
try {
await webDriver.wait(until.urlMatches(regex), timeout);
return true;
} catch (err) {
console.error(`Error waitForUrlMatch: ${err}`);
console.trace();
return false;
}
};
const saveScreenshot = function (webdriver) {
return webdriver.takeScreenshot().then((base64Data) => {
let buffer = Buffer.from(base64Data, "base64");
return sharp(buffer)
.toFile(path.join(__dirname, "screenshots", `${Date.now()}.png`))
.catch((err) => {
console.log(`Error saving screenshot: ${err}`);
});
});
};
const waitForNElements = function (
webDriver,
selector,
num,
timeout = timeoutMs
) {
let conditionFn = () => {
return getWebElements(webDriver, selector).then((els) => {
return els.length === num;
});
};
return webDriver.wait(conditionFn, timeout).catch((err) => {
throw err;
});
};
const dismissErrorBox = function (webDriver) {
let xBtn = css.general.errorBoxDismiss;
return findAndClickElement(webDriver, xBtn).then(() => {
return waitForRemoval(webDriver, css.general.errorBox);
});
};
const waitForAndGetErrorBoxText = function (webDriver) {
return findAndGetText(webDriver, css.general.errorBoxText);
};
const selectSingleSelectizeItem = function (
webDriver,
inputSelector,
text,
itemValue,
options = { willInputClearOnSelect: false }
) {
let { willInputClearOnSelect, toastText } = options;
return getWebElementByCss(webDriver, inputSelector)
.then((selectizeInput) => {
return selectizeInput.sendKeys(text).then(() => {
let dataValSelector = `div[data-value="${itemValue}"]`;
return waitForAndClickElement(webDriver, dataValSelector).then(() => {
return getParentElement(selectizeInput).then((parentNode) => {
if (!willInputClearOnSelect) {
return waitForElementToHaveText(webDriver, parentNode, text);
}
if (toastText) {
return waitForTextInDom(webDriver, toastText);
}
return parentNode;
});
});
});
})
.catch((err) => {
throw err;
});
};
const getWebElementByCss = function (webDriver, selector) {
// not for testing existence or visibility
return webDriver.findElement(By.css(selector)).catch((err) => {
throw err;
});
};
const waitForElementToHaveText = function (
webDriver,
webElOrSelector,
expectedText,
timeout
) {
let conditionFn;
let isSelector = typeof webElOrSelector === "string";
if (isSelector) {
conditionFn = () => {
return findAndGetText(webDriver, webElOrSelector).then((text) => {
return text === expectedText;
});
};
} else {
conditionFn = () => {
return webElOrSelector.getText().then((val) => {
return val === expectedText;
});
};
}
return webDriver.wait(conditionFn, timeout || timeoutMs).catch((err) => {
throw err;
});
};
const getParentElement = function (webElement) {
return webElement.findElement(By.xpath("./..")).catch((err) => {
throw err;
});
};
const waitForAttributeToEql = function (
webDriver,
webElement,
attributeName,
expectedValue,
timeout = timeoutMs
) {
let conditionFn = () => {
return webElement.getAttribute(attributeName).then((attributeVal) => {
return attributeVal === expectedValue;
});
};
return webDriver.wait(conditionFn, timeout).catch((err) => {
throw err;
});
};
const logout = function (webDriver) {
let loginRegex = new RegExp("/#/auth/login");
return findAndClickElement(webDriver, css.topBar.logout)
.then(() => {
return waitForUrlMatch(webDriver, loginRegex);
})
.catch((err) => {
throw err;
});
};
const dismissWorkspaceTour = function (webDriver) {
let xBtnSel = css.workspace.tour.xBtn;
let overlaySel = css.workspace.tour.overlay;
return waitForSelector(webDriver, xBtnSel, 1000)
.then((xBtn) => {
return xBtn.click().then(() => {
return waitForRemoval(webDriver, overlaySel);
});
})
.catch((err) => {
if (err.name === "TimeoutError") {
// tour box didnt pop up
return true;
}
throw err;
});
};
const waitForElementsChild = async function (
webDriver,
element,
xpath,
timeout = timeoutMs
) {
// console.log({ xpath });
let conditionFn = () => {
return element
.findElements({ xpath })
.then((els) => {
if (els.length === 0) {
// console.log("could not find child: ", xpath);
return false;
}
let el = els[0];
// console.log("here we are: ", el);
return el.isDisplayed() ? el : false;
})
.catch((err) => {
console.log(err.message);
});
};
return webDriver.wait(conditionFn, timeout).catch((err) => {
console.log(err.message);
});
};
const selectOptionByIndex = async function (
webDriver,
selector,
index,
isByCss
) {
try {
let selectList;
if (isByCss) {
selectList = await webDriver.findElement(By.css(selector));
} else {
selectList = await webDriver.findElement(By.id(selector));
}
await selectList.click();
let els = await selectList.findElements({ css: "> li" });
console.log(els, "els");
let el = els[index];
await el.click();
return el;
} catch (err) {
console.log(err.message);
throw err;
}
};
//boilerplate setup for running tests by account type
// async function runTests(users) {
// async function _runTests(user) {
// const { accountType, actingRole, testDescriptionTitle } = user;
// describe(`As ${testDescriptionTitle}`, async function() {
// this.timeout(helpers.timeoutTestMsStr);
// let driver = null;
// before(async function() {
// driver = new Builder()
// .forBrowser('chrome')
// .build();
// await dbSetup.prepTestDb();
// return await helpers.login(driver, host, user);
// });
// after(async function() {
// return await driver.quit();
// });
// });
//TESTS HERE
// }
// for (let user of Object.keys(users)) {
// await _runTests(users[user]);
// }
// }
module.exports.getWebElements = getWebElements;
module.exports.getWebElementValue = getWebElementValue;
module.exports.getWebElementTooltip = getWebElementTooltip;
module.exports.navigateAndWait = navigateAndWait;
module.exports.isElementVisible = isElementVisible;
module.exports.findAndGetText = findAndGetText;
module.exports.isTextInDom = isTextInDom;
module.exports.hasTooltipValue = hasTooltipValue;
module.exports.findAndClickElement = findAndClickElement;
module.exports.waitForSelector = waitForSelector;
module.exports.findInputAndType = findInputAndType;
module.exports.checkSelectorsExist = checkSelectorsExist;
module.exports.createSelectors = createSelectors;
module.exports.createFilterList = createFilterList;
module.exports.selectOption = selectOption;
module.exports.waitForAndClickElement = waitForAndClickElement;
module.exports.waitForTextInDom = waitForTextInDom;
module.exports.getCurrentUrl = getCurrentUrl;
module.exports.login = login;
module.exports.signup = signup;
module.exports.clearElement = clearElement;
module.exports.waitForRemoval = waitForRemoval;
module.exports.timeoutTestMsStr = timeoutTestMsStr;
module.exports.waitForUrlMatch = waitForUrlMatch;
module.exports.saveScreenshot = saveScreenshot;
module.exports.waitForNElements = waitForNElements;
module.exports.dismissErrorBox = dismissErrorBox;
module.exports.waitForAndGetErrorBoxText = waitForAndGetErrorBoxText;
module.exports.selectSingleSelectizeItem = selectSingleSelectizeItem;
module.exports.getWebElementByCss = getWebElementByCss;
module.exports.waitForElementToHaveText = waitForElementToHaveText;
module.exports.waitForAttributeToEql = waitForAttributeToEql;
module.exports.logout = logout;
module.exports.dismissWorkspaceTour = dismissWorkspaceTour;
module.exports.waitForElementsChild = waitForElementsChild;
module.exports.findAndDLElement = findAndDLElement;
module.exports.findAndDLbyURL = findAndDLbyURL;
module.exports.findCSVAndDLbyURL = findCSVAndDLbyURL;
module.exports.baseURL = baseURL;
module.exports.slowFactor = slowFactor;