-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
672 lines (579 loc) · 22.1 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
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
'use strict';
/* eslint new-cap: 0 */ // --> OFF for Given, When, Then
/*
* Created by marketionist on 13.11.2016
*/
// #############################################################################
const { defineSupportCode } = require('cucumber');
// Use the external Chai As Promised to deal with resolving promises in
// expectations
const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
const fs = require('fs');
const protractor = require('protractor');
const censor = require('./utils/helpers.js').censor;
const errors = require('./utils/errors.js');
chai.use(chaiAsPromised);
const expect = chai.expect;
const EC = protractor.ExpectedConditions;
const defaultCustomTimeout = 5000;
const customTimeout = browser.params.customTimeout || defaultCustomTimeout;
const pageObjects = browser.params.pageObjects;
const timeToWaitMax = 300100; // Maximum time to wait for in 'I wait for (\d+) ms' step
/**
* Waits for the element to be present and displayed on the page
* @param {string} elementSelector
*/
function waitForDisplayed (elementSelector) {
browser.wait(EC.presenceOf(elementSelector), customTimeout,
errors.ELEMENT_PRESENT);
}
/**
* Composes proper element locator for further actions
* @param {string} page
* @param {string} elem
* @returns {Object} elmnt
*/
function composeLocator (page, elem) {
const locator = pageObjects[page][elem];
let elmnt;
if (locator[0] + locator[1] === '//') {
elmnt = element(by.xpath(locator));
} else {
elmnt = element(by.css(locator));
}
return elmnt;
}
/**
* Composes proper WebdriverJS element locator (inherited from webdriver.By to work without Angular) for further actions
* @param {string} page
* @param {string} elem
* @returns {Object} elmnt
*/
function composeLocatorWebdriver (page, elem) {
const locator = pageObjects[page][elem];
let elmnt;
if (locator[0] + locator[1] === '//') {
elmnt = browser.driver.findElement(by.xpath(locator));
} else {
elmnt = browser.driver.findElement(by.css(locator));
}
return elmnt;
}
/**
* Navigates to URL provided in page object
* @param {string} page
* @param {string} elem
* @returns {Promise} promise
*/
function goTo (page, elem) {
const url = pageObjects[page][elem];
return browser.get(url);
}
/**
* Clicks on element provided in page object
* @param {string} page
* @param {string} elem
* @returns {Promise} promise
*/
function clickOn (page, elem) {
const elmnt = composeLocator(page, elem);
waitForDisplayed(elmnt);
browser.wait(EC.elementToBeClickable(elmnt), customTimeout,
`"${pageObjects[page][elem]}" ${errors.CLICKABLE}`);
return elmnt.click();
}
/**
* Waits for 300 ms and then licks on element provided in page object
* @param {string} page
* @param {string} elem
* @param {function} callback
*/
function waitAndClickOn (page, elem, callback) {
const elmnt = composeLocator(page, elem);
const timeToWait = 300;
waitForDisplayed(elmnt);
browser.wait(EC.elementToBeClickable(elmnt), customTimeout,
`"${pageObjects[page][elem]}" ${errors.CLICKABLE}`);
setTimeout(function () {
elmnt.click().then(function () {
callback();
});
}, timeToWait);
}
/**
* Clicks on element provided in page object only if it is present on the page
* @param {string} page
* @param {string} elem
* @returns {Promise} promise
*/
function clickIfPresent (page, elem) {
const elmnt = composeLocator(page, elem);
return elmnt.isPresent().then(function (isPresent) {
if (isPresent) {
// Click only if element is present
return elmnt.click();
}
})
}
/**
* Double clicks on element provided in page object only if it is present on the page
* @param {string} page
* @param {string} elem
* @returns {Promise} promise
*/
function doubleClickOn (page, elem) {
const elmnt = composeLocator(page, elem);
waitForDisplayed(elmnt);
browser.wait(EC.elementToBeClickable(elmnt), customTimeout,
`"${pageObjects[page][elem]}" ${errors.CLICKABLE}`);
return browser.actions().mouseMove(elmnt).doubleClick().perform();
}
/**
* Wait for element provided in page object to be present on the page
* @param {string} page
* @param {string} elem
* @param {function} callback
*/
function waitForPresent (page, elem, callback) {
const elmnt = composeLocator(page, elem);
waitForDisplayed(elmnt);
elmnt.isPresent().then(function (isPresent) {
if (isPresent) {
callback();
} else {
callback(new Error(errors.ELEMENT_PRESENT));
}
});
}
/**
* Type any text (provided in "" as a string) in the input field provided in page object
* @param {string} text
* @param {string} page
* @param {string} elem
* @returns {Promise} promise
*/
function typeIn (text, page, elem) {
const inputField = composeLocator(page, elem);
waitForDisplayed(inputField);
browser.wait(EC.elementToBeClickable(inputField), customTimeout,
`"${pageObjects[page][elem]}" ${errors.CLICKABLE}`);
browser.actions().mouseMove(inputField).click().perform();
return inputField.sendKeys(text);
}
/**
* Type any text provided in page object in the input field provided in page object
* @param {string} page1
* @param {string} element1
* @param {string} page2
* @param {string} element2
* @returns {Promise} promise
*/
function typePageObjectIn (page1, element1, page2, element2) {
const inputField = composeLocator(page2, element2);
waitForDisplayed(inputField);
browser.wait(EC.elementToBeClickable(inputField), customTimeout,
`"${pageObjects[page2][element2]}" ${errors.CLICKABLE}`);
browser.actions().mouseMove(inputField).click().perform();
return inputField.sendKeys(pageObjects[page1][element1]);
}
/**
* Move the mouse pointer over any element provided in page object
* @param {string} page
* @param {string} elem
* @returns {Promise} promise
*/
function moveTo (page, elem) {
const elmnt = composeLocator(page, elem);
waitForDisplayed(elmnt);
return browser.actions().mouseMove(elmnt).perform();
}
/**
* Move the mouse pointer over any element (provided in page object) with an offset of x: ...px, y: ...px
* @param {string} page
* @param {string} elem
* @param {number} offsetX
* @param {number} offsetY
* @returns {Promise} promise
*/
function moveWithOffsetTo (page, elem, offsetX, offsetY) {
const elmnt = composeLocator(page, elem);
const integerX = parseInt(offsetX, 10) || 0;
const integerY = parseInt(offsetY, 10) || 0;
waitForDisplayed(elmnt);
return browser.actions().mouseMove(elmnt).mouseMove({ x: integerX, y: integerY }).perform();
}
/**
* Switch the context to iframe provided in page object
* @param {string} page
* @param {string} elem
* @returns {Promise} promise
*/
function switchToFrame (page, elem) {
const elmnt = composeLocator(page, elem);
waitForDisplayed(elmnt);
return browser.switchTo().frame(elmnt);
}
/**
* Switch the context to non angular iframe provided in page object
* @param {string} page
* @param {string} elem
* @returns {Promise} promise
*/
function switchToNonAngularFrame (page, elem) {
const elmntWebdriver = composeLocatorWebdriver(page, elem);
return browser.driver.switchTo().frame(elmntWebdriver);
}
/**
* Verify that element provided in page object is present on the page
* @param {string} page
* @param {string} elem
* @returns {Promise} promise
*/
function verifyPresent (page, elem) {
const elmnt = composeLocator(page, elem);
return expect(elmnt.isPresent()).to.eventually.equal(true);
}
/**
* Verify that element provided in page object is not present on the page
* @param {string} page
* @param {string} elem
* @returns {Promise} promise
*/
function verifyNotPresent (page, elem) {
const elmnt = composeLocator(page, elem);
return expect(elmnt.isPresent()).to.eventually.equal(false);
}
/**
* Verify that text of the element provided in page object equals to the text (provided in "" as a string)
* @param {string} page
* @param {string} elem
* @param {string} text
* @returns {Promise} promise
*/
function verifyText (page, elem, text) {
const elmnt = composeLocator(page, elem);
return expect(elmnt.getText()).to.eventually.equal(text);
}
/**
* Verify that text of the element provided in page object equals to the text provided in page object
* @param {string} page1
* @param {string} element1
* @param {string} page2
* @param {string} element2
* @returns {Promise} promise
*/
function verifyPageObjectText (page1, element1, page2, element2) {
const elmnt = composeLocator(page1, element1);
const text = pageObjects[page2][element2];
return expect(elmnt.getText()).to.eventually.equal(text);
}
/**
* Verify that text of the element provided in page object contains the text provided in page object
* @param {string} page
* @param {string} elem
* @param {string} textPart
* @param {function} callback
*/
function verifyTextContains (page, elem, textPart, callback) {
const elmnt = composeLocator(page, elem);
elmnt.getText().then(function (text) {
if (text.indexOf(textPart) === -1) {
callback(new Error(`"${text}" ${errors.CONTAIN} "${textPart}"`));
} else {
callback();
}
});
}
defineSupportCode(function ({ Given, When, Then }) {
// #### When steps #########################################################
When(/^I go to URL "([^"]*)"$/, function (url, callback) {
browser.get(url).then(function () {
callback();
});
});
When(/^I go to "([a-zA-Z0-9_]+)"."([a-zA-Z0-9_]+)"$/, function (page, elem, callback) {
goTo(page, elem).then(function () {
callback();
});
});
When(/^I go to ([a-zA-Z0-9_]+) from ([a-zA-Z0-9_]+) page$/, function (elem, page, callback) {
goTo(page, elem).then(function () {
callback();
});
});
When(/^I reload the page$/, function (callback) {
browser.refresh().then(function () {
callback();
});
});
When(/^I click "([a-zA-Z0-9_]+)"."([a-zA-Z0-9_]+)"$/, function (page, elem, callback) {
clickOn(page, elem).then(function () {
callback();
});
});
When(/^I click ([a-zA-Z0-9_]+) from ([a-zA-Z0-9_]+) page$/, function (elem, page, callback) {
clickOn(page, elem).then(function () {
callback();
});
});
When(/^I wait and click "([a-zA-Z0-9_]+)"."([a-zA-Z0-9_]+)"$/, function (page, elem, callback) {
waitAndClickOn(page, elem, callback);
});
When(/^I wait and click ([a-zA-Z0-9_]+) from ([a-zA-Z0-9_]+) page$/, function (elem, page, callback) {
waitAndClickOn(page, elem, callback);
});
When(/^I click "([a-zA-Z0-9_]+)"."([a-zA-Z0-9_]+)" if present$/, function (page, elem, callback) {
clickIfPresent(page, elem).then(function () {
callback();
});
});
When(/^I click ([a-zA-Z0-9_]+) from ([a-zA-Z0-9_]+) page if present$/, function (elem, page, callback) {
clickIfPresent(page, elem).then(function () {
callback();
});
});
When(/^I double click "([a-zA-Z0-9_]+)"."([a-zA-Z0-9_]+)"$/, function (page, elem, callback) {
doubleClickOn(page, elem).then(function () {
callback();
});
});
When(/^I double click ([a-zA-Z0-9_]+) from ([a-zA-Z0-9_]+) page$/, function (elem, page, callback) {
doubleClickOn(page, elem).then(function () {
callback();
});
});
When(/^I wait for (\d+) ms$/, { timeout: timeToWaitMax }, function (timeToWait, callback) {
setTimeout(callback, timeToWait);
});
When(/^I wait for "([a-zA-Z0-9_]+)"."([a-zA-Z0-9_]+)" to be present$/, function (page, elem, callback) {
waitForPresent(page, elem, callback);
});
When(/^I wait for ([a-zA-Z0-9_]+) from ([a-zA-Z0-9_]+) page to be present$/, function (elem, page, callback) {
waitForPresent(page, elem, callback);
});
When(/^I type "([^"]*)" in "([a-zA-Z0-9_]+)"."([a-zA-Z0-9_]+)"$/, function (text, page, elem, callback) {
typeIn(text, page, elem).then(function () {
callback();
});
});
When(/^I type "([^"]*)" in ([a-zA-Z0-9_]+) from ([a-zA-Z0-9_]+) page$/, function (text, elem, page, callback) {
typeIn(text, page, elem).then(function () {
callback();
});
});
When(/^I type "([a-zA-Z0-9_]+)"."([a-zA-Z0-9_]+)" in "([a-zA-Z0-9_]+)"."([a-zA-Z0-9_]+)"$/, function (
page1, element1, page2, element2, callback) { // eslint-disable-line indent
typePageObjectIn(page1, element1, page2, element2).then(function () {
callback();
});
});
When(/^I type ([a-zA-Z0-9_]+) from ([a-zA-Z0-9_]+) page in ([a-zA-Z0-9_]+) from ([a-zA-Z0-9_]+) page$/, function (
element1, page1, element2, page2, callback) { // eslint-disable-line indent
typePageObjectIn(page1, element1, page2, element2).then(function () {
callback();
});
});
When(/^I move to "([a-zA-Z0-9_]+)"."([a-zA-Z0-9_]+)"$/, function (page, elem, callback) {
moveTo(page, elem).then(function () {
callback();
});
});
When(/^I move to ([a-zA-Z0-9_]+) from ([a-zA-Z0-9_]+) page$/, function (elem, page, callback) {
moveTo(page, elem).then(function () {
callback();
});
});
When(/^I move to "([a-zA-Z0-9_]+)"."([a-zA-Z0-9_]+)" with an offset of x: (\d+)px, y: (\d+)px$/, function (
page, elem, offsetX, offsetY, callback) { // eslint-disable-line indent
moveWithOffsetTo(page, elem, offsetX, offsetY).then(function () {
callback();
});
});
When(/^I move to ([a-zA-Z0-9_]+) from ([a-zA-Z0-9_]+) page with an offset of x: (\d+)px, y: (\d+)px$/, function (
elem, page, offsetX, offsetY, callback) { // eslint-disable-line indent
moveWithOffsetTo(page, elem, offsetX, offsetY).then(function () {
callback();
});
});
When(/^I switch to "([a-zA-Z0-9_]+)"."([a-zA-Z0-9_]+)" frame$/, function (page, elem, callback) {
switchToFrame(page, elem).then(function () {
callback();
});
});
When(/^I switch to ([a-zA-Z0-9_]+) frame from ([a-zA-Z0-9_]+) page$/, function (elem, page, callback) {
switchToFrame(page, elem).then(function () {
callback();
});
});
When(/^I switch to "([a-zA-Z0-9_]+)"."([a-zA-Z0-9_]+)" non angular frame$/, function (page, elem, callback) {
switchToNonAngularFrame(page, elem).then(function () {
callback();
});
});
When(/^I switch to ([a-zA-Z0-9_]+) non angular frame from ([a-zA-Z0-9_]+) page$/, function (elem, page, callback) {
switchToNonAngularFrame(page, elem).then(function () {
callback();
});
});
When(/^I switch to default frame$/, function (callback) {
browser.switchTo().defaultContent().then(function () {
callback();
});
});
When(/^I execute "([^"]*)"$/, function (script, callback) {
browser.driver.executeScript(script).then(function () {
callback();
});
});
When(/^I open new tab$/, function (callback) {
// Inject a link with target="_blank" to the current page
browser.driver.executeScript(function () {
document.body.innerHTML += '<a href="about:blank" id="link-to-open-new-tab" target="_blank">Link</a>';
return document.body.innerHTML;
}).then(function () {
// Click on injected link to open new tab
return element(by.id('link-to-open-new-tab')).click();
}).then(function () {
// Switch to new tab
return browser.getAllWindowHandles();
}).then(function (handles) {
let lastTabHandle = handles[handles.length - 1];
return browser.switchTo().window(lastTabHandle);
}).then(function () {
callback();
});
});
When(/^I close current tab$/, function (callback) {
// Close current tab/window
browser.driver.close().then(function () {
// Switch to last active tab/window
return browser.getAllWindowHandles();
}).then(function (handles) {
let previousTabHandle = handles[handles.length - 1];
return browser.switchTo().window(previousTabHandle);
}).then(function () {
callback();
});
});
When(/^I switch to first tab$/, function (callback) {
browser.getAllWindowHandles().then(function (handles) {
let firstTabHandle = handles[0];
return browser.switchTo().window(firstTabHandle);
}).then(function () {
callback();
});
});
When(/^I switch to last tab$/, function (callback) {
browser.getAllWindowHandles().then(function (handles) {
let lastTabHandle = handles[handles.length - 1];
return browser.switchTo().window(lastTabHandle);
}).then(function () {
callback();
});
});
When(/^I accept browser alert$/, function (callback) {
// Waits for an alert to appear
browser.wait(EC.alertIsPresent(), customTimeout, errors.ALERT_PRESENT);
browser.switchTo().alert().accept().then(function () {
callback();
});
});
When(/^I dismiss browser alert$/, function (callback) {
// Waits for an alert to appear
browser.wait(EC.alertIsPresent(), customTimeout, errors.ALERT_PRESENT);
browser.switchTo().alert().dismiss().then(function () {
callback();
});
});
When(/^I authenticate in browser alert with login "([^"]*)" and password "([^"]*)"$/, function (
login, password, callback) { // eslint-disable-line indent
// Waits for an alert to appear
browser.wait(EC.alertIsPresent(), customTimeout, errors.ALERT_PRESENT);
browser.switchTo().alert().authenticateAs(login, password).then(function () {
callback();
});
});
// #### Then steps #########################################################
Then(/the title should be "([^"]*)"$/, function (text, callback) {
expect(browser.getTitle()).to.eventually.equal(text).and.notify(callback);
});
Then(/^"([a-zA-Z0-9_]+)"."([a-zA-Z0-9_]+)" should be present$/, function (page, elem, callback) {
verifyPresent(page, elem).and.notify(callback);
});
Then(/^([a-zA-Z0-9_]+) from ([a-zA-Z0-9_]+) page should be present$/, function (elem, page, callback) {
verifyPresent(page, elem).and.notify(callback);
});
Then(/^"([a-zA-Z0-9_]+)"."([a-zA-Z0-9_]+)" should not be present$/, function (page, elem, callback) {
verifyNotPresent(page, elem).and.notify(callback);
});
Then(/^([a-zA-Z0-9_]+) from ([a-zA-Z0-9_]+) page should not be present$/, function (elem, page, callback) {
verifyNotPresent(page, elem).and.notify(callback);
});
Then(/^"([a-zA-Z0-9_]+)"."([a-zA-Z0-9_]+)" text should be "([^"]*)"$/, function (page, elem, text, callback) {
verifyText(page, elem, text).and.notify(callback);
});
Then(/^([a-zA-Z0-9_]+) text from ([a-zA-Z0-9_]+) page should be "([^"]*)"$/, function (elem, page, text, callback) {
verifyText(page, elem, text).and.notify(callback);
});
Then(/^"([a-zA-Z0-9_]+)"."([a-zA-Z0-9_]+)" text should be "([a-zA-Z0-9_]+)"."([a-zA-Z0-9_]+)"$/, function (
page1, element1, page2, element2, callback) { // eslint-disable-line indent
verifyPageObjectText(page1, element1, page2, element2).and.notify(callback);
});
Then(/^([a-zA-Z0-9_]+) text from ([a-zA-Z0-9_]+) page should be ([a-zA-Z0-9_]+) from ([a-zA-Z0-9_]+) page$/,
function (element1, page1, element2, page2, callback) {
verifyPageObjectText(page1, element1, page2, element2).and.notify(callback);
}
);
Then(/^"([a-zA-Z0-9_]+)"."([a-zA-Z0-9_]+)" text should contain "([^"]*)"$/, function (
page, elem, textPart, callback) { // eslint-disable-line indent
verifyTextContains(page, elem, textPart, callback);
});
Then(/^([a-zA-Z0-9_]+) text from ([a-zA-Z0-9_]+) page should contain "([^"]*)"$/, function (
elem, page, textPart, callback) { // eslint-disable-line indent
verifyTextContains(page, elem, textPart, callback);
});
Then(/^"([a-zA-Z0-9_]+)"."([a-zA-Z0-9_]+)" text should contain "([a-zA-Z0-9_]+)"."([a-zA-Z0-9_]+)"$/, function (
page1, element1, page2, element2, callback) { // eslint-disable-line indent
const elmnt = composeLocator(page1, element1);
const textPart = pageObjects[page2][element2];
elmnt.getText().then(function (text) {
if (text.indexOf(textPart) === -1) {
callback(new Error(`"${text}" ${errors.CONTAIN} "${textPart}"`));
} else {
callback();
}
});
});
Then(/^URL should be "([^"]*)"$/, function (url, callback) {
expect(browser.getCurrentUrl()).to.eventually.equal(url).and.notify(callback);
});
Then(/^URL should match \/([^"]*)\/$/, function (regexp, callback) {
browser.getCurrentUrl().then(function (url) {
if (new RegExp(regexp).test(url)) {
callback();
} else {
callback(new Error(`"${url}" ${errors.REGEXP} /${regexp}/`));
}
});
});
Then(/^URL should contain "([^"]*)"$/, function (urlPart, callback) {
browser.getCurrentUrl().then(function (url) {
if (url.indexOf(urlPart) === -1) {
callback(new Error(`"${url}" ${errors.CONTAIN} "${urlPart}"`));
} else {
callback();
}
});
});
// Take a callback as an additional argument to execute when the step is done
Then(/^the file "([^"]*)" is empty$/, function (fileName, callback) {
fs.readFile(fileName, 'utf8', function (error, contents) {
if (error) {
callback(error);
} else {
expect(contents).to.eventually.equal('').and.notify(callback);
}
});
});
});