@@ -62,6 +62,20 @@ var buildElementHelper = function(ptor, opt_usingChain) {
62
62
return base ;
63
63
}
64
64
65
+ /**
66
+ * The element function returns an Element Finder. Element Finders do
67
+ * not actually attempt to find the element until a method is called on them,
68
+ * which means they can be set up in helper files before the page is
69
+ * available.
70
+ *
71
+ * Example:
72
+ * var nameInput = element(by.model('name'));
73
+ * browser.get('myurl');
74
+ * nameInput.sendKeys('Jane Doe');
75
+ *
76
+ * @param {webdriver.Locator } locator
77
+ * @return {ElementFinder }
78
+ */
65
79
var element = function ( locator ) {
66
80
var elementFinder = { } ;
67
81
@@ -85,17 +99,46 @@ var buildElementHelper = function(ptor, opt_usingChain) {
85
99
return using ( ) . findElement ( locator ) . findElement ( subLocator ) ;
86
100
} ;
87
101
102
+ /**
103
+ * Return the actual WebElement.
104
+ *
105
+ * @return {webdriver.WebElement }
106
+ */
88
107
elementFinder . find = function ( ) {
89
108
return using ( ) . findElement ( locator ) ;
90
109
} ;
91
110
111
+ /**
112
+ * @return {boolean } whether the element is present on the page.
113
+ */
92
114
elementFinder . isPresent = function ( ) {
93
115
return using ( ) . isElementPresent ( locator ) ;
94
116
} ;
95
117
118
+ /**
119
+ * Calls to element may be chained to find elements within a parent.
120
+ * Example:
121
+ * var name = element(by.id('container')).element(by.model('name'));
122
+ * browser.get('myurl');
123
+ * name.sendKeys('John Smith');
124
+ *
125
+ * @param {Protractor } ptor
126
+ * @param {=Array.<webdriver.Locator> } opt_usingChain
127
+ * @return {function(webdriver.Locator): ElementFinder }
128
+ */
96
129
elementFinder . element =
97
130
buildElementHelper ( ptor , usingChain . concat ( locator ) ) ;
98
131
132
+ /**
133
+ * Shortcut for chaining css element finders.
134
+ * Example:
135
+ * var name = element(by.id('container')).$('input.myclass');
136
+ * browser.get('myurl');
137
+ * name.sendKeys('John Smith');
138
+ *
139
+ * @param {string } cssSelector
140
+ * @return {ElementFinder }
141
+ */
99
142
elementFinder . $ = function ( cssSelector ) {
100
143
return buildElementHelper ( ptor , usingChain . concat ( locator ) ) (
101
144
webdriver . By . css ( cssSelector ) ) ;
@@ -105,24 +148,43 @@ var buildElementHelper = function(ptor, opt_usingChain) {
105
148
} ;
106
149
107
150
/**
108
- * @type {function(webdriver.Locator): ElementArrayFinder }
151
+ * element.all is used for operations on an array of elements (as opposed
152
+ * to a single element).
153
+ *
154
+ * Example:
155
+ * var lis = element.all(by.css('li'));
156
+ * browser.get('myurl');
157
+ * expect(lis.count()).toEqual(4);
158
+ *
159
+ * @param {webdriver.Locator } locator
160
+ * @return {ElementArrayFinder }
109
161
*/
110
162
element . all = function ( locator ) {
111
163
var elementArrayFinder = { } ;
112
164
165
+ /**
166
+ * @return {number } the number of elements matching the locator.
167
+ */
113
168
elementArrayFinder . count = function ( ) {
114
169
return using ( ) . findElements ( locator ) . then ( function ( arr ) {
115
170
return arr . length ;
116
171
} ) ;
117
172
} ;
118
173
174
+ /**
175
+ * @param {number } index
176
+ * @return {webdriver.WebElement } the element at the given index
177
+ */
119
178
elementArrayFinder . get = function ( index ) {
120
179
var id = using ( ) . findElements ( locator ) . then ( function ( arr ) {
121
180
return arr [ index ] ;
122
181
} ) ;
123
182
return ptor . wrapWebElement ( new webdriver . WebElement ( ptor . driver , id ) ) ;
124
183
} ;
125
184
185
+ /**
186
+ * @return {webdriver.WebElement } the first matching element
187
+ */
126
188
elementArrayFinder . first = function ( ) {
127
189
var id = using ( ) . findElements ( locator ) . then ( function ( arr ) {
128
190
if ( ! arr . length ) {
@@ -133,17 +195,29 @@ var buildElementHelper = function(ptor, opt_usingChain) {
133
195
return ptor . wrapWebElement ( new webdriver . WebElement ( ptor . driver , id ) ) ;
134
196
} ;
135
197
198
+ /**
199
+ * @return {webdriver.WebElement } the last matching element
200
+ */
136
201
elementArrayFinder . last = function ( ) {
137
202
var id = using ( ) . findElements ( locator ) . then ( function ( arr ) {
138
203
return arr [ arr . length - 1 ] ;
139
204
} ) ;
140
205
return ptor . wrapWebElement ( new webdriver . WebElement ( ptor . driver , id ) ) ;
141
206
} ;
142
207
208
+ /**
209
+ * @type {webdriver.promise.Promise } a promise which will resolve to
210
+ * an array of WebElements matching the locator.
211
+ */
143
212
elementArrayFinder . then = function ( fn ) {
144
213
return using ( ) . findElements ( locator ) . then ( fn ) ;
145
214
} ;
146
215
216
+ /**
217
+ * Calls the input function on each WebElement found by the locator.
218
+ *
219
+ * @param {function(webdriver.WebElement) }
220
+ */
147
221
elementArrayFinder . each = function ( fn ) {
148
222
using ( ) . findElements ( locator ) . then ( function ( arr ) {
149
223
arr . forEach ( function ( webElem ) {
0 commit comments