forked from tastejs/todomvc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.js
172 lines (134 loc) · 4.94 KB
/
page.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
'use strict';
var webdriver = require('selenium-webdriver');
module.exports = function Page(browser) {
// ----------------- utility methods
this.tryFindByXpath = function (xpath) {
return browser.findElements(webdriver.By.xpath(xpath));
};
this.findByXpath = function (xpath) {
return browser.findElement(webdriver.By.xpath(xpath));
};
this.getTodoListXpath = function () {
return '//ul[@id="todo-list"]';
};
this.xPathForItemAtIndex = function (index) {
// why is XPath the only language silly enough to be 1-indexed?
return this.getTodoListXpath() + '/li[' + (index + 1) + ']';
};
// ----------------- navigation methods
this.back = function() {
return browser.navigate().back();
}
// ----------------- try / get methods
// unfortunately webdriver does not have a decent API for determining if an
// element exists. The standard approach is to obtain an array of elements
// and test that the length is zero. These methods are used to obtain
// elements which *might* be present in the DOM, hence the try/get name.
this.tryGetMainSectionElement = function () {
return this.tryFindByXpath('//section[@id="main"]');
};
this.tryGetFooterElement = function () {
return this.tryFindByXpath('//footer[@id="footer"]');
};
this.tryGetClearCompleteButton = function () {
return this.tryFindByXpath('//button[@id="clear-completed"]');
};
this.tryGetToggleForItemAtIndex = function (index) {
var xpath = this.xPathForItemAtIndex(index) + '//input[contains(@class,"toggle")]';
return this.tryFindByXpath(xpath);
};
this.tryGetItemLabelAtIndex = function (index) {
return this.tryFindByXpath(this.xPathForItemAtIndex(index) + '//label');
};
// ----------------- DOM element access methods
this.getEditInputForItemAtIndex = function (index) {
var xpath = this.xPathForItemAtIndex(index) + '//input[contains(@class,"edit")]';
return this.findByXpath(xpath);
};
this.getItemInputField = function () {
return this.findByXpath('//input[@id="new-todo"]');
};
this.getMarkAllCompletedCheckBox = function () {
return this.findByXpath('//input[@id="toggle-all"]');
};
this.getItemElements = function () {
return this.tryFindByXpath(this.getTodoListXpath() + '/li');
};
this.getNonCompletedItemElements = function () {
return this.tryFindByXpath(this.getTodoListXpath() + '/li[not(contains(@class,"completed"))]');
};
this.getItemsCountElement = function () {
return this.findByXpath('//span[@id="todo-count"]');
};
this.getItemLabelAtIndex = function (index) {
return this.findByXpath(this.xPathForItemAtIndex(index) + '//label');
};
this.getFilterElements = function () {
return this.tryFindByXpath('//ul[@id="filters"]//a');
};
this.getItemLabels = function () {
var xpath = this.getTodoListXpath() + '/li//label';
return this.tryFindByXpath(xpath);
};
// ----------------- page actions
this.clickMarkAllCompletedCheckBox = function () {
return this.getMarkAllCompletedCheckBox().then(function (checkbox) {
checkbox.click();
});
};
this.clickClearCompleteButton = function () {
return this.tryGetClearCompleteButton().then(function (elements) {
var button = elements[0];
button.click();
});
};
this.enterItem = function (itemText) {
var textField = this.getItemInputField();
textField.sendKeys(itemText);
textField.sendKeys(webdriver.Key.ENTER);
};
this.toggleItemAtIndex = function (index) {
return this.tryGetToggleForItemAtIndex(index).then(function (elements) {
var toggleElement = elements[0];
toggleElement.click();
});
};
this.editItemAtIndex = function (index, itemText) {
return this.getEditInputForItemAtIndex(index)
.then(function (itemEditField) {
// send 50 delete keypresses, just to be sure the item text is deleted
var deleteKeyPresses = '';
for (var i = 0; i < 50; i++) {
deleteKeyPresses += webdriver.Key.BACK_SPACE;
}
itemEditField.sendKeys(deleteKeyPresses);
// update the item with the new text.
itemEditField.sendKeys(itemText);
});
};
this.doubleClickItemAtIndex = function (index) {
return this.getItemLabelAtIndex(index).then(function (itemLabel) {
// double click is not 'natively' supported, so we need to send the
// event direct to the element see:
// http://stackoverflow.com/questions/3982442/selenium-2-webdriver-how-to-double-click-a-table-row-which-opens-a-new-window
browser.executeScript('var evt = document.createEvent("MouseEvents");' +
'evt.initMouseEvent("dblclick",true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0,null);' +
'arguments[0].dispatchEvent(evt);', itemLabel);
});
};
this.filterByActiveItems = function () {
return this.getFilterElements().then(function (filters) {
filters[1].click();
});
};
this.filterByCompletedItems = function () {
return this.getFilterElements().then(function (filters) {
filters[2].click();
});
};
this.filterByAllItems = function () {
return this.getFilterElements().then(function (filters) {
filters[0].click();
});
};
};