Module: Appium::Android
- Defined in:
- lib/appium_lib/android/android.rb,
lib/appium_lib/android/espresso.rb,
lib/appium_lib/android/element/text.rb,
lib/appium_lib/android/uiautomator2.rb,
lib/appium_lib/android/common/helper.rb,
lib/appium_lib/android/element/alert.rb,
lib/appium_lib/android/element/button.rb,
lib/appium_lib/android/element/generic.rb,
lib/appium_lib/android/espresso/bridge.rb,
lib/appium_lib/android/espresso/helper.rb,
lib/appium_lib/android/espresso/element.rb,
lib/appium_lib/android/element/textfield.rb,
lib/appium_lib/android/uiautomator2/bridge.rb,
lib/appium_lib/android/uiautomator2/helper.rb,
lib/appium_lib/android/uiautomator2/element.rb,
lib/appium_lib/android/common/command/command.rb,
lib/appium_lib/android/espresso/element/button.rb,
lib/appium_lib/android/espresso/element/generic.rb,
lib/appium_lib/android/uiautomator2/element/button.rb
Defined Under Namespace
Modules: Command, Espresso, Uiautomator2 Classes: AndroidElements, Bridge
Constant Summary collapse
- TextView =
'android.widget.TextView'.freeze
- Button =
'android.widget.Button'.freeze
- ImageButton =
'android.widget.ImageButton'.freeze
- EditText =
'android.widget.EditText'.freeze
Instance Method Summary collapse
-
#alert_accept ⇒ void
Accept the alert.
-
#alert_accept_text ⇒ String
Get the text of the alert’s accept button.
-
#alert_click(value) ⇒ void
Click the first alert button that contains value or by index.
-
#alert_dismiss ⇒ void
Dismiss the alert.
-
#alert_dismiss_text ⇒ String
Get the text of the alert’s dismiss button.
-
#button(value) ⇒ Button
Find the first button that contains value or by index.
-
#button_exact(value) ⇒ Button
Find the first button that exactly matches value.
-
#buttons(value = false) ⇒ Array<Button>
Find all buttons containing value.
-
#buttons_exact(value) ⇒ Array<Button>
Find all buttons that exactly match value.
-
#complex_find_contains(class_name, value) ⇒ Element
Find the first element that contains value.
-
#complex_find_exact(class_name, value) ⇒ Element
Find the first element exactly matching value.
-
#complex_finds_contains(class_name, value) ⇒ Array<Element>
Find all elements containing value.
-
#complex_finds_exact(class_name, value) ⇒ Element
Find all elements exactly matching value.
-
#ele_index(class_name, index) ⇒ Element
Find the element of type class_name at matching index.
-
#find(value) ⇒ Element
Find the first element containing value.
-
#find_exact(value) ⇒ Element
Find the first element exactly matching value.
-
#finds(value) ⇒ Array<Element>
Find all elements containing value.
-
#finds_exact(value) ⇒ Array<Element>
Find all elements exactly matching value.
-
#first_button ⇒ Button
Find the first button.
-
#first_ele(class_name) ⇒ Element
Find the first element that matches class_name.
-
#first_text ⇒ TextView
Find the first TextView.
-
#first_textfield ⇒ EditText
Find the first EditText.
-
#get_android_inspect(class_name = false) ⇒ String
Android only.
-
#id(id) ⇒ Element
Find the first matching element by id.
-
#ids(id) ⇒ Element
Find all matching elements by id.
-
#last_button ⇒ Button
Find the last button.
-
#last_ele(class_name) ⇒ Element
Find the last element that matches class_name.
-
#last_text ⇒ TextView
Find the last TextView.
-
#last_textfield ⇒ EditText
Find the last EditText.
-
#page(opts = {}) ⇒ void
Intended for use with console.
-
#resource_id(string, on_match) ⇒ String
Detects if the string represents a resourceId resourceId is only supported on API >= 18 devices.
-
#scroll_to(text, scrollable_index = 0) ⇒ Element
Scroll to the first element containing target text or description.
-
#scroll_to_exact(text, scrollable_index = 0) ⇒ Element
Scroll to the first element with the exact target text or description.
- #scroll_uiselector(content, index = 0) ⇒ Object
-
#string_visible_contains(class_name, value) ⇒ String
Returns a string that matches the first element that contains value For automationName is Appium example: string_visible_contains ‘UIATextField’, ‘sign in’ note for XPath: github.com/appium/ruby_lib/pull/561.
-
#string_visible_contains_xpath(class_name, value) ⇒ String
Returns a string that matches the first element that contains value For automationName is uiautomator2 example: string_visible_contains_xpath ‘UIATextField’, ‘sign in’ note for XPath: github.com/appium/ruby_lib/pull/561.
-
#string_visible_exact(class_name, value) ⇒ String
Create an string to exactly match the first element with target value.
-
#string_visible_exact_xpath(class_name, value) ⇒ String
Create an string to exactly match the first element with target value For automationName is uiautomator2.
-
#tag(class_name) ⇒ Element
Find the first element of type class_name.
-
#tags(class_name) ⇒ Element
Find all elements of type class_name.
-
#text(value) ⇒ TextView
Find the first TextView that contains value or by index.
-
#text_exact(value) ⇒ TextView
Find the first TextView that exactly matches value.
-
#textfield(value) ⇒ EditText
Find the first EditText that contains value or by index.
-
#textfield_exact(value) ⇒ EditText
Find the first EditText that exactly matches value.
-
#textfields(value = false) ⇒ Array<EditText>
Find all EditTexts containing value.
-
#textfields_exact(value) ⇒ Array<EditText>
Find all EditTexts that exactly match value.
-
#texts(value = false) ⇒ Array<TextView>
Find all TextViews containing value.
-
#texts_exact(value) ⇒ Array<TextView>
Find all TextViews that exactly match value.
Instance Method Details
#alert_accept ⇒ void
This method returns an undefined value.
Accept the alert. The last button is considered “accept.”
13 14 15 |
# File 'lib/appium_lib/android/element/alert.rb', line 13 def alert_accept .click end |
#alert_accept_text ⇒ String
Get the text of the alert’s accept button. The last button is considered “accept.”
20 21 22 |
# File 'lib/appium_lib/android/element/alert.rb', line 20 def alert_accept_text .text end |
#alert_click(value) ⇒ void
This method returns an undefined value.
Click the first alert button that contains value or by index.
6 7 8 |
# File 'lib/appium_lib/android/element/alert.rb', line 6 def alert_click(value) (value).click end |
#alert_dismiss ⇒ void
This method returns an undefined value.
Dismiss the alert. The first button is considered “dismiss.”
27 28 29 |
# File 'lib/appium_lib/android/element/alert.rb', line 27 def alert_dismiss .click end |
#alert_dismiss_text ⇒ String
Get the text of the alert’s dismiss button. The first button is considered “dismiss.”
34 35 36 |
# File 'lib/appium_lib/android/element/alert.rb', line 34 def alert_dismiss_text .text end |
#button(value) ⇒ Button
Find the first button that contains value or by index. If int then the button at that index is returned.
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/appium_lib/android/element/button.rb', line 10 def (value) # Don't use ele_index because that only works on one element type. # Android needs to combine button and image button to match iOS. if value.is_a? Numeric index = value raise "#{index} is not a valid index. Must be >= 1" if index <= 0 # 1 indexed return find_element :uiautomator, (index: index) end find_element :uiautomator, (value) end |
#button_exact(value) ⇒ Button
Find the first button that exactly matches value.
58 59 60 |
# File 'lib/appium_lib/android/element/button.rb', line 58 def (value) find_element :uiautomator, (value) end |
#buttons(value = false) ⇒ Array<Button>
Find all buttons containing value. If value is omitted, all buttons are returned.
28 29 30 31 32 |
# File 'lib/appium_lib/android/element/button.rb', line 28 def (value = false) return find_elements :uiautomator, unless value find_elements :uiautomator, (value) end |
#buttons_exact(value) ⇒ Array<Button>
Find all buttons that exactly match value.
65 66 67 |
# File 'lib/appium_lib/android/element/button.rb', line 65 def (value) find_elements :uiautomator, (value) end |
#complex_find_contains(class_name, value) ⇒ Element
Find the first element that contains value
256 257 258 |
# File 'lib/appium_lib/android/common/helper.rb', line 256 def complex_find_contains(class_name, value) find_element :uiautomator, string_visible_contains(class_name, value) end |
#complex_find_exact(class_name, value) ⇒ Element
Find the first element exactly matching value
306 307 308 |
# File 'lib/appium_lib/android/common/helper.rb', line 306 def complex_find_exact(class_name, value) find_element :uiautomator, string_visible_exact(class_name, value) end |
#complex_finds_contains(class_name, value) ⇒ Array<Element>
Find all elements containing value
264 265 266 |
# File 'lib/appium_lib/android/common/helper.rb', line 264 def complex_finds_contains(class_name, value) find_elements :uiautomator, string_visible_contains(class_name, value) end |
#complex_finds_exact(class_name, value) ⇒ Element
Find all elements exactly matching value
314 315 316 |
# File 'lib/appium_lib/android/common/helper.rb', line 314 def complex_finds_exact(class_name, value) find_elements :uiautomator, string_visible_exact(class_name, value) end |
#ele_index(class_name, index) ⇒ Element
Find the element of type class_name at matching index.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/appium_lib/android/common/helper.rb', line 133 def ele_index(class_name, index) results = (class_name) if index == 'last()' index = results.length index -= 1 if index >= 0 else raise 'Index must be >= 1' unless index >= 1 index -= 1 if index >= 1 end # uiautomator has issues with index/instance so calculate the index # client side. results[index] end |
#find(value) ⇒ Element
Find the first element containing value
6 7 8 |
# File 'lib/appium_lib/android/element/generic.rb', line 6 def find(value) complex_find_contains '*', value end |
#find_exact(value) ⇒ Element
Find the first element exactly matching value
20 21 22 |
# File 'lib/appium_lib/android/element/generic.rb', line 20 def find_exact(value) complex_find_exact '*', value end |
#finds(value) ⇒ Array<Element>
Find all elements containing value
13 14 15 |
# File 'lib/appium_lib/android/element/generic.rb', line 13 def finds(value) complex_finds_contains '*', value end |
#finds_exact(value) ⇒ Array<Element>
Find all elements exactly matching value
27 28 29 |
# File 'lib/appium_lib/android/element/generic.rb', line 27 def finds_exact(value) complex_finds_exact '*', value end |
#first_button ⇒ Button
Find the first button.
36 37 38 |
# File 'lib/appium_lib/android/element/button.rb', line 36 def find_element :uiautomator, (button_index: 0, image_button_index: 0) end |
#first_ele(class_name) ⇒ Element
Find the first element that matches class_name
152 153 154 |
# File 'lib/appium_lib/android/common/helper.rb', line 152 def first_ele(class_name) tag(class_name) end |
#first_text ⇒ TextView
Find the first TextView.
28 29 30 |
# File 'lib/appium_lib/android/element/text.rb', line 28 def first_text first_ele TextView end |
#first_textfield ⇒ EditText
Find the first EditText.
27 28 29 |
# File 'lib/appium_lib/android/element/textfield.rb', line 27 def first_textfield first_ele EditText end |
#get_android_inspect(class_name = false) ⇒ String
Android only. Returns a string containing interesting elements. The text, content description, and id are returned. if false (default) then all classes will be inspected
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/appium_lib/android/common/helper.rb', line 81 def get_android_inspect(class_name = false) source = get_source doctype_string = '<!doctyp' source_header = source[0..doctype_string.length].downcase source_is_html = source_header.start_with?(doctype_string, '<html') parser = if source_is_html # parse html from webview @android_html_parser ||= Nokogiri::HTML::SAX::Parser.new(Appium::Common::HTMLElements.new) else @android_native_parser ||= Nokogiri::XML::SAX::Parser.new(AndroidElements.new) end parser.document.reset # ensure document is reset before parsing parser.document.filter = class_name parser.parse source result = parser.document.result parser.document.reset # clean up any created objects after parsing result end |
#id(id) ⇒ Element
Find the first matching element by id
116 117 118 119 |
# File 'lib/appium_lib/android/common/helper.rb', line 116 def id(id) # Android auto resolves strings.xml ids find_element :id, id end |
#ids(id) ⇒ Element
Find all matching elements by id
124 125 126 127 |
# File 'lib/appium_lib/android/common/helper.rb', line 124 def ids(id) # Android auto resolves strings.xml ids find_elements :id, id end |
#last_button ⇒ Button
Find the last button.
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/appium_lib/android/element/button.rb', line 42 def # uiautomator index doesn't support last # and it's 0 indexed = (Button).length -= 1 if > 0 = (ImageButton).length -= 1 if > 0 find_element :uiautomator, (button_index: , image_button_index: ) end |
#last_ele(class_name) ⇒ Element
Find the last element that matches class_name
159 160 161 |
# File 'lib/appium_lib/android/common/helper.rb', line 159 def last_ele(class_name) ele_index class_name, 'last()' end |
#last_text ⇒ TextView
Find the last TextView.
34 35 36 |
# File 'lib/appium_lib/android/element/text.rb', line 34 def last_text last_ele TextView end |
#last_textfield ⇒ EditText
Find the last EditText.
33 34 35 |
# File 'lib/appium_lib/android/element/textfield.rb', line 33 def last_textfield last_ele EditText end |
#page(opts = {}) ⇒ void
This method returns an undefined value.
Intended for use with console. Inspects and prints the current page. Will return XHTML for Web contexts because of a quirk with Nokogiri. if nil (default) then all classes will be inspected
107 108 109 110 111 |
# File 'lib/appium_lib/android/common/helper.rb', line 107 def page(opts = {}) class_name = opts.is_a?(Hash) ? opts.fetch(:class, nil) : opts puts get_android_inspect class_name nil end |
#resource_id(string, on_match) ⇒ String
Detects if the string represents a resourceId resourceId is only supported on API >= 18 devices
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/appium_lib/android/common/helper.rb', line 188 def resource_id(string, on_match) return '' unless string # unquote the string # "com.example.Test:id/enter" -> com.example.Test:id/enter unquote = string.match(/"(.+)"/) string = unquote[1] if unquote # java_package : type / name # # com.example.Test:id/enter # # ^[a-zA-Z_] - Java package must start with letter or underscore # [a-zA-Z0-9\._]* - Java package may contain letters, numbers, periods and underscores # : - : ends the package and starts the type # [^\/]+ - type is made up of at least one non-/ characters # \\/ - / ends the type and starts the name # [\S]+$ - the name contains at least one non-space character and then the line is ended resource_id = /^[a-zA-Z_][a-zA-Z0-9\._]*:[^\/]+\/[\S]+$/ string.match(resource_id) ? on_match : '' end |
#scroll_to(text, scrollable_index = 0) ⇒ Element
Scroll to the first element containing target text or description.
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/appium_lib/android/element/generic.rb', line 40 def scroll_to(text, scrollable_index = 0) text = %("#{text}") rid = resource_id(text, "new UiSelector().resourceId(#{text});") args = rid.empty? ? ["new UiSelector().textContains(#{text})", "new UiSelector().descriptionContains(#{text})"] : [rid] args.each_with_index do |arg, index| begin elem = find_element :uiautomator, scroll_uiselector(arg, scrollable_index) return elem rescue StandardError => e raise e if index == args.size - 1 end end end |
#scroll_to_exact(text, scrollable_index = 0) ⇒ Element
Scroll to the first element with the exact target text or description.
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/appium_lib/android/element/generic.rb', line 58 def scroll_to_exact(text, scrollable_index = 0) text = %("#{text}") rid = resource_id(text, "new UiSelector().resourceId(#{text});") args = rid.empty? ? ["new UiSelector().text(#{text})", "new UiSelector().description(#{text})"] : [rid] args.each_with_index do |arg, index| begin elem = find_element :uiautomator, scroll_uiselector(arg, scrollable_index) return elem rescue StandardError => e raise e if index == args.size - 1 end end end |
#scroll_uiselector(content, index = 0) ⇒ Object
32 33 34 |
# File 'lib/appium_lib/android/element/generic.rb', line 32 def scroll_uiselector(content, index = 0) "new UiScrollable(new UiSelector().scrollable(true).instance(#{index})).scrollIntoView(#{content}.instance(0));" end |
#string_visible_contains(class_name, value) ⇒ String
Returns a string that matches the first element that contains value For automationName is Appium example: string_visible_contains ‘UIATextField’, ‘sign in’ note for XPath: github.com/appium/ruby_lib/pull/561
238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/appium_lib/android/common/helper.rb', line 238 def string_visible_contains(class_name, value) value = %("#{value}") if class_name == '*' return (resource_id(value, "new UiSelector().resourceId(#{value});") + "new UiSelector().descriptionContains(#{value});" \ "new UiSelector().textContains(#{value});") end class_name = %("#{class_name}") resource_id(value, "new UiSelector().className(#{class_name}).resourceId(#{value});") + "new UiSelector().className(#{class_name}).descriptionContains(#{value});" \ "new UiSelector().className(#{class_name}).textContains(#{value});" end |
#string_visible_contains_xpath(class_name, value) ⇒ String
Returns a string that matches the first element that contains value For automationName is uiautomator2 example: string_visible_contains_xpath ‘UIATextField’, ‘sign in’ note for XPath: github.com/appium/ruby_lib/pull/561
218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/appium_lib/android/common/helper.rb', line 218 def string_visible_contains_xpath(class_name, value) r_id = resource_id(value, " or @resource-id='#{value}'") if class_name == '*' return "//*[contains(translate(@text,'#{value.upcase}', '#{value}'), '#{value}')" \ " or contains(translate(@content-desc,'#{value.upcase}', '#{value}'), '#{value}')" + r_id + ']' end "//#{class_name}[contains(translate(@text,'#{value.upcase}', '#{value}'), '#{value}')" \ " or contains(translate(@content-desc,'#{value.upcase}', '#{value}'), '#{value}')" + r_id + ']' end |
#string_visible_exact(class_name, value) ⇒ String
Create an string to exactly match the first element with target value
287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/appium_lib/android/common/helper.rb', line 287 def string_visible_exact(class_name, value) value = %("#{value}") if class_name == '*' return (resource_id(value, "new UiSelector().resourceId(#{value});") + "new UiSelector().description(#{value});" \ "new UiSelector().text(#{value});") end class_name = %("#{class_name}") resource_id(value, "new UiSelector().className(#{class_name}).resourceId(#{value});") + "new UiSelector().className(#{class_name}).description(#{value});" \ "new UiSelector().className(#{class_name}).text(#{value});" end |
#string_visible_exact_xpath(class_name, value) ⇒ String
Create an string to exactly match the first element with target value For automationName is uiautomator2
274 275 276 277 278 279 280 |
# File 'lib/appium_lib/android/common/helper.rb', line 274 def string_visible_exact_xpath(class_name, value) r_id = resource_id(value, " or @resource-id='#{value}'") return "//*[@text='#{value}' or @content-desc='#{value}'" + r_id + ']' if class_name == '*' "//#{class_name}[@text='#{value}' or @content-desc='#{value}'" + r_id + ']' end |
#tag(class_name) ⇒ Element
Find the first element of type class_name
167 168 169 |
# File 'lib/appium_lib/android/common/helper.rb', line 167 def tag(class_name) find_element :class, class_name end |
#tags(class_name) ⇒ Element
Find all elements of type class_name
175 176 177 |
# File 'lib/appium_lib/android/common/helper.rb', line 175 def (class_name) find_elements :class, class_name end |
#text(value) ⇒ TextView
Find the first TextView that contains value or by index. If int then the TextView at that index is returned.
10 11 12 13 14 |
# File 'lib/appium_lib/android/element/text.rb', line 10 def text(value) return ele_index TextView, value if value.is_a? Numeric complex_find_contains TextView, value end |
#text_exact(value) ⇒ TextView
Find the first TextView that exactly matches value.
41 42 43 |
# File 'lib/appium_lib/android/element/text.rb', line 41 def text_exact(value) complex_find_exact TextView, value end |
#textfield(value) ⇒ EditText
Find the first EditText that contains value or by index. If int then the EditText at that index is returned.
9 10 11 12 13 |
# File 'lib/appium_lib/android/element/textfield.rb', line 9 def textfield(value) return ele_index EditText, value if value.is_a? Numeric complex_find_contains EditText, value end |
#textfield_exact(value) ⇒ EditText
Find the first EditText that exactly matches value.
40 41 42 |
# File 'lib/appium_lib/android/element/textfield.rb', line 40 def textfield_exact(value) complex_find_exact EditText, value end |
#textfields(value = false) ⇒ Array<EditText>
Find all EditTexts containing value. If value is omitted, all EditTexts are returned.
19 20 21 22 23 |
# File 'lib/appium_lib/android/element/textfield.rb', line 19 def textfields(value = false) return EditText unless value complex_finds_contains EditText, value end |
#textfields_exact(value) ⇒ Array<EditText>
Find all EditTexts that exactly match value.
47 48 49 |
# File 'lib/appium_lib/android/element/textfield.rb', line 47 def textfields_exact(value) complex_finds_exact EditText, value end |
#texts(value = false) ⇒ Array<TextView>
Find all TextViews containing value. If value is omitted, all texts are returned.
20 21 22 23 24 |
# File 'lib/appium_lib/android/element/text.rb', line 20 def texts(value = false) return TextView unless value complex_finds_contains TextView, value end |
#texts_exact(value) ⇒ Array<TextView>
Find all TextViews that exactly match value.
48 49 50 |
# File 'lib/appium_lib/android/element/text.rb', line 48 def texts_exact(value) complex_finds_exact TextView, value end |