@@ -157,7 +157,7 @@ def page opts={}
157
157
def source_window window_number = 0
158
158
# appium 1.0 still returns JSON when getTree() is invoked so this
159
159
# doesn't need to change to XML. If getTree() is removed then
160
- # source_window will need to parse the results of getTreeForXML()\
160
+ # source_window will need to parse the elements of getTreeForXML()\
161
161
# https://github.com/appium/appium-uiauto/blob/247eb71383fa1a087ff8f8fc96fac25025731f3f/uiauto/appium/element.js#L145
162
162
execute_script "UIATarget.localTarget().frontMostApp().windows()[#{ window_number } ].getTree()"
163
163
end
@@ -192,11 +192,19 @@ def ios_version
192
192
# @param index [Integer] the index
193
193
# @return [Element]
194
194
def ele_index class_name , index
195
- unless index == 'last()'
196
- # XPath index starts at 1.
197
- raise "#{ index } is not a valid xpath index. Must be >= 1" if index <= 0
195
+ raise 'Index must be >= 1' unless index == 'last()' || ( index . is_a? ( Integer ) && index >= 1 )
196
+ elements = tags ( class_name )
197
+
198
+ if index == 'last()'
199
+ result = elements . last
200
+ else
201
+ # elements array is 0 indexed
202
+ index -= 1
203
+ result = elements [ index ]
198
204
end
199
- find_element :xpath , %Q(//#{ class_name } [@visible="true"][#{ index } ])
205
+
206
+ raise _no_such_element if result . nil?
207
+ result
200
208
end
201
209
202
210
# @private
@@ -205,6 +213,7 @@ def string_attr_exact class_name, attr, value
205
213
end
206
214
207
215
# Find the first element exactly matching class and attribute value.
216
+ # Note: Uses XPath
208
217
# @param class_name [String] the class name to search for
209
218
# @param attr [String] the attribute to inspect
210
219
# @param value [String] the expected value of the attribute
@@ -214,6 +223,7 @@ def find_ele_by_attr class_name, attr, value
214
223
end
215
224
216
225
# Find all elements exactly matching class and attribute value.
226
+ # Note: Uses XPath
217
227
# @param class_name [String] the class name to match
218
228
# @param attr [String] the attribute to compare
219
229
# @param value [String] the value of the attribute that the element must have
@@ -228,6 +238,7 @@ def string_attr_include class_name, attr, value
228
238
end
229
239
230
240
# Get the first tag by attribute that exactly matches value.
241
+ # Note: Uses XPath
231
242
# @param class_name [String] the tag name to match
232
243
# @param attr [String] the attribute to compare
233
244
# @param value [String] the value of the attribute that the element must include
@@ -237,6 +248,7 @@ def find_ele_by_attr_include class_name, attr, value
237
248
end
238
249
239
250
# Get tags by attribute that include value.
251
+ # Note: Uses XPath
240
252
# @param class_name [String] the tag name to match
241
253
# @param attr [String] the attribute to compare
242
254
# @param value [String] the value of the attribute that the element must include
@@ -260,24 +272,30 @@ def last_ele class_name
260
272
ele_index class_name , 'last()'
261
273
end
262
274
263
- # Returns the first element matching class_name
275
+ # Returns the first visible element matching class_name
264
276
#
265
277
# @param class_name [String] the class_name to search for
266
278
# @return [Element]
267
279
def tag class_name
268
- xpath %Q(//#{ class_name } [@visible="true"])
280
+ ele_by_json ( {
281
+ typeArray : [ class_name ] ,
282
+ onlyVisible : true ,
283
+ } )
269
284
end
270
285
271
- # Returns all elements matching class_name
286
+ # Returns all visible elements matching class_name
272
287
#
273
288
# @param class_name [String] the class_name to search for
274
289
# @return [Element]
275
290
def tags class_name
276
- xpaths %Q(//#{ class_name } [@visible="true"])
291
+ eles_by_json ( {
292
+ typeArray : [ class_name ] ,
293
+ onlyVisible : true ,
294
+ } )
277
295
end
278
296
279
297
# @private
280
- # Returns a string xpath that matches the first element that contains value
298
+ # Returns an object that matches the first element that contains value
281
299
#
282
300
# example: ele_by_json_visible_contains 'UIATextField', 'sign in'
283
301
#
@@ -317,7 +335,7 @@ def eles_by_json_visible_contains element, value
317
335
end
318
336
319
337
# @private
320
- # Create an xpath string to exactly match the first element with target value
338
+ # Create an object to exactly match the first element with target value
321
339
# @param element [String] the class name for the element
322
340
# @param value [String] the value to search for
323
341
# @return [String]
0 commit comments