Skip to content

Commit 67f6361

Browse files
Fix textfield methods on iOS
Fix id method Fix #89
1 parent be86199 commit 67f6361

File tree

4 files changed

+38
-15
lines changed

4 files changed

+38
-15
lines changed

lib/appium_lib/android/helper.rb

+10
Original file line numberDiff line numberDiff line change
@@ -424,4 +424,14 @@ def current_app
424424
activity: act,
425425
am_start: pkg + '/' + act
426426
end
427+
428+
# Find by id. Useful for selendroid
429+
# @param id [String] the id to search for
430+
# @return [Element]
431+
def id id
432+
lazy_load_strings
433+
# resource ids must include ':' and they're not contained in strings_xml
434+
raise "Invalid id `#{id}`" unless @strings_xml[id] || id.include?(':')
435+
find_element :id, id
436+
end
427437
end # module Appium::Android

lib/appium_lib/common/helper.rb

+6-7
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,6 @@ def wait_true max_wait=30, interval=0.5, &block
6363
result
6464
end
6565

66-
# Find by id. Useful for selendroid
67-
# @param id [String] the id to search for
68-
# @return [Element]
69-
def id id
70-
find_element :id, id
71-
end
72-
7366
# Navigate back.
7467
# @return [void]
7568
def back
@@ -260,4 +253,10 @@ def resolve_id id
260253
lazy_load_strings
261254
@strings_xml[id]
262255
end
256+
257+
# Used to error when finding a single element fails.
258+
def raise_no_element_error
259+
raise Selenium::WebDriver::Error::NoSuchElementError, 'An element could not be located on the page using the given search parameters.'
260+
end
261+
263262
end # module Appium::Common

lib/appium_lib/ios/element/textfield.rb

+13-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ module Appium::Ios
55
# Find textfield and then secure elements in one server call
66
# to match Android.
77

8+
def locate_single_textfield js
9+
ele = execute_script(js)
10+
ele = ele.first if ele.kind_of? Array
11+
raise_no_element_error unless ele.kind_of? Selenium::WebDriver::Element
12+
ele
13+
end
14+
815
# Get an array of textfield texts.
916
# @return [Array<String>]
1017
def textfields
@@ -21,14 +28,14 @@ def e_textfields
2128
# @return [Textfield]
2229
def first_textfield
2330
js = textfield_js 'r = r.length > 0 ? $(r[0]) : r;'
24-
execute_script(js).first
31+
locate_single_textfield js
2532
end
2633

2734
# Get the last textfield element.
2835
# @return [Textfield]
2936
def last_textfield
3037
js = textfield_js 'r = r.length > 0 ? $(r[r.length - 1]) : r;'
31-
execute_script(js).first
38+
locate_single_textfield js
3239
end
3340

3441
# Get the first textfield that matches text.
@@ -39,7 +46,7 @@ def textfield text
3946
# iOS needs to combine textfield and secure to match Android.
4047
if text.is_a? Numeric
4148
js = textfield_js "r = r.length > 0 ? $(r[#{text}]) : r;"
42-
return execute_script(js).first
49+
return locate_single_textfield js
4350
end
4451

4552
textfield_include text
@@ -54,8 +61,7 @@ def textfield_include text
5461
var s = au.getElementsByXpath('secure[contains(@text, "#{text}")]').value;
5562
t.concat(s)[0];
5663
)
57-
58-
execute_script js
64+
locate_single_textfield js
5965
end
6066

6167
# Get the first textfield that exactly matches text.
@@ -68,15 +74,14 @@ def textfield_exact text
6874
var s = au.getElementsByXpath('secure[@text="#{text}"]').value;
6975
t.concat(s)[0];
7076
)
71-
72-
execute_script js
77+
locate_single_textfield js
7378
end
7479

7580
# @private
7681
# Return combined lookup of textfield and secure
7782
# with an optional filter. $() wrap is required for .each
7883
def textfield_js filter=''
79-
%Q(
84+
%Q(
8085
var t = au.lookup('textfield');
8186
var s = au.lookup('secure');
8287
var r = $(t.concat(s));

lib/appium_lib/ios/helper.rb

+9
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,13 @@ def page_window window_number=0
176176
def fast_duration
177177
0.5
178178
end
179+
180+
# Find by id. Useful for selendroid
181+
# @param id [String] the id to search for
182+
# @return [Element]
183+
def id id
184+
lazy_load_strings
185+
raise "Invalid id `#{id}`" unless @strings_xml[id]
186+
find_element :id, id
187+
end
179188
end # module Appium::Ios

0 commit comments

Comments
 (0)