Skip to content

Commit c5cf51d

Browse files
Rewrite iOS helpers
1 parent eb22269 commit c5cf51d

File tree

5 files changed

+52
-63
lines changed

5 files changed

+52
-63
lines changed

ios_tests/lib/ios/specs/common/helper.rb

+3-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# rake ios[common/helper]
22
describe 'common/helper.rb' do
3+
34
def before_first
45
screen.must_equal catalog
56
end
@@ -158,20 +159,10 @@ def uibutton_text
158159
end
159160

160161
t 'invalid id should error' do
161-
begin
162-
id 'does not exist'
163-
rescue Exception => e
164-
message = e.message
165-
end
166-
message.must_equal 'Invalid id `does not exist`'
162+
proc { id 'does not exist' }.must_raise Selenium::WebDriver::Error::NoSuchElementError
167163

168164
# resource id should error on ios
169-
begin
170-
id 'android:id/text1'
171-
rescue Exception => e
172-
message = e.message
173-
end
174-
message.must_equal 'Invalid id `android:id/text1`'
165+
proc { id 'android:id/text1' }.must_raise Selenium::WebDriver::Error::NoSuchElementError
175166
end
176167

177168
t 'tag' do

lib/appium_lib/ios/element/button.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module Ios
1010
def button value
1111
# return button at index.
1212
return ele_index UIAButton, value if value.is_a? Numeric
13-
xpath_visible_contains UIAButton, value
13+
ele_by_json_visible_contains UIAButton, value
1414
end
1515

1616
# Find all UIAButtons containing value.
@@ -19,7 +19,7 @@ def button value
1919
# @return [Array<UIAButton>]
2020
def buttons value=false
2121
return tags UIAButton unless value
22-
xpaths_visible_contains UIAButton, value
22+
eles_by_json_visible_contains UIAButton, value
2323
end
2424

2525
# Find the first UIAButton.
@@ -38,14 +38,14 @@ def last_button
3838
# @param value [String] the value to match exactly
3939
# @return [UIAButton]
4040
def button_exact value
41-
xpath_visible_exact UIAButton, value
41+
ele_by_json_visible_exact UIAButton, value
4242
end
4343

4444
# Find all UIAButtons that exactly match value.
4545
# @param value [String] the value to match exactly
4646
# @return [Array<UIAButton>]
4747
def buttons_exact value
48-
xpaths_visible_exact UIAButton, value
48+
eles_by_json_visible_exact UIAButton, value
4949
end
5050
end # module Ios
5151
end # module Appium

lib/appium_lib/ios/element/generic.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,28 @@ module Ios
55
# @param value [String] the value to search for
66
# @return [Element]
77
def find value
8-
xpath_visible_contains '*', value
8+
ele_by_json_visible_contains '*', value
99
end
1010

1111
# Find all elements containing value
1212
# @param value [String] the value to search for
1313
# @return [Array<Element>]
1414
def finds value
15-
xpaths_visible_contains '*', value
15+
eles_by_json_visible_contains '*', value
1616
end
1717

1818
# Find the first element exactly matching value
1919
# @param value [String] the value to search for
2020
# @return [Element]
2121
def find_exact value
22-
xpath_visible_exact '*', value
22+
ele_by_json_visible_exact '*', value
2323
end
2424

2525
# Find all elements exactly matching value
2626
# @param value [String] the value to search for
2727
# @return [Array<Element>]
2828
def finds_exact value
29-
xpaths_visible_exact '*', value
29+
eles_by_json_visible_exact '*', value
3030
end
3131
end # module Ios
3232
end # module Appium

lib/appium_lib/ios/element/text.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module Ios
99
# @return [UIAStaticText]
1010
def text value
1111
return ele_index UIAStaticText, value if value.is_a? Numeric
12-
xpath_visible_contains UIAStaticText, value
12+
ele_by_json_visible_contains UIAStaticText, value
1313
end
1414

1515
# Find all UIAStaticText containing value.
@@ -18,7 +18,7 @@ def text value
1818
# @return [Array<UIAStaticText>]
1919
def texts value=false
2020
return tags UIAStaticText unless value
21-
xpaths_visible_contains UIAStaticText, value
21+
eles_by_json_visible_contains UIAStaticText, value
2222
end
2323

2424
# Find the first UIAStaticText.
@@ -37,14 +37,14 @@ def last_text
3737
# @param value [String] the value to match exactly
3838
# @return [UIAStaticText]
3939
def text_exact value
40-
xpath_visible_exact UIAStaticText, value
40+
ele_by_json_visible_exact UIAStaticText, value
4141
end
4242

4343
# Find all UIAStaticTexts that exactly match value.
4444
# @param value [String] the value to match exactly
4545
# @return [Array<UIAStaticText>]
4646
def texts_exact value
47-
xpaths_visible_exact UIAStaticText, value
47+
eles_by_json_visible_exact UIAStaticText, value
4848
end
4949
end # module Ios
5050
end # module Appium

lib/appium_lib/ios/helper.rb

+37-39
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,7 @@ def page_window window_number=0
177177
# @param id [String] the id to search for
178178
# @return [Element]
179179
def id id
180-
value = resolve_id id
181-
raise "Invalid id `#{id}`" unless value
182-
exact = string_visible_exact '*', value
183-
contains = string_visible_contains '*', value
184-
xpath "#{exact} | #{contains}"
180+
find_element :id, id
185181
end
186182

187183
# Return the iOS version as an array of integers
@@ -283,41 +279,41 @@ def tags class_name
283279
# @private
284280
# Returns a string xpath that matches the first element that contains value
285281
#
286-
# example: xpath_visible_contains 'UIATextField', 'sign in'
282+
# example: ele_by_json_visible_contains 'UIATextField', 'sign in'
287283
#
288284
# @param element [String] the class name for the element
289285
# @param value [String] the value to search for
290286
# @return [String]
291287
def string_visible_contains element, value
292-
result = []
293-
attributes = %w[name hint label value]
294-
295-
value_up = value.upcase
296-
value_down = value.downcase
297-
298-
attributes.each do |attribute|
299-
result << %Q(contains(translate(@#{attribute},"#{value_up}","#{value_down}"), "#{value_down}"))
300-
end
288+
contains = {
289+
target: value,
290+
substring: true,
291+
insensitive: true,
292+
}
301293

302-
result = result.join(' or ')
303-
result = %Q(@visible="true" and (#{result}))
304-
"//#{element}[#{result}]"
294+
{
295+
typeArray: [element],
296+
onlyVisible: true,
297+
name: contains,
298+
label: contains,
299+
value: contains,
300+
}
305301
end
306302

307303
# Find the first element that contains value
308304
# @param element [String] the class name for the element
309305
# @param value [String] the value to search for
310306
# @return [Element]
311-
def xpath_visible_contains element, value
312-
xpath string_visible_contains element, value
307+
def ele_by_json_visible_contains element, value
308+
ele_by_json string_visible_contains element, value
313309
end
314310

315311
# Find all elements containing value
316312
# @param element [String] the class name for the element
317313
# @param value [String] the value to search for
318314
# @return [Array<Element>]
319-
def xpaths_visible_contains element, value
320-
xpaths string_visible_contains element, value
315+
def eles_by_json_visible_contains element, value
316+
eles_by_json string_visible_contains element, value
321317
end
322318

323319
# @private
@@ -326,33 +322,35 @@ def xpaths_visible_contains element, value
326322
# @param value [String] the value to search for
327323
# @return [String]
328324
def string_visible_exact element, value
329-
result = []
330-
attributes = %w[name hint label value]
331-
332-
attributes.each do |attribute|
333-
result << %Q(@#{attribute}="#{value}")
334-
end
335-
336-
result = result.join(' or ')
337-
result = %Q(@visible="true" and (#{result}))
325+
exact = {
326+
target: value,
327+
substring: false,
328+
insensitive: false,
329+
}
338330

339-
"//#{element}[#{result}]"
331+
{
332+
typeArray: [element],
333+
onlyVisible: true,
334+
name: exact,
335+
label: exact,
336+
value: exact,
337+
}
340338
end
341339

342340
# Find the first element exactly matching value
343341
# @param element [String] the class name for the element
344342
# @param value [String] the value to search for
345343
# @return [Element]
346-
def xpath_visible_exact element, value
347-
xpath string_visible_exact element, value
344+
def ele_by_json_visible_exact element, value
345+
ele_by_json string_visible_exact element, value
348346
end
349347

350348
# Find all elements exactly matching value
351349
# @param element [String] the class name for the element
352350
# @param value [String] the value to search for
353351
# @return [Element]
354-
def xpaths_visible_exact element, value
355-
xpaths string_visible_exact element, value
352+
def eles_by_json_visible_exact element, value
353+
eles_by_json string_visible_exact element, value
356354
end
357355

358356
# @private
@@ -448,7 +446,7 @@ def _validate_object *objects
448446
objects.each do |obj|
449447
next unless obj # obj may be nil. if so, ignore.
450448

451-
valid_keys = [:target, :substring, :insensitive]
449+
valid_keys = [:target, :substring, :insensitive]
452450
unknown_keys = obj.keys - valid_keys
453451
raise "Unknown keys: #{unknown_keys}" unless unknown_keys.empty?
454452

@@ -492,7 +490,7 @@ def _validate_object *objects
492490
# }
493491
#
494492
def _by_json opts
495-
valid_keys = [:typeArray, :onlyFirst, :onlyVisible, :name, :label, :value]
493+
valid_keys = [:typeArray, :onlyFirst, :onlyVisible, :name, :label, :value]
496494
unknown_keys = opts.keys - valid_keys
497495
raise "Unknown keys: #{unknown_keys}" unless unknown_keys.empty?
498496

@@ -539,7 +537,7 @@ def eles_by_json opts
539537
# see eles_by_json
540538
def ele_by_json opts
541539
opts[:onlyFirst] = true
542-
result = _by_json(opts).first
540+
result = _by_json(opts).first
543541
raise _no_such_element if result.nil?
544542
result
545543
end

0 commit comments

Comments
 (0)