Skip to content

Commit ead35f9

Browse files
committedApr 28, 2014
Refactor iOS textfield xpath
1 parent b26d77d commit ead35f9

File tree

3 files changed

+80
-59
lines changed

3 files changed

+80
-59
lines changed
 

‎ios_tests/lib/ios/specs/ios/element/textfield.rb

+31-27
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,10 @@ def after_last
2020
before_first
2121
end
2222

23-
t 'e_textfields' do
24-
e_textfields.length.must_equal 4
25-
end
26-
27-
t 'first_textfield' do
28-
first_textfield.text.must_equal enter_text
29-
end
30-
31-
t 'last_textfield' do
32-
last_textfield.text.must_equal enter_password
33-
end
34-
3523
t 'textfield' do
3624
textfield(1).text.must_equal(enter_text)
3725
textfield(enter_text).text.must_equal(enter_text)
26+
textfield('word').text.must_equal enter_password
3827
end
3928

4029
t 'textfields' do
@@ -43,16 +32,28 @@ def after_last
4332
values.include?('<enter password>').must_equal true
4433
end
4534

46-
t 'textfield_include' do
47-
textfield_include('word').text.must_equal enter_password
35+
t 'first_textfield' do
36+
first_textfield.text.must_equal enter_text
37+
end
38+
39+
t 'last_textfield' do
40+
last_textfield.text.must_equal enter_password
4841
end
4942

5043
t 'textfield_exact' do
5144
textfield_exact(enter_password).text.must_equal enter_password
5245
end
5346

47+
t 'textfields_exact' do
48+
textfields_exact(enter_password).first.text.must_equal enter_password
49+
end
50+
51+
t 'e_textfields' do
52+
e_textfields.length.must_equal 4
53+
end
54+
5455
def keyboard_exists?
55-
!! ignore { wait_true(3) { execute_script 'au.mainApp().keyboard().type() !== "UIAElementNil"' } }
56+
!!ignore { wait_true(3) { execute_script 'au.mainApp().keyboard().type() !== "UIAElementNil"' } }
5657
end
5758

5859
def keyboard_must_not_exist
@@ -81,15 +82,19 @@ def must_raise_no_element &block
8182
end
8283

8384
# test textfield methods with no textfields
84-
t 'no textfields' do
85+
86+
t 'leave textfields' do
8587
set_wait 1
86-
# must leave textfield screen for the rest of the tests
8788
leave_textfields
88-
e_textfields.must_equal []
8989
end
9090

91-
t 'no e_textfields' do
92-
e_textfields.length.must_equal 0
91+
t 'no textfield' do
92+
must_raise_no_element { textfield(1) }
93+
must_raise_no_element { textfield('does not exist') }
94+
end
95+
96+
t 'no textfields' do
97+
textfields('does not exist').length.must_equal 0
9398
end
9499

95100
t 'no first_textfield' do
@@ -100,17 +105,16 @@ def must_raise_no_element &block
100105
must_raise_no_element { last_textfield }
101106
end
102107

103-
t 'no textfield' do
104-
must_raise_no_element { textfield(1) }
105-
must_raise_no_element { textfield('does not exist') }
108+
t 'no textfield_exact' do
109+
must_raise_no_element { textfield_exact('does not exist') }
106110
end
107111

108-
t 'no textfield_include' do
109-
must_raise_no_element { textfield_include('does not exist') }
112+
t 'no textfields_exact' do
113+
textfields_exact('does not exist').length.must_equal 0
110114
end
111115

112-
t 'no textfield_exact' do
113-
must_raise_no_element { textfield_exact('does not exist') }
116+
t 'no e_textfields' do
117+
e_textfields.length.must_equal 0
114118
end
115119

116120
t 'after_last' do
+44-28
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
11
module Appium
22
module Ios
3-
UIATextField = 'UIATextField'
3+
UIATextField = 'UIATextField'
44
UIASecureTextField = 'UIASecureTextField'
5-
# Get an array of textfield elements.
6-
# @return [Array<Textfield>]
7-
def e_textfields
8-
xpaths %Q(//#{UIATextField}[@visible="true"] | //#{UIASecureTextField}[@visible="true"])
5+
6+
# @private
7+
def _textfield_visible_string opts={}
8+
index = opts.fetch :index, false
9+
if index
10+
%Q(//#{UIATextField}[@visible="true"][#{index}] | //#{UIASecureTextField}[@visible="true"][#{index}])
11+
else
12+
%Q(//#{UIATextField}[@visible="true"] | //#{UIASecureTextField}[@visible="true"])
13+
end
914
end
1015

11-
# Get the first textfield element.
12-
# @return [Textfield]
13-
def first_textfield
14-
xpath %Q(//#{UIATextField}[@visible="true"] | //#{UIASecureTextField}[@visible="true"])
16+
# @private
17+
def _textfield_exact_string value
18+
textfield = string_visible_exact UIATextField, value
19+
secure = string_visible_exact UIASecureTextField, value
20+
"#{textfield} | #{secure}"
1521
end
1622

17-
# Get the last textfield element.
18-
# @return [Textfield]
19-
def last_textfield
20-
xpath %Q(//#{UIATextField}[@visible="true"][last()] | //#{UIASecureTextField}[@visible="true"][last()])
23+
# @private
24+
def _textfield_contains_string value
25+
textfield = string_visible_contains UIATextField, value
26+
secure = string_visible_contains UIASecureTextField, value
27+
"#{textfield} | #{secure}"
2128
end
2229

2330
# Get the first textfield that matches text.
24-
# @param text [String, Integer] the text to match exactly. If int then the textfield at that index is returned.
31+
# @param value [String, Integer] the text to match exactly. If int then the textfield at that index is returned.
2532
# @return [Textfield]
2633
def textfield value
2734
# Don't use ele_index because that only works on one element type.
@@ -30,34 +37,43 @@ def textfield value
3037
index = value
3138
raise "#{index} is not a valid xpath index. Must be >= 1" if index <= 0
3239

33-
return xpath(%Q(//#{UIATextField}[@visible="true"][#{index}] | //#{UIASecureTextField}[@visible="true"][#{index}]))
40+
return xpath _textfield_visible_string index: index
3441
end
3542

36-
textfield_include value
43+
xpath _textfield_contains_string value
3744
end
3845

3946
def textfields value
40-
textfield = string_visible_include UIATextField, value
41-
secure = string_visible_include UIASecureTextField, value
42-
xpaths "#{textfield} | #{secure}"
47+
xpaths _textfield_contains_string value
4348
end
4449

45-
# Get the first textfield that includes text.
46-
# @param value [String] the value the textfield must include
50+
# Get the first textfield element.
4751
# @return [Textfield]
48-
def textfield_include value
49-
textfield = string_visible_include UIATextField, value
50-
secure = string_visible_include UIASecureTextField, value
51-
xpath "#{textfield} | #{secure}"
52+
def first_textfield
53+
xpath _textfield_visible_string
54+
end
55+
56+
# Get the last textfield element.
57+
# @return [Textfield]
58+
def last_textfield
59+
xpath _textfield_visible_string index: 'last()'
5260
end
5361

5462
# Get the first textfield that exactly matches text.
5563
# @param value [String] the value the textfield must exactly match
5664
# @return [Textfield]
5765
def textfield_exact value
58-
textfield = string_visible_exact UIATextField, value
59-
secure = string_visible_exact UIASecureTextField, value
60-
xpath "#{textfield} | #{secure}"
66+
xpath _textfield_exact_string value
67+
end
68+
69+
def textfields_exact value
70+
xpaths _textfield_exact_string value
71+
end
72+
73+
# Get an array of textfield elements.
74+
# @return [Array<Textfield>]
75+
def e_textfields
76+
xpaths _textfield_visible_string
6177
end
6278
end # module Ios
6379
end # module Appium

‎lib/appium_lib/ios/helper.rb

+5-4
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def id id
138138
value = resolve_id id
139139
raise "Invalid id `#{id}`" unless value
140140
exact = string_visible_exact '*', value
141-
contains = string_visible_include '*', value
141+
contains = string_visible_contains '*', value
142142
xpath "#{exact} | #{contains}"
143143
end
144144

@@ -226,7 +226,7 @@ def tags class_name
226226

227227
# xpath fragment helper
228228
# example: xpath_visible_contains 'UIATextField', text
229-
def string_visible_include element, value
229+
def string_visible_contains element, value
230230
result = []
231231
attributes = %w[name hint label value]
232232

@@ -243,11 +243,11 @@ def string_visible_include element, value
243243
end
244244

245245
def xpath_visible_contains element, value
246-
xpath string_visible_include element, value
246+
xpath string_visible_contains element, value
247247
end
248248

249249
def xpaths_visible_contains element, value
250-
xpaths string_visible_include element, value
250+
xpaths string_visible_contains element, value
251251
end
252252

253253
def string_visible_exact element, value
@@ -260,6 +260,7 @@ def string_visible_exact element, value
260260

261261
result = result.join(' or ')
262262
result = %Q(@visible="true" and (#{result}))
263+
263264
"//#{element}[#{result}]"
264265
end
265266

0 commit comments

Comments
 (0)
Please sign in to comment.