@@ -7,6 +7,11 @@ var ProtractorBy = require('./locators.js').ProtractorBy;
7
7
8
8
var DEFER_LABEL = 'NG_DEFER_BOOTSTRAP!' ;
9
9
10
+ var WEB_ELEMENT_FUNCTIONS = [
11
+ 'click' , 'sendKeys' , 'getTagName' , 'getCssValue' , 'getAttribute' , 'getText' ,
12
+ 'getSize' , 'getLocation' , 'isEnabled' , 'isSelected' , 'submit' , 'clear' ,
13
+ 'isDisplayed' , 'getOuterHtml' , 'getInnerHtml' ] ;
14
+
10
15
/**
11
16
* Mix in other webdriver functionality to be accessible via protractor.
12
17
*/
@@ -17,6 +22,8 @@ for (foo in webdriver) {
17
22
/**
18
23
* Mix a function from one object onto another. The function will still be
19
24
* called in the context of the original object.
25
+ *
26
+ * @private
20
27
* @param {Object } to
21
28
* @param {Object } from
22
29
* @param {string } fnName
@@ -28,7 +35,93 @@ var mixin = function(to, from, fnName, setupFn) {
28
35
setupFn ( ) ;
29
36
}
30
37
return from [ fnName ] . apply ( from , arguments ) ;
38
+ } ;
39
+ } ;
40
+
41
+ /**
42
+ * Build the helper 'element' function for a given instance of Protractor.
43
+ *
44
+ * @private
45
+ * @param {Protractor } ptor
46
+ * @return {function(webdriver.Locator): ElementFinder }
47
+ */
48
+ var buildElementHelper = function ( ptor ) {
49
+ var element = function ( locator ) {
50
+ var elementFinder = { } ;
51
+ var webElementFns = WEB_ELEMENT_FUNCTIONS . concat (
52
+ [ 'findElement' , 'findElements' , 'isElementPresent' ,
53
+ 'evaluate' , '$' , '$$' ] ) ;
54
+ webElementFns . forEach ( function ( fnName ) {
55
+ elementFinder [ fnName ] = function ( ) {
56
+ var elem = ptor . findElement ( locator ) ;
57
+ return elem [ fnName ] . apply ( elem , arguments ) ;
58
+ } ;
59
+ } ) ;
60
+
61
+ elementFinder . find = function ( ) {
62
+ return ptor . findElement ( locator ) ;
63
+ } ;
64
+
65
+ elementFinder . isPresent = function ( ) {
66
+ return ptor . isElementPresent ( locator ) ;
67
+ } ;
68
+
69
+ return elementFinder ;
70
+ } ;
71
+
72
+ /**
73
+ * @type {function(webdriver.Locator): ElementArrayFinder }
74
+ */
75
+ element . all = function ( locator ) {
76
+ var elementArrayFinder = { } ;
77
+
78
+ elementArrayFinder . count = function ( ) {
79
+ return ptor . findElements ( locator ) . then ( function ( arr ) {
80
+ return arr . length ;
81
+ } ) ;
82
+ } ;
83
+
84
+ elementArrayFinder . get = function ( index ) {
85
+ var id = ptor . findElements ( locator ) . then ( function ( arr ) {
86
+ return arr [ index ] ;
87
+ } ) ;
88
+ return new webdriver . WebElement ( ptor . driver , id ) ;
89
+ } ;
90
+
91
+ elementArrayFinder . then = function ( ) {
92
+ return ptor . findElements ( locator ) ;
93
+ } ;
94
+
95
+ return elementArrayFinder ;
31
96
}
97
+
98
+ return element ;
99
+ } ;
100
+
101
+ /**
102
+ * Build the helper '$' function for a given instance of Protractor.
103
+ *
104
+ * @private
105
+ * @param {Protractor } ptor
106
+ * @return {function(string): ElementFinder }
107
+ */
108
+ var buildCssHelper = function ( ptor ) {
109
+ return function ( cssSelector ) {
110
+ return buildElementHelper ( ptor ) ( webdriver . By . css ( cssSelector ) ) ;
111
+ } ;
112
+ } ;
113
+
114
+ /**
115
+ * Build the helper '$$' function for a given instance of Protractor.
116
+ *
117
+ * @private
118
+ * @param {Protractor } ptor
119
+ * @return {function(string): ElementArrayFinder }
120
+ */
121
+ var buildMultiCssHelper = function ( ptor ) {
122
+ return function ( cssSelector ) {
123
+ return buildElementHelper ( ptor ) . all ( webdriver . By . css ( cssSelector ) ) ;
124
+ } ;
32
125
} ;
33
126
34
127
/**
@@ -63,6 +156,27 @@ var Protractor = function(webdriver, opt_baseUrl, opt_rootElement) {
63
156
*/
64
157
this . driver = webdriver ;
65
158
159
+ /**
160
+ * Helper function for finding elements.
161
+ *
162
+ * @type {function(webdriver.Locator): ElementFinder }
163
+ */
164
+ this . element = buildElementHelper ( this ) ;
165
+
166
+ /**
167
+ * Helper function for finding elements by css.
168
+ *
169
+ * @type {function(string): ElementFinder }
170
+ */
171
+ this . $ = buildCssHelper ( this ) ;
172
+
173
+ /**
174
+ * Helper function for finding arrays of elements by css.
175
+ *
176
+ * @type {function(string): ElementArrayFinder }
177
+ */
178
+ this . $$ = buildMultiCssHelper ( this ) ;
179
+
66
180
/**
67
181
* All get methods will be resolved against this base URL. Relative URLs are =
68
182
* resolved the way anchor tags resolve.
@@ -134,14 +248,10 @@ Protractor.prototype.waitForAngular = function() {
134
248
*/
135
249
Protractor . prototype . wrapWebElement = function ( element ) {
136
250
var thisPtor = this ;
137
- // Before any of these WebElement functions, Protractor will wait to make sure
251
+ // Before any of the WebElement functions, Protractor will wait to make sure
138
252
// Angular is synched up.
139
- var functionsToSync = [
140
- 'click' , 'sendKeys' , 'getTagName' , 'getCssValue' , 'getAttribute' , 'getText' ,
141
- 'getSize' , 'getLocation' , 'isEnabled' , 'isSelected' , 'submit' , 'clear' ,
142
- 'isDisplayed' , 'getOuterHtml' , 'getInnerHtml' ] ;
143
253
var originalFns = { } ;
144
- functionsToSync . forEach ( function ( name ) {
254
+ WEB_ELEMENT_FUNCTIONS . forEach ( function ( name ) {
145
255
originalFns [ name ] = element [ name ] ;
146
256
element [ name ] = function ( ) {
147
257
thisPtor . waitForAngular ( ) ;
@@ -254,18 +364,6 @@ Protractor.prototype.wrapWebElement = function(element) {
254
364
return element ;
255
365
} ;
256
366
257
- /**
258
- * Shortcut for querying the document directly with css.
259
- *
260
- * @param {string } selector a css selector
261
- * @see webdriver.WebDriver.findElement
262
- * @return {!webdriver.WebElement }
263
- */
264
- Protractor . prototype . $ = function ( selector ) {
265
- var locator = protractor . By . css ( selector ) ;
266
- return this . findElement ( locator ) ;
267
- } ;
268
-
269
367
/**
270
368
* Waits for Angular to finish rendering before searching for elements.
271
369
* @see webdriver.WebDriver.findElement
@@ -284,19 +382,6 @@ Protractor.prototype.findElement = function(locator, varArgs) {
284
382
return this . wrapWebElement ( found ) ;
285
383
} ;
286
384
287
- /**
288
- * Shortcut for querying the document directly with css.
289
- *
290
- * @param {string } selector a css selector
291
- * @see webdriver.WebDriver.findElements
292
- * @return {!webdriver.promise.Promise } A promise that will be resolved to an
293
- * array of the located {@link webdriver.WebElement}s.
294
- */
295
- Protractor . prototype . $$ = function ( selector ) {
296
- var locator = protractor . By . css ( selector ) ;
297
- return this . findElements ( locator ) ;
298
- } ;
299
-
300
385
/**
301
386
* Waits for Angular to finish rendering before searching for elements.
302
387
* @see webdriver.WebDriver.findElements
0 commit comments