Module: TestaAppiumDriver::Helpers

Included in:
Driver, Locator
Defined in:
lib/testa_appium_driver/common/helpers.rb

Instance Method Summary collapse

Instance Method Details

#extract_selectors_from_params(params = {}) ⇒ Array

noinspection RubyUnnecessaryReturnStatement,RubyUnusedLocalVariable separate selectors from given hash parameters

Parameters:

  • params (Hash) (defaults to: {})

Returns:

  • (Array)

    first element is params, second are selectors



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/testa_appium_driver/common/helpers.rb', line 211

def extract_selectors_from_params(params = {})
  selectors = params.select { |key, value| [
      :id,
      :long_clickable,
      :desc,
      :class,
      :text,
      :package,
      :checkable,
      :checked,
      :clickable,
      :enabled,
      :focusable,
      :focused,
      :index,
      :selected,
      :scrollable,

      # ios specific
      :type,
      :label,
      :x,
      :y,
      :width,
      :height,
      :visible,
      :name,
      :value,

      :image
  ].include?(key) }
  params = Hash[params.to_a - selectors.to_a]

  # default params
  params[:single] = true if params[:single].nil?
  params[:scrollable_locator] = nil if params[:scrollable_locator].nil?
  if params[:default_find_strategy].nil?
    params[:default_find_strategy] = DEFAULT_ANDROID_FIND_STRATEGY if @driver.device == :android
    params[:default_find_strategy] = DEFAULT_IOS_FIND_STRATEGY if @driver.device == :ios || @driver.device == :tvos
  end
  if params[:default_scroll_strategy].nil?
    params[:default_scroll_strategy] = DEFAULT_ANDROID_SCROLL_STRATEGY if @driver.device == :android
    params[:default_scroll_strategy] = DEFAULT_IOS_SCROLL_STRATEGY if @driver.device == :ios || @driver.device == :tvos
  end
  return params, selectors
end

#hash_to_class_chain(hash, single = true) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/testa_appium_driver/common/helpers.rb', line 162

def hash_to_class_chain(hash, single = true)
  command = "**/"

  hash[:type] = hash[:class] unless hash[:class].nil?
  hash[:label] = hash[:text] unless hash[:text].nil?
  hash[:name] = hash[:id] unless hash[:id].nil?
  if hash[:type] && hash[:type].kind_of?(String)
    command = "#{ command }#{hash[:type] }"
  else
    command = "#{command}*"
  end

  command = "#{ command }[`enabled == #{ hash[:enabled] }`]" unless hash[:enabled].nil?
  command = "#{ command }[`label == \"#{ %(#{hash[:label] }) }\"`]" if hash[:label] && hash[:label].kind_of?(String)
  command = "#{ command }[`label CONTAINS \"#{ %(#{hash[:label].source }) }\"`]" if hash[:label] && hash[:label].kind_of?(Regexp)
  command = "#{ command }[`name == \"#{ %(#{hash[:name] }) }\"`]" if hash[:name] && hash[:name].kind_of?(String)
  command = "#{ command }[`name CONTAINS \"#{ %(#{hash[:name].source }) }\"`]" if hash[:name] && hash[:name].kind_of?(Regexp)
  command = "#{ command }[`value == \"#{ %(#{hash[:value] }) }\"`]" if hash[:value] && hash[:value].kind_of?(String)
  command = "#{ command }[`value CONTAINS \"#{ %(#{hash[:value].source }) }\"`]" if hash[:value] && hash[:value].kind_of?(Regexp)
  command = "#{ command }[`visible == #{ hash[:visible] }`]" unless hash[:visible].nil?

  command += "[1]" if single

  command
end

#hash_to_uiautomator(hash, single = true) ⇒ String

supported selectors id: “com.my.package:id/myId” id: “myId” => will be converted to “com.my.package:id/myId” id: /my/ will find all elements with ids that contain my desc: “element description” desc: /ription/ will find all elements that contains ription class: “android.widget.Button” class: /Button/ will find all elements with classes that contain Button text: “Hello world” text: /ello/ will find all elements with text that contain ello package: “com.my.package” package: /my/ will find all elements with package that contains my long_clickable: true or false checkable: true or false checked: true or false clickable: true or false enabled: true or false focusable: true or false focused: true or false index: child index inside of a parent element, index starts from 0 selected: true or false scrollable: true or false

Parameters:

  • hash (Hash)

    selectors for finding elements

  • single (Boolean) (defaults to: true)

    should the command return first instance or all of matched elements

Returns:

  • (String)

    hash selectors converted to uiautomator command



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/testa_appium_driver/common/helpers.rb', line 29

def hash_to_uiautomator(hash, single = true)
  command = "new UiSelector()"

  id = resolve_id(hash[:id])
  command = "#{ command }.resourceId(\"#{ %(#{ id }) }\")" if id && id.kind_of?(String)
  command = "#{ command }.resourceIdMatches(\".*#{ %(#{ id.source }) }.*\")" if id && id.kind_of?(Regexp)
  command = "#{ command }.description(\"#{ %(#{ hash[:desc] }) }\")" if hash[:desc] && hash[:desc].kind_of?(String)
  command = "#{ command }.descriptionMatches(\".*#{ %(#{ hash[:desc].source }) }.*\")" if hash[:desc] && hash[:desc].kind_of?(Regexp)
  command = "#{ command }.className(\"#{ %(#{ hash[:class] }) }\")" if hash[:class] && hash[:class].kind_of?(String)
  command = "#{ command }.classNameMatches(\".*#{ %(#{ hash[:class].source }) }.*\")" if hash[:class] && hash[:class].kind_of?(Regexp)
  command = "#{ command }.text(\"#{ %(#{ hash[:text] }) }\")" if hash[:text] && hash[:text].kind_of?(String)
  command = "#{ command }.textMatches(\".*#{ %(#{ hash[:text].source }) }.*\")" if hash[:text] && hash[:text].kind_of?(Regexp)
  command = "#{ command }.packageName(\"#{ %(#{ hash[:package] }) }\")" if hash[:package] && hash[:package].kind_of?(String)
  command = "#{ command }.packageNameMatches(\".*#{ %(#{ hash[:package].source }) }.*\")" if hash[:package] && hash[:package].kind_of?(Regexp)

  command = "#{ command }.longClickable(#{ hash[:long_clickable] })" if hash[:long_clickable]
  command = "#{ command }.checkable(#{ hash[:checkable] })" unless hash[:checkable].nil?
  command = "#{ command }.checked(#{ hash[:checked] })" unless hash[:checked].nil?
  command = "#{ command }.clickable(#{ hash[:clickable] })" unless hash[:clickable].nil?
  command = "#{ command }.enabled(#{ hash[:enabled] })" unless hash[:enabled].nil?
  command = "#{ command }.focusable(#{ hash[:focusable] })" unless hash[:focusable].nil?
  command = "#{ command }.focused(#{ hash[:focused] })" unless hash[:focused].nil?
  command = "#{ command }.index(#{ hash[:index].to_s })" unless hash[:index].nil?
  command = "#{ command }.selected(#{ hash[:selected] })" unless hash[:selected].nil?
  command = "#{ command }.scrollable(#{ hash[:scrollable] })" unless hash[:scrollable].nil?

  command += ".instance(0)" if single

  command
end

#hash_to_xpath(device, hash, single = true) ⇒ String

supported selectors id: “com.my.package:id/myId” id: “myId” => will be converted to “com.my.package:id/myId” id: /my/ will find all elements with ids that contain my desc: “element description” desc: /ription/ will find all elements that contains ription class: “android.widget.Button” class: /Button/ will find all elements with classes that contain Button text: “Hello world” text: /ello/ will find all elements with text that contain ello package: “com.my.package” package: /my/ will find all elements with package that contains my long_clickable: true or false checkable: true or false checked: true or false clickable: true or false enabled: true or false focusable: true or false focused: true or false index: child index inside of a parent element, index starts from 0 selected: true or false scrollable: true or false

Parameters:

  • hash (Hash)

    selectors for finding elements

  • single (Boolean) (defaults to: true)

    should the command return first instance or all of matched elements

Returns:

  • (String)

    hash selectors converted to xpath command



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/testa_appium_driver/common/helpers.rb', line 86

def hash_to_xpath(device, hash, single = true)
  for_android = device == :android


  command = "//"



  if for_android
    id = resolve_id(hash[:id])
    if hash[:class] && hash[:class].kind_of?(String)
      command = "#{ command }#{hash[:class] }"
    elsif hash[:class] && hash[:class].kind_of?(Regexp)
      command = "#{ command}*[contains(@class, \"#{ %(#{hash[:class].source }) }\")]"
    else
      command = "#{command}*"
    end

    # TODO: with new uiautomator sever you can use matches to look with regex instead of contains
    command = "#{ command }[@resource-id=\"#{ %(#{ id }) }\"]" if  id &&  id.kind_of?(String)
    command = "#{ command }[contains(@resource-id, \"#{ %(#{ id.source }) }\")]" if  id &&  id.kind_of?(Regexp)
    command = "#{ command }[@content-desc=\"#{ %(#{hash[:desc] }) }\"]" if hash[:desc] && hash[:desc].kind_of?(String)
    command = "#{ command }[contains(@content-desc, \"#{ %(#{hash[:desc].source }) }\")]" if hash[:desc] && hash[:desc].kind_of?(Regexp)
    command = "#{ command }[contains(@class, \"#{ %(#{hash[:class].source }) }\")]" if hash[:class] && hash[:class].kind_of?(Regexp)
    command = "#{ command }[@text=\"#{ %(#{hash[:text] }) }\"]" if hash[:text] && hash[:text].kind_of?(String)
    command = "#{ command }[contains(@text, \"#{ %(#{hash[:text].source }) }\")]" if hash[:text] && hash[:text].kind_of?(Regexp)
    command = "#{ command }[@package=\"#{ %(#{hash[:package] }) }\"]" if hash[:package] && hash[:package].kind_of?(String)
    command = "#{ command }[contains=(@package, \"#{ %(#{hash[:package].source }) }\")]" if hash[:package] && hash[:package].kind_of?(Regexp)
    command = "#{ command }[@long-clickable=\"#{ hash[:long_clickable] }\"]" if hash[:long_clickable]
    command = "#{ command }[@checkable=\"#{ hash[:checkable] }\"]" unless hash[:checkable].nil?
    command = "#{ command }[@checked=\"#{ hash[:checked] }\"]" unless hash[:checked].nil?
    command = "#{ command }[@clickable=\"#{ hash[:clickable] }\"]" unless hash[:clickable].nil?
    command = "#{ command }[@enabled=\"#{ hash[:enabled] }\"]" unless hash[:enabled].nil?
    command = "#{ command }[@focusable=\"#{ hash[:focusable] }\"]" unless hash[:focusable].nil?
    command = "#{ command }[@focused=\"#{ hash[:focused] }\"]" unless hash[:focused].nil?
    command = "#{ command }[@index=\"#{ hash[:index] }\"]" unless hash[:index].nil?
    command = "#{ command }[@selected=\"#{ hash[:selected] }\"]" unless hash[:selected].nil?

    # it seems like you cannot query by scrollable
    # command = "#{ command }[@scrollable=\"#{ hash[:scrollable] }\"]" unless hash[:scrollable].nil?
  else

    hash[:type] = hash[:class] unless hash[:class].nil?
    if hash[:type] && hash[:type].kind_of?(String)
      command = "#{ command }#{hash[:type] }"
    elsif hash[:type] && hash[:type].kind_of?(Regexp)
      command = "#{ command}*[contains(@type, \"#{ %(#{hash[:type].source }) }\")]"
    else
      command = "#{command}*"
    end


    #  # ios specific
    hash[:label] = hash[:text] unless hash[:text].nil?
    hash[:name] = hash[:id] unless hash[:id].nil?

    command = "#{ command }[@enabled=\"#{ hash[:enabled] }\"]" unless hash[:enabled].nil?
    command = "#{ command }[@label=\"#{ %(#{hash[:label] }) }\"]" if hash[:label] && hash[:label].kind_of?(String)
    command = "#{ command }[contains(@label, \"#{ %(#{hash[:label].source }) }\")]" if hash[:label] && hash[:label].kind_of?(Regexp)
    command = "#{ command }[@name=\"#{ %(#{hash[:name] }) }\"]" if hash[:name] && hash[:name].kind_of?(String)
    command = "#{ command }[contains(@name, \"#{ %(#{hash[:name].source }) }\")]" if hash[:name] && hash[:name].kind_of?(Regexp)
    command = "#{ command }[@value=\"#{ %(#{hash[:value] }) }\"]" if hash[:value] && hash[:value].kind_of?(String)
    command = "#{ command }[contains(@value, \"#{ %(#{hash[:value].source }) }\")]" if hash[:value] && hash[:value].kind_of?(Regexp)
    command = "#{ command }[@width=\"#{ hash[:width] }\"]" unless hash[:width].nil?
    command = "#{ command }[@height=\"#{ hash[:height] }\"]" unless hash[:height].nil?
    command = "#{ command }[@visible=\"#{ hash[:visible] }\"]" unless hash[:visible].nil?
    command = "#{ command }[@index=\"#{ hash[:index] }\"]" unless hash[:index].nil?
  end


  command += "[1]" if single

  command
end

#is_scrollable_selector?(selectors, single) ⇒ Boolean

check if selectors are for a scrollable element

Parameters:

  • single (Boolean)

    should the command return first instance or all of matched elements

  • selectors (Hash)

    for fetching elements

Returns:

  • (Boolean)

    true if element has scrollable attribute true or is class one of (RecyclerView, HorizontalScrollView, ScrollView, ListView)



192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/testa_appium_driver/common/helpers.rb', line 192

def is_scrollable_selector?(selectors, single)
  return false unless single
  return true if selectors[:scrollable]
  if selectors[:class] == "androidx.recyclerview.widget.RecyclerView" ||
      selectors[:class] == "android.widget.HorizontalScrollView" ||
      selectors[:class] == "android.widget.ScrollView" ||
      selectors[:class] == "android.widget.ListView"
    return true
  elsif selectors[:type] == "XCUIElementTypeScrollView" ||
    selectors[:type] == "XCUIElementTypeTable"
    return true
  end
  false
end

#resolve_id(id) ⇒ Object



259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/testa_appium_driver/common/helpers.rb', line 259

def resolve_id(id)
  if id && id.kind_of?(String) && !id.match?(/.*:id\//)
    # shorthand ids like myId make full ids => my.app.package:id/myId
    if id[0] == "="
      return id[1..-1]
    else
      return "#{@driver.current_package}:id/#{id}"
    end
  else
    id
  end
end