forked from fossasia/open-event-wsgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basePage.js
369 lines (296 loc) · 10.5 KB
/
basePage.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
var until = require('selenium-webdriver').until;
var By = require('selenium-webdriver').By;
var request = require('request');
var BasePage = {
init: function(webdriver) {
this.driver = webdriver;
},
visit: function(url) {
return this.driver.get(url);
},
find: function(locator, timeout) {
var waitTime = timeout || 20000;
this.driver.wait(until.elementLocated(locator, waitTime));
return this.driver.findElement(locator);
},
findAll: function(locator, timeout) {
var waitTime = timeout || 20000;
this.driver.wait(until.elementLocated(locator, waitTime));
return this.driver.findElements(locator);
},
getColor: function(el) {
var element = el || this;
return element.getAttribute('color');
},
click: function(el) {
return el.click();
},
toggleStarredButton: function() {
return this.find(By.id('starred')).then(this.click);
},
toggleSessionBookmark: function(sessionIds) {
var self = this;
var promiseArr = [];
sessionIds.forEach(function(sessionId) {
var promElem = new Promise(function(resolve, reject) {
self.find(By.id(sessionId)).then(function(el) {
el.findElement(By.className('bookmark')).then(self.click).then(function() {
resolve('done');
});
});
});
promiseArr.push(promElem);
});
return Promise.all(promiseArr);
},
getElemsDisplayStatus: function(arr) {
var promiseArr = [];
arr.forEach(function(elem) {
promiseArr.push(elem.isDisplayed());
});
return Promise.all(promiseArr);
},
getSessionElemsColor: function() {
var self = this;
var sessionElemIdArr = ['title-3014', 'title-2941'];
var colorPromArr = sessionElemIdArr.map(function(sessionElemId) {
return self.find(By.id(sessionElemId)).getCssValue('color');
});
return Promise.all(colorPromArr);
},
checkDownButton: function() {
var self = this;
return self.driver.executeScript('window.scrollTo(0, document.body.scrollHeight)').then(self.find.bind(self, By.id('down-button'))).then(function(el) {
return el.click().then(self.driver.sleep(1000)).then(function() {
return self.driver.executeScript('return window.scrollY');
});
});
},
search: function(text) {
var self = this;
var searchClass = 'fossasia-filter';
// There are two search input box with the class fossasia-filter. One of them is for mobile view and other is for bigger
// screens. We are doing this test on bigger screen so we have to select that input element and enter text in it. The second
// input element is for the bigger screens so working with it
return self.findAll(By.className(searchClass)).then(function(inputArr) {
return inputArr[1].sendKeys(text);
});
},
commonSearchTest: function(text, idList) {
var self = this;
var searchText = text || 'Mario';
// First 4 session ids should show up on default search text and the last two not
var arrId = idList || ['3017', '3029', '3013', '3031', '3014', '3015'];
var promise = new Promise(function(resolve) {
self.search(searchText).then(function() {
var promiseArr = arrId.map(function(curElem) {
return self.find(By.id(curElem)).isDisplayed();
});
self.resetSearchBar().then(function() {
resolve(Promise.all(promiseArr));
});
});
});
return promise;
},
resetSearchBar: function() {
var self = this;
var searchClass = 'fossasia-filter';
return self.findAll(By.className(searchClass)).then(function(inputArr) {
return inputArr[1].clear();
});
},
countOnesInArray: function(arr) {
return arr.reduce(function(counter, value) {
return (value === 1 || value === 'true') ? counter + 1 : counter;
}, 0);
},
getAllLinks: function(locator) {
function linksFromAnchorTags(anchorTags) {
var promiseArr = anchorTags.map(function(anchor) { return anchor.getAttribute('href'); });
return Promise.all(promiseArr);
}
return this.find(locator).then(function(el) {
return el.findElements(By.tagName('a')).then(linksFromAnchorTags);
});
},
countBrokenLinks: function(links) {
return new Promise(function(resolve) {
var brokenLinks = 0, counter = 0;
links.forEach(function(link) {
request(link, function(error, response) {
counter += 1;
if (error || response.statusCode == 404) { brokenLinks++; }
if (counter == links.length) { resolve(brokenLinks); }
});
});
});
},
getPageUrl: function() {
return this.driver.getCurrentUrl();
},
jumpToSpeaker: function() {
var self = this;
var sessionTitleId = 'title-3014';
var sessionDetailId = 'desc-3014';
var pageVertScrollOffset = 'return window.scrollY';
return new Promise(function(resolve) {
self.find(By.id(sessionTitleId)).then(self.click).then(function() {
self.find(By.id(sessionDetailId)).findElement(By.css('a')).click().then(self.getPageUrl.bind(self)).then(function(url) {
self.driver.executeScript(pageVertScrollOffset).then(function(height) {
resolve(height > 0 && (url.search('speakers') != -1));
});
});
});
});
},
resizeWindow: function(width, height) {
return this.driver.manage().window().setSize(width, height);
},
checkScrollbar: function() {
var scrollVisible = 'return document.documentElement.scrollWidth > document.documentElement.clientWidth';
return this.driver.executeScript(scrollVisible);
},
justSleep: function(duration) {
return this.driver.sleep(duration);
},
getVerticalOffset: function() {
var pageVertScrollOffset = 'return window.scrollY';
return this.driver.executeScript(pageVertScrollOffset);
},
goToTop: function() {
var self = this;
var sleepDuration = 1000;
return self.find(By.id('down-button')).click().then(function() {
return self.driver.sleep(sleepDuration);
});
},
subnavbarStatus: function(day) {
var self = this;
var tabSelectClass = 'tabs-nav-link';
var tabLinkContainerClass = 'tab-content';
var sleepDuration = 1000;
var subnavbarPromise = new Promise(function(resolve) {
self.findAll(By.className(tabSelectClass)).then(function(dayElems) {
dayElems[day - 1].click().then(function() {
self.findAll(By.className(tabLinkContainerClass)).then(function(linkContainer) {
linkContainer[day - 1].findElements(By.css('a')).then(function(linksArr) {
linksArr[linksArr.length - 1].click().then(self.justSleep.bind(self, sleepDuration))
.then(self.getVerticalOffset.bind(self)).then(function(height) {
self.goToTop().then(function() {
resolve(height > 0);
});
});
});
});
});
});
});
return subnavbarPromise;
},
checkAllSubnav: function() {
var self = this;
var promiseArr = [];
var counter = 1;
var days = 3;
while(counter <= days) {
promiseArr.push(self.subnavbarStatus(counter));
counter += 1;
}
return Promise.all(promiseArr);
},
getScrollbarVisibility: function(sizeList) {
var promiseArr = [];
var self = this;
sizeList.forEach(function(size) {
promiseArr.push(self.resizeWindow(size[0], size[1]).then(self.checkScrollbar.bind(self)));
});
return Promise.all(promiseArr);
},
getPos: function(el) {
return el.getLocation();
},
checkTrackNamePos: function() {
var self = this;
return self.find(By.className('track-room-names')).then(self.getPos).then(function(posObj) {
return posObj.y < 2000;
});
},
checkAlignment: function() {
var self = this;
var posPromArr = [];
posPromArr.push(self.find(By.id('tabs')).then(self.getPos));
posPromArr.push(self.find(By.className('text')).then(self.getPos));
var checkAlign = new Promise(function(resolve) {
Promise.all(posPromArr).then(function(posArr) {
resolve(Math.abs(posArr[0].x - posArr[1].x));
});
});
return checkAlign;
},
bookmarkCheck: function(sessionToggleArr, visCheckSessionArr) {
var self = this;
var sessionElemArr = visCheckSessionArr.map(function(id) {
return self.find(By.id(id));
});
return self.toggleSessionBookmark(sessionToggleArr).then(function() {
return self.driver.executeScript('window.scrollTo(0, 0)').then(self.toggleStarredButton.bind(self)).then(self.getElemsDisplayStatus.bind(null, sessionElemArr));
});
},
getAllWindows: function() {
return this.driver.getAllWindowHandles();
},
closeOtherWindows: function(windowArr, baseWindow) {
// Close all other windows and switch to the base window
var self = this;
var closeWindowProm = windowArr.map(function(windowElem) {
return self.driver.switchTo().window(windowElem).then(function() {
return self.driver.close();
});
});
return Promise.all(closeWindowProm).then(function() {
return self.driver.switchTo().window(baseWindow);
});
},
getSocialButtonElems: function() {
// Returns social button elements of a particular session
var sessionId = '3014';
var socialToggleClass = 'session-lin';
var socialButtonClass = 'social-button';
var self = this;
var socialButtonProm = new Promise(function(resolve) {
self.find(By.id(sessionId)).then(function(el) {
self.click(el).then(function() {
el.findElement(By.className(socialToggleClass)).click().then(function() {
resolve(el.findElements(By.className(socialButtonClass)));
});
});
});
});
return socialButtonProm;
},
checkSocialLinks: function() {
// Returns the number of social button links of a session which are working properly
var self = this;
var baseWindow;
self.driver.getWindowHandle().then(function(windowElem) {
baseWindow = windowElem;
});
var socialPromise = new Promise(function(resolve) {
self.getSocialButtonElems().then(function(socialArr) {
var windowPromArr = socialArr.map(function(linkElem) {
return linkElem.click();
});
Promise.all(windowPromArr).then(self.getAllWindows.bind(self)).then(function(windowArr) {
var len = windowArr.length;
windowArr.splice(windowArr.indexOf(baseWindow), 1);
self.closeOtherWindows(windowArr, baseWindow).then(function() {
resolve(len);
});
});
});
});
return socialPromise;
}
};
module.exports = BasePage;