Skip to content

Commit b26d77d

Browse files
Move iOS specific helpers out of common
1 parent a97c1cd commit b26d77d

File tree

2 files changed

+132
-133
lines changed

2 files changed

+132
-133
lines changed

lib/appium_lib/common/helper.rb

-122
Original file line numberDiff line numberDiff line change
@@ -103,65 +103,6 @@ def xpaths xpath_str
103103
find_elements :xpath, xpath_str
104104
end
105105

106-
# Get the element of type class_name at matching index.
107-
# @param class_name [String] the class name to find
108-
# @param index [Integer] the index
109-
# @return [Element] the found element of type class_name
110-
def ele_index class_name, index
111-
# XPath index starts at 1.
112-
raise "#{index} is not a valid xpath index. Must be >= 1" if index <= 0
113-
find_element :xpath, %Q(//#{class_name}[@visible="true"][#{index}])
114-
end
115-
116-
def string_attr_exact class_name, attr, value
117-
%Q(//#{class_name}[@visible="true" and @#{attr}='#{value}'])
118-
end
119-
120-
def find_ele_by_attr class_name, attr, value
121-
@driver.find_element :xpath, string_attr_exact(class_name, attr, value)
122-
end
123-
124-
def find_eles_by_attr class_name, attr, value
125-
@driver.find_elements :xpath, string_attr_exact(class_name, attr, value)
126-
end
127-
128-
def string_attr_include class_name, attr, value
129-
%Q(//#{class_name}[@visible="true" and contains(translate(@#{attr},'#{value.upcase}', '#{value}'), '#{value}')])
130-
end
131-
132-
# Get the first tag by attribute that exactly matches value.
133-
# @param class_name [String] the tag name to match
134-
# @param attr [String] the attribute to compare
135-
# @param value [String] the value of the attribute that the element must include
136-
# @return [Element] the element of type tag who's attribute includes value
137-
def find_ele_by_attr_include class_name, attr, value
138-
@driver.find_element :xpath, string_attr_include(class_name, attr, value)
139-
end
140-
141-
# Get tags by attribute that include value.
142-
# @param class_name [String] the tag name to match
143-
# @param attr [String] the attribute to compare
144-
# @param value [String] the value of the attribute that the element must include
145-
# @return [Array<Element>] the elements of type tag who's attribute includes value
146-
def find_eles_by_attr_include class_name, attr, value
147-
@driver.find_elements :xpath, string_attr_include(class_name, attr, value)
148-
end
149-
150-
# Get the first tag that matches class_name
151-
# @param class_name [String] the tag to match
152-
# @return [Element]
153-
def first_ele class_name
154-
# XPath index starts at 1
155-
find_element :xpath, %Q(//#{class_name}[@visible="true"][1])
156-
end
157-
158-
# Get the last tag that matches class_name
159-
# @param class_name [String] the tag to match
160-
# @return [Element]
161-
def last_ele class_name
162-
xpath %Q(//#{class_name}[@visible="true"][last()])
163-
end
164-
165106
# Prints xml of the current page
166107
# @return [void]
167108
def source
@@ -222,22 +163,6 @@ def page_class
222163
nil
223164
end
224165

225-
# Returns the first element matching class_name
226-
#
227-
# @param class_name [String] the class_name to search for
228-
# @return [Element]
229-
def tag class_name
230-
xpath %Q(//#{class_name}[@visible="true"])
231-
end
232-
233-
# Returns all elements matching class_name
234-
#
235-
# @param class_name [String] the class_name to search for
236-
# @return [Element]
237-
def tags class_name
238-
xpaths %Q(//#{class_name}[@visible="true"])
239-
end
240-
241166
# Converts pixel values to window relative values
242167
#
243168
# ```ruby
@@ -281,53 +206,6 @@ def resolve_id id
281206
@strings_xml[id]
282207
end
283208

284-
# xpath fragment helper
285-
# example: xpath_visible_contains 'UIATextField', text
286-
def string_visible_include element, value
287-
result = []
288-
attributes = %w[name hint label value]
289-
290-
value_up = value.upcase
291-
value_down = value.downcase
292-
293-
attributes.each do |attribute|
294-
result << %Q(contains(translate(@#{attribute},"#{value_up}","#{value_down}"), "#{value_down}"))
295-
end
296-
297-
result = result.join(' or ')
298-
result = %Q(@visible="true" and (#{result}))
299-
"//#{element}[#{result}]"
300-
end
301-
302-
def xpath_visible_contains element, value
303-
xpath string_visible_include element, value
304-
end
305-
306-
def xpaths_visible_contains element, value
307-
xpaths string_visible_include element, value
308-
end
309-
310-
def string_visible_exact element, value
311-
result = []
312-
attributes = %w[name hint label value]
313-
314-
attributes.each do |attribute|
315-
result << %Q(@#{attribute}="#{value}")
316-
end
317-
318-
result = result.join(' or ')
319-
result = %Q(@visible="true" and (#{result}))
320-
"//#{element}[#{result}]"
321-
end
322-
323-
def xpath_visible_exact element, value
324-
xpath string_visible_exact element, value
325-
end
326-
327-
def xpaths_visible_exact element, value
328-
xpaths string_visible_exact element, value
329-
end
330-
331209
# Used to error when finding a single element fails.
332210
def raise_no_element_error
333211
raise Selenium::WebDriver::Error::NoSuchElementError, 'An element could not be located on the page using the given search parameters.'

lib/appium_lib/ios/helper.rb

+132-11
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def ios_password length=1
1717
# @param element [Object] the element to search. omit to search everything
1818
# @return [String]
1919
def get_page element=source_window(0), class_name=nil
20-
lazy_load_strings
20+
lazy_load_strings # populate @strings_xml
2121

2222
# @private
2323
def empty ele
@@ -42,12 +42,12 @@ def fix_space s
4242
end
4343

4444
unless empty(element) || element['visible'] == false
45-
name = fix_space element['name']
46-
label = fix_space element['label']
47-
value = fix_space element['value']
48-
hint = fix_space element['hint']
45+
name = fix_space element['name']
46+
label = fix_space element['label']
47+
value = fix_space element['value']
48+
hint = fix_space element['hint']
4949
visible = fix_space element['visible']
50-
type = fix_space element['type']
50+
type = fix_space element['type']
5151

5252
# if class_name is set, mark non-matches as invisible
5353
visible = (type == class_name).to_s if class_name
@@ -99,7 +99,7 @@ def fix_space s
9999
# @return [void]
100100
def page opts={}
101101
window_number = opts.fetch :window, -1
102-
class_name = opts.fetch :class, nil
102+
class_name = opts.fetch :class, nil
103103

104104
if window_number == -1
105105
# if the 0th window has no children, find the next window that does.
@@ -135,11 +135,10 @@ def page_window window_number=0
135135
# @param id [String] the id to search for
136136
# @return [Element]
137137
def id id
138-
lazy_load_strings
139-
value = @strings_xml[id]
138+
value = resolve_id id
140139
raise "Invalid id `#{id}`" unless value
141-
exact = string_visible_exact '*', value
142-
contains = string_visible_include '*', value
140+
exact = string_visible_exact '*', value
141+
contains = string_visible_include '*', value
143142
xpath "#{exact} | #{contains}"
144143
end
145144

@@ -149,5 +148,127 @@ def ios_version
149148
ios_version = execute_script 'UIATarget.localTarget().systemVersion()'
150149
ios_version.split('.').map { |e| e.to_i }
151150
end
151+
152+
# Get the element of type class_name at matching index.
153+
# @param class_name [String] the class name to find
154+
# @param index [Integer] the index
155+
# @return [Element] the found element of type class_name
156+
def ele_index class_name, index
157+
# XPath index starts at 1.
158+
raise "#{index} is not a valid xpath index. Must be >= 1" if index <= 0
159+
find_element :xpath, %Q(//#{class_name}[@visible="true"][#{index}])
160+
end
161+
162+
def string_attr_exact class_name, attr, value
163+
%Q(//#{class_name}[@visible="true" and @#{attr}='#{value}'])
164+
end
165+
166+
def find_ele_by_attr class_name, attr, value
167+
@driver.find_element :xpath, string_attr_exact(class_name, attr, value)
168+
end
169+
170+
def find_eles_by_attr class_name, attr, value
171+
@driver.find_elements :xpath, string_attr_exact(class_name, attr, value)
172+
end
173+
174+
def string_attr_include class_name, attr, value
175+
%Q(//#{class_name}[@visible="true" and contains(translate(@#{attr},'#{value.upcase}', '#{value}'), '#{value}')])
176+
end
177+
178+
# Get the first tag by attribute that exactly matches value.
179+
# @param class_name [String] the tag name to match
180+
# @param attr [String] the attribute to compare
181+
# @param value [String] the value of the attribute that the element must include
182+
# @return [Element] the element of type tag who's attribute includes value
183+
def find_ele_by_attr_include class_name, attr, value
184+
@driver.find_element :xpath, string_attr_include(class_name, attr, value)
185+
end
186+
187+
# Get tags by attribute that include value.
188+
# @param class_name [String] the tag name to match
189+
# @param attr [String] the attribute to compare
190+
# @param value [String] the value of the attribute that the element must include
191+
# @return [Array<Element>] the elements of type tag who's attribute includes value
192+
def find_eles_by_attr_include class_name, attr, value
193+
@driver.find_elements :xpath, string_attr_include(class_name, attr, value)
194+
end
195+
196+
# Get the first tag that matches class_name
197+
# @param class_name [String] the tag to match
198+
# @return [Element]
199+
def first_ele class_name
200+
# XPath index starts at 1
201+
find_element :xpath, %Q(//#{class_name}[@visible="true"][1])
202+
end
203+
204+
# Get the last tag that matches class_name
205+
# @param class_name [String] the tag to match
206+
# @return [Element]
207+
def last_ele class_name
208+
xpath %Q(//#{class_name}[@visible="true"][last()])
209+
end
210+
211+
# Returns the first element matching class_name
212+
#
213+
# @param class_name [String] the class_name to search for
214+
# @return [Element]
215+
def tag class_name
216+
xpath %Q(//#{class_name}[@visible="true"])
217+
end
218+
219+
# Returns all elements matching class_name
220+
#
221+
# @param class_name [String] the class_name to search for
222+
# @return [Element]
223+
def tags class_name
224+
xpaths %Q(//#{class_name}[@visible="true"])
225+
end
226+
227+
# xpath fragment helper
228+
# example: xpath_visible_contains 'UIATextField', text
229+
def string_visible_include element, value
230+
result = []
231+
attributes = %w[name hint label value]
232+
233+
value_up = value.upcase
234+
value_down = value.downcase
235+
236+
attributes.each do |attribute|
237+
result << %Q(contains(translate(@#{attribute},"#{value_up}","#{value_down}"), "#{value_down}"))
238+
end
239+
240+
result = result.join(' or ')
241+
result = %Q(@visible="true" and (#{result}))
242+
"//#{element}[#{result}]"
243+
end
244+
245+
def xpath_visible_contains element, value
246+
xpath string_visible_include element, value
247+
end
248+
249+
def xpaths_visible_contains element, value
250+
xpaths string_visible_include element, value
251+
end
252+
253+
def string_visible_exact element, value
254+
result = []
255+
attributes = %w[name hint label value]
256+
257+
attributes.each do |attribute|
258+
result << %Q(@#{attribute}="#{value}")
259+
end
260+
261+
result = result.join(' or ')
262+
result = %Q(@visible="true" and (#{result}))
263+
"//#{element}[#{result}]"
264+
end
265+
266+
def xpath_visible_exact element, value
267+
xpath string_visible_exact element, value
268+
end
269+
270+
def xpaths_visible_exact element, value
271+
xpaths string_visible_exact element, value
272+
end
152273
end # module Ios
153274
end # module Appium

0 commit comments

Comments
 (0)