@@ -22,19 +22,25 @@ function Element(definition, options) {
22
22
23
23
this . selector = definition . selector ;
24
24
this . locateStrategy = definition . locateStrategy || options . locateStrategy ;
25
+ this . index = definition . index ;
25
26
this . parent = options . parent ;
26
27
}
27
28
28
29
Element . prototype . toString = function ( ) {
29
30
if ( Array . isArray ( this . selector ) ) { // recursive
30
31
return this . selector . join ( ',' ) ;
31
32
}
33
+
34
+ var index = parseInt ( this . index , 10 ) ;
35
+ var indexStr = isNaN ( index ) ? '' : '[' + index + ']' ;
36
+
32
37
if ( ! this . name ) { // inline (not defined in page or section)
33
38
return this . selector ;
34
39
}
40
+
35
41
var classType = this . constructor . name ;
36
42
var prefix = this . constructor === Element ? '@' : '' ;
37
- return classType + '[name=' + prefix + this . name + ']' ;
43
+ return classType + '[name=' + prefix + this . name + indexStr + ']' ;
38
44
} ;
39
45
40
46
/**
@@ -78,7 +84,7 @@ Element.prototype.getRecursiveLookupElement = function () {
78
84
* @param {Object } source The object to capture values from.
79
85
*/
80
86
Element . copyDefaults = function ( target , source ) {
81
- var props = [ 'name' , 'parent' , 'selector' , 'locateStrategy' ] ;
87
+ var props = [ 'name' , 'parent' , 'selector' , 'locateStrategy' , 'index' ] ;
82
88
props . forEach ( function ( prop ) {
83
89
if ( target [ prop ] === undefined || target [ prop ] === null ) {
84
90
target [ prop ] = source [ prop ] ;
@@ -119,6 +125,48 @@ Element.fromSelector = function(value, using) {
119
125
return new Element ( definition , options ) ;
120
126
} ;
121
127
128
+ /**
129
+ * Returns true when an elements() request is needed to capture
130
+ * the result of the Element definition. When false, it means the
131
+ * Element targets the first result the selector match meaning an
132
+ * element() (single match only) result can be used.
133
+ *
134
+ * @param {Object } element The Element instance to check to see if
135
+ * it will apply filtering.
136
+ */
137
+ Element . requiresFiltering = function ( element ) {
138
+
139
+ var usingIndex = ! isNaN ( parseInt ( element . index , 10 ) ) ;
140
+ if ( usingIndex ) {
141
+ return true ;
142
+ }
143
+
144
+ return false ;
145
+ } ;
146
+
147
+ /**
148
+ * Filters an elements() results array to a more specific set based
149
+ * on an Element object's definition.
150
+ *
151
+ * @param {Object } element The Element instance to check to see if
152
+ * it will apply filtering.
153
+ * @param {Array } resultElements Array of WebElement JSON objects
154
+ * returned from a call to elements() or elementIdElements().
155
+ * @returns {Array } A filtered version of the elements array or, if
156
+ * the filter failed (no matches found) null.
157
+ */
158
+ Element . applyFiltering = function ( element , resultElements ) {
159
+
160
+ var index = parseInt ( element . index , 10 ) ;
161
+ var usingIndex = ! isNaN ( index ) ;
162
+ if ( usingIndex ) {
163
+ var foundElem = resultElements [ index ] ;
164
+ return foundElem ? [ foundElem ] : null ; // null = not found
165
+ }
166
+
167
+ return resultElements ;
168
+ } ;
169
+
122
170
/**
123
171
* Gets a simple description of the element based on whether or not
124
172
* it is used as an inline selector in a command call or a definition
0 commit comments