forked from readium/readium-js-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathold_tests.js
636 lines (472 loc) · 22.4 KB
/
old_tests.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
// Copyright (c) 2014 Readium Foundation and/or its licensees. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation and/or
// other materials provided with the distribution.
// 3. Neither the name of the organization nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
var webdriver = require('selenium-webdriver');
var extensionUrl = 'chrome-extension://mgknhofkmkhdmjgjjbbcdcipolnmgipj/index.html';
var driver;
exports.setUp = function(callback){
var capabilities = webdriver.Capabilities.chrome();
capabilities.set('chromeOptions', {'args' : 'load-extension=' + process.cwd() + '/build/chrome-app'});
driver = new webdriver.Builder().
withCapabilities(capabilities).
build();
callback();
}
exports.tearDown = function(callback){
//driver.close();
driver.quit();
callback();
}
var testErrback = function(test, msg){
debugger;
var msg = msg || 'Required element is not present or visible'
test.ok(false, msg);
test.done();
}
var waitForPresent = function(test, locator, msg, timeout){
return driver.wait(function(){
return driver.isElementPresent(locator);
}, timeout || 5000).addErrback(testErrback.bind(null, test, msg));
}
var waitForVisible = function(test, element, msg, timeout){
return driver.wait(function(){
return element.isDisplayed().then(function(displayed){
return displayed;
});
}, timeout || 5000).addErrback(testErrback.bind(null, test, msg));
}
var initAddToLibTests = function(test){
driver.get(extensionUrl);
driver.sleep(3000);
driver.get(extensionUrl);
var err = testErrback.bind(null, test);
var btn = driver.findElement({className : 'icon-add-epub'});
btn.addErrback(err);
btn.click();
var dlg = driver.findElement({id: 'add-epub-dialog'});
waitForVisible(test, dlg, 'The add epub dialog didn\'t appear when the button was clicked');
}
exports.testLibraryNavbar = function(test){
driver.get(extensionUrl);
driver.sleep(3000);
driver.get(extensionUrl);
waitForPresent(test, {className : 'navbar'}, 'navbar not present');
waitForPresent(test, {className : 'icon-add-epub'}, 'epub button not present').then(function(){
//test.ok(present, 'Add epub button not present');
test.done();
});
}
var verifyProgressBar = function(test){
var progressBar = driver.findElement({className: 'progress-bar'});
progressBar.addErrback(testErrback.bind(null, test, 'The progress dialog didn\'t get created'));
waitForVisible(test, progressBar, 'The progress bar was never made visible');
};
var verifyLibraryItem = function(test, expectedTitle, expectedAuthor){
waitForPresent(test, {className:'library-item'}, 'The item was not imported into the library', 80000);
var titleElement = driver.findElement({css: '.library-item .title'});
titleElement.addErrback(testErrback.bind(null, test, 'The imported book has no title element'));
titleElement.getText().then(function(text){
test.equal(text, expectedTitle, 'Incorrect title');
});
var authorElement = driver.findElement({css: '.library-item .author'});
authorElement.addErrback(testErrback.bind(null, test, 'The imported book has no author element'));
authorElement.getText().then(function(text){
test.equal(text, expectedAuthor);
});
return driver.executeScript("var naturalWidth = $('img.read').prop('naturalWidth');\
return $('img.read').prop('complete') && typeof naturalWidth != 'undefined' && naturalWidth > 0").then(function(goodImg){
test.ok(goodImg, "Cover image is broken");
});
}
exports.addToLibTests =
{
// testAddEpubFromWeb : function(test){
// var err = testErrback.bind(null, test);
// // have to reload because it seems like some chrome.* APIs don't get initialized unless I do.
// initAddToLibTests(test);
// var urlUpload = driver.findElement({id: 'url-upload'});
// urlUpload.addErrback(err);
// var testUrl = 'http://www.gutenberg.org/ebooks/30971.epub.images';
// urlUpload.sendKeys(testUrl).then(function(){
// var dlgSubmit = driver.findElement({className: 'add-book'});
// dlgSubmit.addErrback(err);
// dlgSubmit.click();
// });
// verifyProgressBar(test);
// verifyLibraryItem(test, 'Industrial Revolution', 'Poul William Anderson').then(function(){
// test.done();
// })
// },
testAddEpubFromZipFile : function(test){
initAddToLibTests(test);
var epubUpload = driver.findElement({id: 'epub-upload'});
epubUpload.addErrback(testErrback.bind(null, test, 'file upload control does not exist'));
var testFile = process.cwd() + '/chrome-app/tests/epubs/cc-shared-culture.epub';
epubUpload.sendKeys(testFile);
verifyProgressBar(test);
verifyLibraryItem(test, 'Creative Commons - A Shared Culture', 'Jesse Dylan').then(function(){
test.done();
});
},
testAddEpubFromDirectory : function(test){
// can't really test directory selects because of https://code.google.com/p/chromedriver/issues/detail?id=183
// leaving it in but commented out because it may be fixed one day.
test.done();
// initAddToLibTests(test);
// var dirUpload = driver.findElement({id: 'dir-upload'});
// dirUpload.addErrback(testErrback.bind(null, test, 'directory upload control does not exist'));
// var testDir = process.cwd() + '/chrome-app/tests/epubs/accessible_epub_3'
// driver.sleep(3000);
// dirUpload.sendKeys(testDir);
// verifyProgressBar(test);
// verifyLibraryItem(test, 'asdasd', 'adadad').then(function(){
// test.done();
// })
}
}
var uploadTestFile = function(test, path){
var epubUpload = driver.findElement({id: 'epub-upload'});
epubUpload.addErrback(testErrback.bind(null, test, 'file upload control does not exist'));
var testFile = process.cwd() + path;
epubUpload.sendKeys(testFile);
};
var loadDetailsDialog = function(test){
waitForPresent(test, {className:'details'}, 'The item was not imported into the library', 40000);
driver.sleep(1000);
var detailsBtn = driver.findElement({className: 'details'});
detailsBtn.click();
waitForPresent(test, {className: 'details-dialog'}, 'The details dialog did not appear', 2000);
waitForPresent(test, {className: 'details-body'}, 'The details never loaded');
};
exports.libViewTests = {
testViewDetails : function(test){
initAddToLibTests(test);
uploadTestFile(test, '/chrome-app/tests/epubs/accessible_epub_3.epub');
loadDetailsDialog(test);
var title = driver.findElement({css: '.modal-book-info .modal-title'});
title.addErrback(testErrback.bind(null, test, 'The details title does not exist'));
title.getText().then(function(text){
test.equal(text, 'Accessible EPUB 3');
});
driver.findElements({css: '.modal-book-info .modal-detail'}).then(function(items){
test.equal(items.length, 6, "There should be 6 detail items");
items[0].getText().then(function(text){
test.equal(text, 'Author: Matt Garrish', 'author detail is wrong');
});
items[1].getText().then(function(text){
test.equal(text, 'Publisher: O’Reilly Media, Inc.', 'publisher detail is wrong');
})
items[2].getText().then(function(text){
test.equal(text, 'Pub Date: 2012-02-20', 'publish date is wrong');
})
items[3].getText().then(function(text){
test.equal(text, 'Modified Date: 2012-10-24T15:30:00Z', 'modified date is wrong');
})
items[4].getText().then(function(text){
test.equal(text, 'ID: urn:isbn:9781449328030', 'id detail is wrong');
})
items[5].getText().then(function(text){
test.equal(text, 'EPUB version: 3.0', 'EPUB version detail is wrong');
})
}).then(function(){
test.done();
});
},
testReadFromDetails : function(test){
initAddToLibTests(test);
uploadTestFile(test, '/chrome-app/tests/epubs/accessible_epub_3.epub');
loadDetailsDialog(test);
var readBtn = driver.findElement({css: '.details-body .btn.read'});
readBtn.addErrback(testErrback.bind(null, test, 'The read button does not exist'));
driver.sleep(500);
readBtn.click();
waitForPresent(test, {id: 'epubContentIframe'}, 'The reader never loaded after trying to open it from the details dialog');
//driver.sleep(1000);
driver.switchTo().frame('epubContentIframe').addErrback(testErrback.bind(null, test, 'The reader frame does not exist'));
waitForPresent(test, {id: 'cover-image'}, 'The book was not loaded into the reader').then(function(){
test.done();
});
},
testSwitchViews : function(test){
initAddToLibTests(test);
uploadTestFile(test, '/chrome-app/tests/epubs/accessible_epub_3.epub');
waitForPresent(test, {className:'details'}, 'The item was not imported into the library', 40000);
driver.sleep(500);
var thumbnailButton = driver.findElement({className: 'icon-thumbnails'});
thumbnailButton.addErrback(testErrback.bind(null, test, 'The thumbnail button does not exist'));
thumbnailButton.isDisplayed().then(function(displayed){
test.ok(!displayed, 'The thumbnail button should be hidden');
});
var listViewButton = driver.findElement({className: 'icon-list-view'});
thumbnailButton.addErrback(testErrback.bind(null, test, 'The list view button does not exist'));
listViewButton.isDisplayed().then(function(displayed){
test.ok(displayed, 'the list view button should be visible');
});
listViewButton.click();
waitForPresent(test, {css: 'body.list-view'}, 'The list view was not enabled');
listViewButton.isDisplayed().then(function(displayed){
test.ok(!displayed, 'the list view button should not be visible');
});
thumbnailButton.isDisplayed().then(function(displayed){
test.ok(displayed, 'The thumbnail button should be visible');
});
thumbnailButton.click();
driver.isElementPresent({css: 'body.list-view'}).then(function(present){
test.ok(!present, 'The thumbnail view was not enabled');
test.done();
});
}
}
var switchToReaderFrame = function(test){
driver.switchTo().frame('epubContentIframe').addErrback(testErrback.bind(null, test, 'The reader frame does not exist'));
}
var switchToDefaultFrame = function(){
driver.switchTo().defaultContent();
}
var findElement = function(test, locator, msg){
var element = driver.findElement(locator);
element.addErrback(testErrback.bind(null, test, msg));
return element;
}
var readerSetUp = function(callback){
driver.get(extensionUrl);
driver.sleep(2000);
driver.get(extensionUrl);
err = callback;
var btn = driver.findElement({className : 'icon-add-epub'});
btn.addErrback(err);
btn.click();
driver.sleep(1000);
var epubUpload = driver.findElement({id: 'epub-upload'});
epubUpload.addErrback(callback);
var testFile = process.cwd() + '/chrome-app/tests/epubs/accessible_epub_3.epub';
epubUpload.sendKeys(testFile);
driver.wait(function(){
return driver.isElementPresent({className: 'read'});
}, 40000).addErrback(callback);
driver.sleep(500);
var read = driver.findElement({className: 'read'}).click();
driver.wait(function(){
return driver.isElementPresent({id: 'reading-area'});
}, 6000).addErrback(callback);
callback();
}
exports.readerTests = {
setUp : readerSetUp,
testTurnPage : function(test){
switchToReaderFrame(test);
waitForPresent(test, {id: 'cover-image'}, 'The book was not loaded into the reader');
//driver.sleep(2000);
switchToDefaultFrame();
var nextBtn = driver.findElement({id: 'next-page-btn'});
nextBtn.addErrback(testErrback.bind(null, test, 'next button does not exist'));
nextBtn.click();
driver.sleep(2000);
switchToReaderFrame(test);
waitForPresent(test, {css: 'h1.title'}, 'The next page did not appear.');
var title = driver.findElement({css: 'h1.title'});
// getText does not seem to work in the frame
driver.executeScript('return arguments[0].innerText;', title).then(function(text){
test.equal(text, 'Accessible EPUB 3');
});
driver.sleep(2000);
switchToDefaultFrame();
var prevBtn = driver.findElement({id: 'previous-page-btn'});
prevBtn.addErrback(testErrback.bind(null, test, 'prev button does not exist'));
prevBtn.click();
switchToReaderFrame(test);
waitForPresent(test, {id: 'cover-image'}, 'The previous page did not appear').then(function(){
test.done();
});
},
testTableOfContents : function(test){
var tocBtn = driver.findElement({className: 'icon-toc'});
tocBtn.addErrback(testErrback.bind(null, test, 'toc button does not exist'));
tocBtn.click();
var tocBody = driver.findElement({id: 'readium-toc-body'});
tocBody.addErrback(testErrback.bind(null, test, 'toc body does not exist'));
waitForVisible(test, tocBody, 'table of contents did not appear after clicking on the button');
// just test the preface and the last link
var firstLink = driver.findElement({css: 'a[href="pr01.xhtml"]'});
firstLink.addErrback(testErrback.bind(null, test, 'the first toc link did not exist'));
firstLink.getText().then(function(text){
test.equal(text, 'Preface');
});
firstLink.click();
switchToReaderFrame(test);
waitForPresent(test, {css: '#_preface > h2.title'}, 'The preface title header did not appear');
var prefaceTitle = driver.findElement({css: '#_preface > h2.title'});
driver.executeScript('return arguments[0].innerText;', prefaceTitle).then(function(text){
test.equal(text, 'Preface');
});
switchToDefaultFrame();
driver.executeScript('$("#readium-toc-body").scrollTop(10000)');
var lastLink = driver.findElement({css: 'a[href="ch04.xhtml#_about_the_book"]'});
lastLink.addErrback(testErrback.bind(null, test, 'the last toc link did not exist'));
lastLink.isDisplayed().then(function(displayed){
test.ok(displayed, 'The last link in the toc is not displayed after scrolling to the bottom');
});
lastLink.click();
switchToReaderFrame(test);
waitForPresent(test, {id: '_about_the_book'}, 'The last section title never appeared');
var title = driver.findElement({id: '_about_the_book'});
driver.executeScript('return arguments[0].innerText;', title).then(function(text){
test.equal(text, 'About the Book');
test.done();
});
},
tearDown : exports.tearDown
}
var loadSettingsDialog = function(test){
var settingsBtn = driver.findElement({className: 'icon-settings'});
settingsBtn.addErrback(testErrback.bind(null, test, 'settings button does not exist'));
settingsBtn.click();
var settingsDlg = driver.findElement({id: 'settings-dialog'})
settingsDlg.addErrback(testErrback.bind(null, test, 'settings dialog does not exist'));
waitForVisible(test, settingsDlg, 'settings dialog did not appear');
}
var saveSettings = function(test){
var saveBtn = findElement(test, {css: '#settings-dialog .btn-primary'}, 'save settings button not present');
saveBtn.click();
}
exports.settingsTests = {
setUp : readerSetUp,
testFontSize : function(test){
loadSettingsDialog(test);
var fontSlider = driver.findElement({id: 'font-size-input'});
fontSlider.isDisplayed().then(function(displayed){
test.ok(displayed, 'font slider is not visible');
});
driver.executeScript('return $(arguments[0]).val()', fontSlider).then(function(val){
test.equal(val, 100, "Font size default value is wrong")
});
driver.executeScript('$(arguments[0]).val(160); $(arguments[0]).change();', fontSlider);
var previewText = findElement(test, {className: 'preview-text'}, 'preview text does not exist');
driver.executeScript('return arguments[0].style.fontSize;',previewText).then(function(fontSize){
test.equal(fontSize, '1.6em');
});
saveSettings(test);
driver.sleep(2000);
switchToReaderFrame(test);
var htmlElement = findElement(test, {css: 'html'}, 'No content in reader frame');
driver.executeScript('return arguments[0].style.fontSize;', htmlElement).then(function(fontSize){
test.equal(fontSize, '160%');
test.done();
});
},
testLayoutOptions : function(test){
loadSettingsDialog(test);
var twoPageOption = findElement(test, {css: '#two-up-option input'}, 'two page option not present');
var onePageOption = findElement(test, {css: '#one-up-option input'}, 'one page option not present');
var checkChecked = function(ele, expect, msg){
driver.executeScript('return $(arguments[0]).prop("checked");', ele).then(function(checked){
test.equal(checked, expect, msg);
});
}
checkChecked(twoPageOption, true, "The two page options should be selected by default");
checkChecked(onePageOption, false, "The one page option should be unselected by default");
var selectRadio = function(ele){
driver.executeScript('$(arguments[0]).prop("checked", true);', ele);
}
selectRadio(onePageOption);
saveSettings(test);
driver.sleep(2000);
var iframeWidth;
driver.executeScript('return $("iframe").css("width");').then(function(width){
iframeWidth = width;
});
switchToReaderFrame(test);
var htmlElement = findElement(test, {css: 'html'}, 'No content in reader frame');
driver.executeScript('return arguments[0].style.width;', htmlElement).then(function(width){
test.equal(width, iframeWidth, 'Did not switch to one column');
});
switchToDefaultFrame();
loadSettingsDialog(test);
selectRadio(twoPageOption);
driver.sleep(1000);
saveSettings(test);
driver.sleep(2000);
switchToReaderFrame(test);
driver.executeScript('return arguments[0].style.width;', htmlElement).then(function(width){
test.notEqual(width, iframeWidth, 'Did not switch to two column');
test.done();
});
},
testThemes : function(test){
loadSettingsDialog(test);
var previewText = findElement(test, {className: 'preview-text'}, 'preview text does not exist');
driver.findElements({className: 'theme-option'}).then(function(themes){
themes.forEach(function(theme){
var themeColor,
themeBg;
theme.getCssValue('color').then(function(color){
themeColor = color;
});
theme.getCssValue('backgroundColor').then(function(bgcolor){
themeBg = bgcolor;
});
theme.click();
previewText.getCssValue('color').then(function(color){
test.equal(color, themeColor);
});
previewText.getCssValue('backgroundColor').then(function(bgcolor){
test.equal(bgcolor, themeBg);
});
});
});
var nightTheme = findElement(test, {css: '#theme-radio-group .night-theme'}, 'Cannot find night theme button');
nightTheme.click();
var nightThemeColor,
nightThemeBg;
nightTheme.getCssValue('color').then(function(color){
nightThemeColor = color;
});
nightTheme.getCssValue('backgroundColor').then(function(bgcolor){
nightThemeBg = bgcolor;
});
driver.sleep(1000);
saveSettings(test);
switchToReaderFrame(test);
var body = findElement(test, {css: 'body'}, 'Cannot find reader body');
body.getCssValue('color').then(function(color){
test.equal(color, nightThemeColor);
});
body.getCssValue('backgroundColor').then(function(bg){
test.equal(bg, nightThemeBg);
test.done();
});
},
testMargins : function(test){
loadSettingsDialog(test);
var marginSlider = driver.findElement({id: 'margin-size-input'});
marginSlider.isDisplayed().then(function(displayed){
test.ok(displayed, 'margin slider is not visible');
});
driver.executeScript('return $(arguments[0]).val()', marginSlider).then(function(val){
test.equal(val, 20, "Font size default value is wrong")
});
driver.executeScript('$(arguments[0]).val(80);', marginSlider);
saveSettings(test);
driver.sleep(2000);
switchToReaderFrame(test);
var htmlElement = findElement(test, {css: 'html'}, 'No content in reader frame');
driver.executeScript('return arguments[0].style.webkitColumnGap;', htmlElement).then(function(gap){
test.equal(gap, '80px');
});
switchToDefaultFrame();
var container = findElement(test, {id: "epub-reader-container"}, 'Cannot locate reader container');
container.getCssValue('margin').then(function(margin){
test.equal(margin, '80px');
test.done();
})
}
}