@@ -15,7 +15,7 @@ module Common
15
15
# Android and iOS have proper accessibility attributes.
16
16
# .text and .value should be the same so use .text over .value.
17
17
#
18
- # secure tag_name is iOS only because it can't be implemented using uiautomator for Android.
18
+ # secure class_name is iOS only because it can't be implemented using uiautomator for Android.
19
19
#
20
20
# find_element :text doesn't work so use XPath to find by text.
21
21
@@ -77,17 +77,6 @@ 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
- when 'button' , :button
85
- device_is_android? ? 'android.widget.Button' : 'UIAButton'
86
- else
87
- tag_name
88
- end
89
- end
90
-
91
80
# Navigate back.
92
81
# @return [void]
93
82
def back
@@ -115,115 +104,62 @@ def xpaths xpath_str
115
104
find_elements :xpath , xpath_str
116
105
end
117
106
118
- # Get the element of type tag_name at matching index.
119
- # @param tag_name [String] the tag name to find
107
+ # Get the element of type class_name at matching index.
108
+ # @param class_name [String] the class name to find
120
109
# @param index [Integer] the index
121
- # @return [Element] the found element of type tag_name
122
- def ele_index tag_name , index
123
- class_name = tag_to_class tag_name
110
+ # @return [Element] the found element of type class_name
111
+ def ele_index class_name , index
124
112
# XPath index starts at 1.
125
113
raise "#{ index } is not a valid xpath index. Must be >= 1" if index <= 0
126
114
find_element :xpath , "//#{ class_name } [#{ index } ]"
127
115
end
128
116
129
- # Get all elements exactly matching tag name
130
- # @param tag_name [String] the tag name to find
131
- # @return [Array<Element>] the found elements of type tag_name
132
- def find_eles tag_name
133
- class_name = tag_to_class tag_name
134
- @driver . find_elements :class , class_name
135
- end
136
-
137
- # Get the first tag that exactly matches tag and text.
138
- # @param tag_name [String] the tag name to match
139
- # @param text [String] the text to exactly match
140
- # @return [Element] the element of type tag exactly matching text
141
- def find_ele_by_text tag_name , text
142
- class_name = tag_to_class tag_name
143
- @driver . find_element :xpath , %Q(//#{ class_name } [@text='#{ text } '])
117
+ def string_attr_exact class_name , attr , value
118
+ %Q(//#{ class_name } [@visible="true" and @#{ attr } ='#{ value } '])
144
119
end
145
120
146
- # Get all tags that exactly match tag and text.
147
- # @param tag_name [String] the tag name to match
148
- # @param text [String] the text to exactly match
149
- # @return [Array<Element>] the elements of type tag exactly matching text
150
- def find_eles_by_text tag_name , text
151
- class_name = tag_to_class tag_name
152
- @driver . find_elements :xpath , %Q(//#{ class_name } [@text='#{ text } '])
121
+ def find_ele_by_attr class_name , attr , value
122
+ @driver . find_element :xpath , string_attr_exact ( class_name , attr , value )
153
123
end
154
124
155
- def find_ele_by_name tag_name , name
156
- class_name = tag_to_class tag_name
157
- @driver . find_element :xpath , %Q(//#{ class_name } [@name='#{ name } '])
125
+ def find_eles_by_attr class_name , attr , value
126
+ @driver . find_elements :xpath , string_attr_exact ( class_name , attr , value )
158
127
end
159
128
160
- def find_eles_by_name tag_name , name
161
- class_name = tag_to_class tag_name
162
- @driver . find_elements :xpath , %Q(//#{ class_name } [@name='#{ name } '])
129
+ def string_attr_include class_name , attr , value
130
+ %Q(//#{ class_name } [@visible="true" and contains(translate(@#{ attr } ,'#{ value . upcase } ', '#{ value } '), '#{ value } ')])
163
131
end
164
132
165
133
# Get the first tag by attribute that exactly matches value.
166
- # @param tag_name [String] the tag name to match
134
+ # @param class_name [String] the tag name to match
167
135
# @param attr [String] the attribute to compare
168
136
# @param value [String] the value of the attribute that the element must include
169
137
# @return [Element] the element of type tag who's attribute includes value
170
- def find_ele_by_attr_include tag_name , attr , value
171
- class_name = tag_to_class tag_name
172
- value . downcase!
173
- @driver . find_element :xpath , %Q(//#{ class_name } [contains(translate(@#{ attr } ,'#{ value . upcase } ', '#{ value } '), '#{ value } ')])
138
+ def find_ele_by_attr_include class_name , attr , value
139
+ @driver . find_element :xpath , string_attr_include ( class_name , attr , value )
174
140
end
175
141
176
142
# Get tags by attribute that include value.
177
- # @param tag_name [String] the tag name to match
143
+ # @param class_name [String] the tag name to match
178
144
# @param attr [String] the attribute to compare
179
145
# @param value [String] the value of the attribute that the element must include
180
146
# @return [Array<Element>] the elements of type tag who's attribute includes value
181
- def find_eles_by_attr_include tag_name , attr , value
182
- class_name = tag_to_class tag_name
183
- value . downcase!
184
- @driver . find_elements :xpath , %Q(//#{ class_name } [contains(translate(@#{ attr } ,'#{ value . upcase } ', '#{ value } '), '#{ value } ')])
185
- end
186
-
187
- # Get the first tag that includes text.
188
- # @param tag_name [String] the tag name to match
189
- # @param text [String] the text the element must include
190
- # @return [Element] the element of type tag that includes text
191
- # element.attribute(:text).include? text
192
- def find_ele_by_text_include tag_name , text
193
- find_ele_by_attr_include tag_name , :text , text
194
- end
195
-
196
- # Get the tags that include text.
197
- # @param tag_name [String] the tag name to match
198
- # @param text [String] the text the element must include
199
- # @return [Array<Element>] the elements of type tag that includes text
200
- # element.attribute(:text).include? text
201
- def find_eles_by_text_include tag_name , text
202
- find_eles_by_attr_include tag_name , :text , text
147
+ def find_eles_by_attr_include class_name , attr , value
148
+ @driver . find_elements :xpath , string_attr_include ( class_name , attr , value )
203
149
end
204
150
205
- def find_ele_by_name_include tag_name , text
206
- find_ele_by_attr_include tag_name , :name , text
207
- end
208
-
209
- def find_eles_by_name_include tag_name , text
210
- find_eles_by_attr_include tag_name , :name , text
211
- end
212
-
213
- # Get the first tag that matches tag_name
214
- # @param tag_name [String] the tag to match
151
+ # Get the first tag that matches class_name
152
+ # @param class_name [String] the tag to match
215
153
# @return [Element]
216
- def first_ele tag_name
154
+ def first_ele class_name
217
155
# XPath index starts at 1
218
- class_name = tag_to_class tag_name
219
156
find_element :xpath , "//#{ class_name } [1]"
220
157
end
221
158
222
- # Get the last tag that matches tag_name
223
- # @param tag_name [String] the tag to match
159
+ # Get the last tag that matches class_name
160
+ # @param class_name [String] the tag to match
224
161
# @return [Element]
225
- def last_ele tag_name
226
- class_name = tag_to_class tag_name
162
+ def last_ele class_name
227
163
xpath "//#{ class_name } [last()]"
228
164
end
229
165
@@ -236,7 +172,6 @@ def source
236
172
puts doc . to_xml indent : 2
237
173
end
238
174
239
-
240
175
# Returns XML string for the current page
241
176
# Same as driver.page_source
242
177
# @return [String]
@@ -288,37 +223,19 @@ def page_class
288
223
nil
289
224
end
290
225
291
- # Returns the first element that exactly matches name
226
+ # Returns the first element matching class_name
292
227
#
293
- # @param name [String] the name to exactly match
228
+ # @param class_name [String] the class_name to search for
294
229
# @return [Element]
295
- def name_exact name
296
- find_element :name , name
297
- end
298
-
299
- # Returns all elements that exactly match name
300
- #
301
- # @param name [String] the name to exactly match
302
- # @return [Array<Element>]
303
- def names_exact name
304
- find_elements :name , name
305
- end
306
-
307
- # Returns the first element matching tag_name
308
- #
309
- # @param tag_name [String] the tag_name to search for
310
- # @return [Element]
311
- def tag tag_name
312
- class_name = tag_to_class tag_name
230
+ def tag class_name
313
231
find_element :class , class_name
314
232
end
315
233
316
- # Returns all elements matching tag_name
234
+ # Returns all elements matching class_name
317
235
#
318
- # @param tag_name [String] the tag_name to search for
236
+ # @param class_name [String] the class_name to search for
319
237
# @return [Element]
320
- def tags tag_name
321
- class_name = tag_to_class tag_name
238
+ def tags class_name
322
239
find_elements :class , class_name
323
240
end
324
241
@@ -367,7 +284,7 @@ def resolve_id id
367
284
368
285
# xpath fragment helper
369
286
# example: xpath_visible_contains 'UIATextField', text
370
- def xpath_visible_contains element , value
287
+ def string_visible_contains element , value
371
288
result = [ ]
372
289
attributes = %w[ name hint label value ]
373
290
@@ -383,7 +300,15 @@ def xpath_visible_contains element, value
383
300
"//#{ element } [#{ result } ]"
384
301
end
385
302
386
- def xpath_visible_exact element , value
303
+ def xpath_visible_contains element , value
304
+ xpath string_visible_contains element , value
305
+ end
306
+
307
+ def xpaths_visible_contains element , value
308
+ xpaths string_visible_contains element , value
309
+ end
310
+
311
+ def string_visible_exact element , value
387
312
result = [ ]
388
313
attributes = %w[ name hint label value ]
389
314
@@ -396,6 +321,14 @@ def xpath_visible_exact element, value
396
321
"//#{ element } [#{ result } ]"
397
322
end
398
323
324
+ def xpath_visible_exact element , value
325
+ xpath string_visible_exact element , value
326
+ end
327
+
328
+ def xpaths_visible_exact element , value
329
+ xpaths string_visible_exact element , value
330
+ end
331
+
399
332
# Used to error when finding a single element fails.
400
333
def raise_no_element_error
401
334
raise Selenium ::WebDriver ::Error ::NoSuchElementError , 'An element could not be located on the page using the given search parameters.'
0 commit comments