@@ -77,6 +77,15 @@ def wait_true max_wait=30, interval=0.5, &block
77
77
result
78
78
end
79
79
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
+
80
89
# Navigate back.
81
90
# @return [void]
82
91
def back
@@ -109,83 +118,111 @@ def xpaths xpath_str
109
118
# @param index [Integer] the index
110
119
# @return [Element] the found element of type tag_name
111
120
def ele_index tag_name , index
121
+ class_name = tag_to_class tag_name
112
122
# XPath index starts at 1.
113
123
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 } ]"
115
125
end
116
126
117
127
# Get all elements exactly matching tag name
118
128
# @param tag_name [String] the tag name to find
119
129
# @return [Array<Element>] the found elements of type tag_name
120
130
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
122
133
end
123
134
124
135
# 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
126
137
# @param text [String] the text to exactly match
127
138
# @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 } '])
130
142
end
131
143
132
144
# 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
134
146
# @param text [String] the text to exactly match
135
147
# @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 } '])
138
161
end
139
162
140
163
# 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
142
165
# @param attr [String] the attribute to compare
143
166
# @param value [String] the value of the attribute that the element must include
144
167
# @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 } ')])
147
172
end
148
173
149
174
# 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
151
176
# @param attr [String] the attribute to compare
152
177
# @param value [String] the value of the attribute that the element must include
153
178
# @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 } ')])
156
183
end
157
184
158
185
# 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
160
187
# @param text [String] the text the element must include
161
188
# @return [Element] the element of type tag that includes text
162
189
# 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
165
192
end
166
193
167
194
# 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
169
196
# @param text [String] the text the element must include
170
197
# @return [Array<Element>] the elements of type tag that includes text
171
198
# 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
174
209
end
175
210
176
211
# Get the first tag that matches tag_name
177
212
# @param tag_name [String] the tag to match
178
213
# @return [Element]
179
214
def first_ele tag_name
180
215
# 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]"
182
218
end
183
219
184
220
# Get the last tag that matches tag_name
185
221
# @param tag_name [String] the tag to match
186
222
# @return [Element]
187
223
def last_ele tag_name
188
- xpath "//#{ tag_name } [last()]"
224
+ class_name = tag_to_class tag_name
225
+ xpath "//#{ class_name } [last()]"
189
226
end
190
227
191
228
# Prints xml of the current page
@@ -270,15 +307,17 @@ def find_names name
270
307
# @param tag_name [String] the tag_name to search for
271
308
# @return [Element]
272
309
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
274
312
end
275
313
276
314
# Returns all elements matching tag_name
277
315
#
278
316
# @param tag_name [String] the tag_name to search for
279
317
# @return [Element]
280
318
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
282
321
end
283
322
284
323
# Converts pixel values to window relative values
@@ -288,7 +327,6 @@ def tags tag_name
288
327
# ```
289
328
290
329
def px_to_window_rel opts = { }
291
-
292
330
w = $driver. window_size
293
331
x = opts . fetch :x , 0
294
332
y = opts . fetch :y , 0
0 commit comments