forked from tastejs/todomvc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
402 lines (319 loc) · 12 KB
/
test.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
'use strict';
var webdriver = require('selenium-webdriver');
var chrome = require('selenium-webdriver/chrome');
var test = require('selenium-webdriver/testing');
var Page = require('./page');
var PageLaxMode = require('./pageLaxMode');
var TestOperations = require('./testOperations');
module.exports.todoMVCTest = function (frameworkName, baseUrl, speedMode, laxMode, browserName) {
test.describe('TodoMVC - ' + frameworkName, function () {
var TODO_ITEM_ONE = 'buy some cheese';
var TODO_ITEM_TWO = 'feed the cat';
var TODO_ITEM_THREE = 'book a doctors appointment';
var browser, testOps, page;
// a number of tests use this set of ToDo items.
function createStandardItems(done) {
page.enterItem(TODO_ITEM_ONE);
page.enterItem(TODO_ITEM_TWO);
return page.enterItem(TODO_ITEM_THREE)
.then(function () {
if (done instanceof Function) {
done();
}
});
}
function launchBrowser(done) {
var chromeOptions = new chrome.Options();
chromeOptions.addArguments('no-sandbox');
if (process.env.CHROME_PATH !== undefined) {
chromeOptions.setChromeBinaryPath(process.env.CHROME_PATH);
}
browser = new webdriver.Builder()
.withCapabilities({browserName: browserName})
.setChromeOptions(chromeOptions)
.build();
browser.get(baseUrl);
page = laxMode ? new PageLaxMode(browser) : new Page(browser);
testOps = new TestOperations(page);
return page.ensureAppIsVisibleAndLoaded()
.then(function () {
if (done instanceof Function) {
done();
}
});
}
function printCapturedLogs() {
var logs = browser.manage().logs();
return logs.get('browser')
.then(function (entries) {
if (entries && entries.length) {
console.log(entries);
}
});
}
function closeBrowser(done) {
return browser
.quit()
.then(function () {
if (done instanceof Function) { done(); }
});
}
if (speedMode) {
test.before(launchBrowser);
test.after(closeBrowser);
test.afterEach(function (done) {
return browser.executeScript('window.localStorage && localStorage.clear(); location.reload(true);')
.then(function () { done(); });
});
} else {
test.beforeEach(launchBrowser);
test.afterEach(function (done) {
printCapturedLogs()
.then(function () {
return closeBrowser(done);
});
});
}
test.describe('When page is initially opened', function () {
test.it('should focus on the todo input field', function (done) {
testOps.assertNewInputFocused()
.then(function () { done(); });
});
});
test.describe('No Todos', function () {
test.it('should hide #main and #footer', function (done) {
testOps.assertItemCount(0);
testOps.assertMainSectionVisibility(false);
testOps.assertFooterVisibility(false)
.then(function () { done(); });
});
});
test.describe('New Todo', function () {
test.it('should allow me to add todo items', function (done) {
page.enterItem(TODO_ITEM_ONE);
testOps.assertItems([TODO_ITEM_ONE]);
page.enterItem(TODO_ITEM_TWO);
testOps.assertItems([TODO_ITEM_ONE, TODO_ITEM_TWO])
.then(function () { done(); });
});
test.it('should clear text input field when an item is added', function (done) {
page.enterItem(TODO_ITEM_ONE);
testOps.assertNewItemInputFieldText('')
.then(function () { done(); });
});
test.it('should append new items to the bottom of the list', function (done) {
createStandardItems();
testOps.assertItemCount(3);
testOps.assertItemText(0, TODO_ITEM_ONE);
testOps.assertItemText(1, TODO_ITEM_TWO);
testOps.assertItemText(2, TODO_ITEM_THREE)
.then(function () { done(); });
});
test.it('should trim text input', function (done) {
page.enterItem(' ' + TODO_ITEM_ONE + ' ');
testOps.assertItemText(0, TODO_ITEM_ONE)
.then(function () { done(); });
});
test.it('should show #main and #footer when items added', function (done) {
page.enterItem(TODO_ITEM_ONE);
testOps.assertMainSectionVisibility(true);
testOps.assertFooterVisibility(true)
.then(function () { done(); });
});
});
test.describe('Mark all as completed', function () {
test.beforeEach(createStandardItems);
test.it('should allow me to mark all items as completed', function (done) {
page.clickMarkAllCompletedCheckBox();
testOps.assertItemCompletedStates([true, true, true])
.then(function () { done(); });
});
test.it('should correctly update the complete all checked state', function (done) {
// manually check all items
page.toggleItemAtIndex(0);
page.toggleItemAtIndex(1);
page.toggleItemAtIndex(2);
// ensure checkall is in the correct state
testOps.assertCompleteAllCheckedStatus(true)
.then(function () { done(); });
});
test.it('should allow me to clear the completion state of all items', function (done) {
page.clickMarkAllCompletedCheckBox();
page.clickMarkAllCompletedCheckBox();
testOps.assertItemCompletedStates([false, false, false])
.then(function () { done(); });
});
test.it('complete all checkbox should update state when items are completed / cleared', function (done) {
page.clickMarkAllCompletedCheckBox();
testOps.assertCompleteAllCheckedStatus(true);
// all items are complete, now mark one as not-complete
page.toggleItemAtIndex(0);
testOps.assertCompleteAllCheckedStatus(false);
// now mark as complete, so that once again all items are completed
page.toggleItemAtIndex(0);
testOps.assertCompleteAllCheckedStatus(true)
.then(function () { done(); });
});
});
test.describe('Item', function () {
test.it('should allow me to mark items as complete', function (done) {
page.enterItem(TODO_ITEM_ONE);
page.enterItem(TODO_ITEM_TWO);
page.toggleItemAtIndex(0);
testOps.assertItemCompletedStates([true, false]);
page.toggleItemAtIndex(1);
testOps.assertItemCompletedStates([true, true])
.then(function () { done(); });
});
test.it('should allow me to un-mark items as complete', function (done) {
page.enterItem(TODO_ITEM_ONE);
page.enterItem(TODO_ITEM_TWO);
page.toggleItemAtIndex(0);
testOps.assertItemCompletedStates([true, false]);
page.toggleItemAtIndex(0);
testOps.assertItemCompletedStates([false, false])
.then(function () { done(); });
});
});
test.describe('Editing', function () {
test.beforeEach(function (done) {
createStandardItems();
page.doubleClickItemAtIndex(1)
.then(function () { done(); });
});
test.it('should focus the input', function (done) {
testOps.assertItemInputFocused();
testOps.assertNewInputBlurred() // Unnecessary? The HTML spec dictates that only one element can be focused.
.then(function () { done(); });
});
test.it('should hide other controls when editing', function (done) {
testOps.assertItemToggleIsHidden(1);
testOps.assertItemLabelIsHidden(1)
.then(function () { done(); });
});
test.it('should save edits on enter', function (done) {
page.editItemAtIndex(1, 'buy some sausages' + webdriver.Key.ENTER);
testOps.assertItems([TODO_ITEM_ONE, 'buy some sausages', TODO_ITEM_THREE])
.then(function () { done(); });
});
test.it('should save edits on blur', function (done) {
page.editItemAtIndex(1, 'buy some sausages');
// click a toggle button so that the blur() event is fired
page.toggleItemAtIndex(0);
testOps.assertItems([TODO_ITEM_ONE, 'buy some sausages', TODO_ITEM_THREE])
.then(function () { done(); });
});
test.it('should trim entered text', function (done) {
page.editItemAtIndex(1, ' buy some sausages ' + webdriver.Key.ENTER);
testOps.assertItems([TODO_ITEM_ONE, 'buy some sausages', TODO_ITEM_THREE])
.then(function () { done(); });
});
test.it('should remove the item if an empty text string was entered', function (done) {
page.editItemAtIndex(1, webdriver.Key.ENTER);
testOps.assertItems([TODO_ITEM_ONE, TODO_ITEM_THREE])
.then(function () { done(); });
});
test.it('should cancel edits on escape', function (done) {
page.editItemAtIndex(1, 'foo' + webdriver.Key.ESCAPE);
testOps.assertItems([TODO_ITEM_ONE, TODO_ITEM_TWO, TODO_ITEM_THREE])
.then(function () { done(); });
});
});
test.describe('Counter', function () {
test.it('should display the current number of todo items', function (done) {
page.enterItem(TODO_ITEM_ONE);
testOps.assertItemCountText('1 item left');
page.enterItem(TODO_ITEM_TWO);
testOps.assertItemCountText('2 items left')
.then(function () { done(); });
});
});
test.describe('Clear completed button', function () {
test.beforeEach(createStandardItems);
test.it('should display the correct text', function (done) {
page.toggleItemAtIndex(1);
testOps.assertClearCompleteButtonText('Clear completed')
.then(function () { done(); });
});
test.it('should remove completed items when clicked', function (done) {
page.toggleItemAtIndex(1);
page.clickClearCompleteButton();
testOps.assertItemCount(2);
testOps.assertItems([TODO_ITEM_ONE, TODO_ITEM_THREE])
.then(function () { done(); });
});
test.it('should be hidden when there are no items that are completed', function (done) {
page.toggleItemAtIndex(1);
testOps.assertClearCompleteButtonVisibility(true);
page.clickClearCompleteButton();
testOps.assertClearCompleteButtonVisibility(false)
.then(function () { done(); });
});
});
test.describe('Persistence', function () {
test.it('should persist its data', function (done) {
function stateTest() {
testOps.assertItemCount(2);
testOps.assertItems([TODO_ITEM_ONE, TODO_ITEM_TWO]);
return testOps.assertItemCompletedStates([false, true]);
}
// set up state
page.enterItem(TODO_ITEM_ONE);
page.enterItem(TODO_ITEM_TWO);
page.toggleItemAtIndex(1);
stateTest();
// navigate away and back again
browser.get('about:blank');
browser.get(baseUrl);
// repeat the state test
stateTest()
.then(function () { done(); });
});
});
test.describe('Routing', function () {
test.beforeEach(createStandardItems);
test.it('should allow me to display active items', function (done) {
page.toggleItemAtIndex(1);
page.filterByActiveItems();
testOps.assertItems([TODO_ITEM_ONE, page.ITEM_HIDDEN_OR_REMOVED, TODO_ITEM_THREE])
.then(function () { return done(); });
});
test.it('should respect the back button', function (done) {
page.toggleItemAtIndex(1);
page.filterByActiveItems();
page.filterByCompletedItems();
testOps.assertItems([page.ITEM_HIDDEN_OR_REMOVED, TODO_ITEM_TWO]); // should show completed items
page.back(); // then active items
testOps.assertItems([TODO_ITEM_ONE, page.ITEM_HIDDEN_OR_REMOVED, TODO_ITEM_THREE]);
page.back(); // then all items
testOps.assertItems([TODO_ITEM_ONE, TODO_ITEM_TWO, TODO_ITEM_THREE])
.then(function () { done(); });
});
test.it('should allow me to display completed items', function (done) {
page.toggleItemAtIndex(1);
page.filterByCompletedItems();
testOps.assertItems([page.ITEM_HIDDEN_OR_REMOVED, TODO_ITEM_TWO]);
page.filterByAllItems() // TODO: why
.then(function () { done(); });
});
test.it('should allow me to display all items', function (done) {
page.toggleItemAtIndex(1);
// apply the other filters first, before returning to the 'all' state
page.filterByActiveItems();
page.filterByCompletedItems();
page.filterByAllItems();
testOps.assertItems([TODO_ITEM_ONE, TODO_ITEM_TWO, TODO_ITEM_THREE])
.then(function () { done(); });
});
test.it('should highlight the currently applied filter', function (done) {
// initially 'all' should be selected
testOps.assertFilterAtIndexIsSelected(0);
page.filterByActiveItems();
testOps.assertFilterAtIndexIsSelected(1);
page.filterByCompletedItems();
testOps.assertFilterAtIndexIsSelected(2)
.then(function () { done(); });
});
});
});
};