Skip to content

Commit 252df47

Browse files
Reimplement ele_index / tag to avoid xpath
1 parent c5cf51d commit 252df47

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

lib/appium_lib/ios/helper.rb

+29-11
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def page opts={}
157157
def source_window window_number=0
158158
# appium 1.0 still returns JSON when getTree() is invoked so this
159159
# 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()\
161161
# https://github.com/appium/appium-uiauto/blob/247eb71383fa1a087ff8f8fc96fac25025731f3f/uiauto/appium/element.js#L145
162162
execute_script "UIATarget.localTarget().frontMostApp().windows()[#{window_number}].getTree()"
163163
end
@@ -192,11 +192,19 @@ def ios_version
192192
# @param index [Integer] the index
193193
# @return [Element]
194194
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]
198204
end
199-
find_element :xpath, %Q(//#{class_name}[@visible="true"][#{index}])
205+
206+
raise _no_such_element if result.nil?
207+
result
200208
end
201209

202210
# @private
@@ -205,6 +213,7 @@ def string_attr_exact class_name, attr, value
205213
end
206214

207215
# Find the first element exactly matching class and attribute value.
216+
# Note: Uses XPath
208217
# @param class_name [String] the class name to search for
209218
# @param attr [String] the attribute to inspect
210219
# @param value [String] the expected value of the attribute
@@ -214,6 +223,7 @@ def find_ele_by_attr class_name, attr, value
214223
end
215224

216225
# Find all elements exactly matching class and attribute value.
226+
# Note: Uses XPath
217227
# @param class_name [String] the class name to match
218228
# @param attr [String] the attribute to compare
219229
# @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
228238
end
229239

230240
# Get the first tag by attribute that exactly matches value.
241+
# Note: Uses XPath
231242
# @param class_name [String] the tag name to match
232243
# @param attr [String] the attribute to compare
233244
# @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
237248
end
238249

239250
# Get tags by attribute that include value.
251+
# Note: Uses XPath
240252
# @param class_name [String] the tag name to match
241253
# @param attr [String] the attribute to compare
242254
# @param value [String] the value of the attribute that the element must include
@@ -260,24 +272,30 @@ def last_ele class_name
260272
ele_index class_name, 'last()'
261273
end
262274

263-
# Returns the first element matching class_name
275+
# Returns the first visible element matching class_name
264276
#
265277
# @param class_name [String] the class_name to search for
266278
# @return [Element]
267279
def tag class_name
268-
xpath %Q(//#{class_name}[@visible="true"])
280+
ele_by_json({
281+
typeArray: [class_name],
282+
onlyVisible: true,
283+
})
269284
end
270285

271-
# Returns all elements matching class_name
286+
# Returns all visible elements matching class_name
272287
#
273288
# @param class_name [String] the class_name to search for
274289
# @return [Element]
275290
def tags class_name
276-
xpaths %Q(//#{class_name}[@visible="true"])
291+
eles_by_json({
292+
typeArray: [class_name],
293+
onlyVisible: true,
294+
})
277295
end
278296

279297
# @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
281299
#
282300
# example: ele_by_json_visible_contains 'UIATextField', 'sign in'
283301
#
@@ -317,7 +335,7 @@ def eles_by_json_visible_contains element, value
317335
end
318336

319337
# @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
321339
# @param element [String] the class name for the element
322340
# @param value [String] the value to search for
323341
# @return [String]

0 commit comments

Comments
 (0)