Skip to content

Commit 2e10eb3

Browse files
committedApr 23, 2014
Start updating for new XPath
1 parent 62c977a commit 2e10eb3

File tree

5 files changed

+133
-28
lines changed

5 files changed

+133
-28
lines changed
 

‎ios_tests/lib/ios/specs/common/element/text.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# encoding: utf-8
2-
2+
# rake ios['common/element/text']
33
describe 'common/element/text' do
44
def before_first
55
screen.must_equal catalog

‎lib/appium_lib/common/element/text.rb ‎lib/appium_lib/android/element/text.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# encoding: utf-8
22
# UIAStaticText methods
33
module Appium
4-
module Common
4+
module Android
55
# s_ prefix for static_text to avoid conflict with generic text methods.
66

77
# Get an array of text texts if text is nil else

‎lib/appium_lib/common/helper.rb

+63-25
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ def wait_true max_wait=30, interval=0.5, &block
7777
result
7878
end
7979

80+
def tag_to_class tag_name
81+
case tag_name
82+
when 'text', :text
83+
device_is_android? ? 'android.widget.TextView' : 'UIAStaticText'
84+
else
85+
tag_name
86+
end
87+
end
88+
8089
# Navigate back.
8190
# @return [void]
8291
def back
@@ -109,83 +118,111 @@ def xpaths xpath_str
109118
# @param index [Integer] the index
110119
# @return [Element] the found element of type tag_name
111120
def ele_index tag_name, index
121+
class_name = tag_to_class tag_name
112122
# XPath index starts at 1.
113123
raise "#{index} is not a valid xpath index. Must be >= 1" if index <= 0
114-
find_element :xpath, "//#{tag_name}[#{index}]"
124+
find_element :xpath, "//#{class_name}[#{index}]"
115125
end
116126

117127
# Get all elements exactly matching tag name
118128
# @param tag_name [String] the tag name to find
119129
# @return [Array<Element>] the found elements of type tag_name
120130
def find_eles tag_name
121-
@driver.find_elements :tag_name, tag_name
131+
class_name = tag_to_class tag_name
132+
@driver.find_elements :class, class_name
122133
end
123134

124135
# Get the first tag that exactly matches tag and text.
125-
# @param tag [String] the tag name to match
136+
# @param tag_name [String] the tag name to match
126137
# @param text [String] the text to exactly match
127138
# @return [Element] the element of type tag exactly matching text
128-
def find_ele_by_text tag, text
129-
@driver.find_element :xpath, %Q(#{tag}[@text='#{text}'])
139+
def find_ele_by_text tag_name, text
140+
class_name = tag_to_class tag_name
141+
@driver.find_element :xpath, %Q(//#{class_name}[@text='#{text}'])
130142
end
131143

132144
# Get all tags that exactly match tag and text.
133-
# @param tag [String] the tag name to match
145+
# @param tag_name [String] the tag name to match
134146
# @param text [String] the text to exactly match
135147
# @return [Array<Element>] the elements of type tag exactly matching text
136-
def find_eles_by_text tag, text
137-
@driver.find_elements :xpath, %Q(#{tag}[@text='#{text}'])
148+
def find_eles_by_text tag_name, text
149+
class_name = tag_to_class tag_name
150+
@driver.find_elements :xpath, %Q(//#{class_name}[@text='#{text}'])
151+
end
152+
153+
def find_ele_by_name tag_name, name
154+
class_name = tag_to_class tag_name
155+
@driver.find_element :xpath, %Q(//#{class_name}[@name='#{name}'])
156+
end
157+
158+
def find_eles_by_name tag_name, name
159+
class_name = tag_to_class tag_name
160+
@driver.find_elements :xpath, %Q(//#{class_name}[@name='#{name}'])
138161
end
139162

140163
# Get the first tag by attribute that exactly matches value.
141-
# @param tag [String] the tag name to match
164+
# @param tag_name [String] the tag name to match
142165
# @param attr [String] the attribute to compare
143166
# @param value [String] the value of the attribute that the element must include
144167
# @return [Element] the element of type tag who's attribute includes value
145-
def find_ele_by_attr_include tag, attr, value
146-
@driver.find_element :xpath, %Q(#{tag}[contains(@#{attr}, '#{value}')])
168+
def find_ele_by_attr_include tag_name, attr, value
169+
class_name = tag_to_class tag_name
170+
value.downcase!
171+
@driver.find_element :xpath, %Q(//#{class_name}[contains(translate(@#{attr},'#{value.upcase}', '#{value}'), '#{value}')])
147172
end
148173

149174
# Get tags by attribute that include value.
150-
# @param tag [String] the tag name to match
175+
# @param tag_name [String] the tag name to match
151176
# @param attr [String] the attribute to compare
152177
# @param value [String] the value of the attribute that the element must include
153178
# @return [Array<Element>] the elements of type tag who's attribute includes value
154-
def find_eles_by_attr_include tag, attr, value
155-
@driver.find_elements :xpath, %Q(#{tag}[contains(@#{attr}, '#{value}')])
179+
def find_eles_by_attr_include tag_name, attr, value
180+
class_name = tag_to_class tag_name
181+
value.downcase!
182+
@driver.find_elements :xpath, %Q(//#{class_name}[contains(translate(@#{attr},'#{value.upcase}', '#{value}'), '#{value}')])
156183
end
157184

158185
# Get the first tag that includes text.
159-
# @param tag [String] the tag name to match
186+
# @param tag_name [String] the tag name to match
160187
# @param text [String] the text the element must include
161188
# @return [Element] the element of type tag that includes text
162189
# element.attribute(:text).include? text
163-
def find_ele_by_text_include tag, text
164-
find_ele_by_attr_include tag, :text, text
190+
def find_ele_by_text_include tag_name, text
191+
find_ele_by_attr_include tag_name, :text, text
165192
end
166193

167194
# Get the tags that include text.
168-
# @param tag [String] the tag name to match
195+
# @param tag_name [String] the tag name to match
169196
# @param text [String] the text the element must include
170197
# @return [Array<Element>] the elements of type tag that includes text
171198
# element.attribute(:text).include? text
172-
def find_eles_by_text_include tag, text
173-
find_eles_by_attr_include tag, :text, text
199+
def find_eles_by_text_include tag_name, text
200+
find_eles_by_attr_include tag_name, :text, text
201+
end
202+
203+
def find_ele_by_name_include tag_name, text
204+
find_ele_by_attr_include tag_name, :name, text
205+
end
206+
207+
def find_eles_by_name_include tag_name, text
208+
find_eles_by_attr_include tag_name, :name, text
174209
end
175210

176211
# Get the first tag that matches tag_name
177212
# @param tag_name [String] the tag to match
178213
# @return [Element]
179214
def first_ele tag_name
180215
# XPath index starts at 1
181-
find_element :xpath, "//#{tag_name}[1]"
216+
class_name = tag_to_class tag_name
217+
find_element :xpath, "//#{class_name}[1]"
182218
end
183219

184220
# Get the last tag that matches tag_name
185221
# @param tag_name [String] the tag to match
186222
# @return [Element]
187223
def last_ele tag_name
188-
xpath "//#{tag_name}[last()]"
224+
class_name = tag_to_class tag_name
225+
xpath "//#{class_name}[last()]"
189226
end
190227

191228
# Prints xml of the current page
@@ -270,15 +307,17 @@ def find_names name
270307
# @param tag_name [String] the tag_name to search for
271308
# @return [Element]
272309
def tag tag_name
273-
find_element :tag_name, tag_name
310+
class_name = tag_to_class tag_name
311+
find_element :class, class_name
274312
end
275313

276314
# Returns all elements matching tag_name
277315
#
278316
# @param tag_name [String] the tag_name to search for
279317
# @return [Element]
280318
def tags tag_name
281-
find_elements :tag_name, tag_name
319+
class_name = tag_to_class tag_name
320+
find_elements :class, class_name
282321
end
283322

284323
# Converts pixel values to window relative values
@@ -288,7 +327,6 @@ def tags tag_name
288327
# ```
289328

290329
def px_to_window_rel opts={}
291-
292330
w = $driver.window_size
293331
x = opts.fetch :x, 0
294332
y = opts.fetch :y, 0

‎lib/appium_lib/driver.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ def initialize opts={}
260260

261261
# load common methods
262262
extend Appium::Common
263-
if @device == :android
263+
if device_is_android?
264264
# load Android specific methods
265265
extend Appium::Android
266266
else
@@ -320,6 +320,10 @@ def driver_attributes
320320
attributes
321321
end
322322

323+
def device_is_android?
324+
@device == :android
325+
end
326+
323327
# Returns the server's version info
324328
#
325329
# ```ruby

‎lib/appium_lib/ios/element/text.rb

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# encoding: utf-8
2+
# UIAStaticText methods
3+
module Appium
4+
module Ios
5+
# s_ prefix for static_text to avoid conflict with generic text methods.
6+
7+
# Get an array of text texts if text is nil else
8+
# Get all static textfields that include text.
9+
# @param text [String] the text to find.
10+
# @return [Array]
11+
def s_texts text=nil
12+
if text
13+
find_eles_by_name_include :text, text
14+
else
15+
find_eles_attr :text, :name
16+
end
17+
end
18+
19+
def s_texts_names
20+
find_eles_attr :text, :name
21+
end
22+
23+
# Get an array of text elements.
24+
# @return [Array<Text>]
25+
def e_s_texts
26+
find_eles :text
27+
end
28+
29+
# Get the first text element.
30+
# @return [Text]
31+
def first_s_text
32+
first_ele :text
33+
end
34+
35+
# Get the last text element
36+
# @return [Text]
37+
def last_s_text
38+
last_ele :text
39+
end
40+
41+
# Get the first element that includes text.
42+
# @param text [String, Integer] the text to find. If int then the text at that index is returned.
43+
# @return [Text]
44+
def s_text text
45+
return ele_index :text, text if text.is_a? Numeric
46+
find_ele_by_name_include :text, text
47+
end
48+
49+
# Get the first textfield that matches text.
50+
# @param text [String] the text that the tag must match
51+
# @return [Text]
52+
def s_text_exact text
53+
find_ele_by_name :text, text
54+
end
55+
56+
# Get all static textfields that matches text.
57+
# @param text [String] the text that the tag must match
58+
# @return [Array<Text>]
59+
def s_texts_exact text
60+
find_eles_by_name :text, text
61+
end
62+
end # module Common
63+
end # module Appium

0 commit comments

Comments
 (0)
Please sign in to comment.