Module: Appium::Common
- Defined in:
- lib/appium_lib/common/helper.rb,
lib/appium_lib/common/element/text.rb,
lib/appium_lib/common/element/button.rb,
lib/appium_lib/common/element/window.rb,
lib/appium_lib/common/patch.rb
Overview
end Add status to WebDriver
Instance Method Summary collapse
-
#back ⇒ void
Navigate back.
-
#button(text, number = 0) ⇒ Button
Find a button by text and optionally number.
-
#button_exact(text) ⇒ Button
Get the first button element that exactly matches text.
-
#button_num(text, number = 1) ⇒ Button
Expected to be called via button method.
-
#buttons(text = nil) ⇒ Array<String>, Array<Buttons>
Get an array of button texts or button elements if text is provided.
-
#buttons_exact(text) ⇒ Array<Button>
Get all button elements that exactly match text.
-
#e_buttons ⇒ Array<Button>
Get an array of button elements.
-
#e_s_texts ⇒ Array<Text>
Get an array of text elements.
-
#ele_index(tag_name, index) ⇒ Element
Get the element of type tag_name at matching index.
-
#find_ele_by_attr_include(tag, attr, value) ⇒ Element
Get the first tag by attribute that exactly matches value.
-
#find_ele_by_text(tag, text) ⇒ Element
Get the first tag that exactly matches tag and text.
-
#find_ele_by_text_include(tag, text) ⇒ Element
Get the first tag that includes text.
-
#find_eles(tag_name) ⇒ Array<Element>
Get all elements exactly matching tag name.
-
#find_eles_by_attr_include(tag, attr, value) ⇒ Array<Element>
Get tags by attribute that include value.
-
#find_eles_by_text(tag, text) ⇒ Array<Element>
Get all tags that exactly match tag and text.
-
#find_eles_by_text_include(tag, text) ⇒ Array<Element>
Get the tags that include text.
-
#find_name(name) ⇒ Element
Returns the first element that exactly matches name.
-
#find_names(name) ⇒ Array<Element>
Returns all elements that exactly match name.
-
#first_button ⇒ Button
Get the first button element.
-
#first_ele(tag_name) ⇒ Element
Get the first tag that matches tag_name.
-
#first_s_text ⇒ Text
Get the first text element.
-
#get_source ⇒ JSON
Gets a JSON view of the current page.
-
#id(id) ⇒ Element
Find by id.
-
#ignore(&block) ⇒ Object
Return block.call and ignore any exceptions.
-
#last_button ⇒ Button
Get the last button element.
-
#last_ele(tag_name) ⇒ Element
Get the last tag that matches tag_name.
-
#last_s_text ⇒ Text
Get the last text element.
-
#px_to_window_rel(opts = {}) ⇒ Object
Converts pixel values to window relative values.
-
#s_text(text) ⇒ Text
Get the first element that includes text.
-
#s_text_exact(text) ⇒ Text
Get the first textfield that matches text.
-
#s_texts ⇒ Array<String>
Get an array of text texts.
-
#session_id ⇒ Object
For Sauce Labs reporting.
-
#source ⇒ void
Prints a JSON view of the current page.
-
#tag(tag_name) ⇒ Element
Returns the first element matching tag_name.
-
#wait(max_wait = 30, interval = 0.5, &block) ⇒ Object
Check every 0.5 seconds to see if block.call doesn’t raise an exception.
-
#wait_true(max_wait = 30, interval = 0.5, &block) ⇒ Object
Check every 0.5 seconds to see if block.call returns true.
-
#window_size ⇒ Object
Get the window’s size.
-
#xpath(xpath_str) ⇒ Element
Returns the first element that matches the provided xpath.
-
#xpaths(xpath_str) ⇒ Array<Element>
Returns all elements that match the provided xpath.
Instance Method Details
#back ⇒ void
This method returns an undefined value.
Navigate back.
75 76 77 |
# File 'lib/appium_lib/common/helper.rb', line 75 def back @driver.navigate.back end |
#button(text, number = 0) ⇒ Button
Find a button by text and optionally number.
8 9 10 11 12 13 14 |
# File 'lib/appium_lib/common/element/button.rb', line 8 def text, number=0 # return button at index. return ele_index :button, text if text.is_a? Numeric number >= 1 ? ( text, number ) : find_ele_by_text_include( :button, text ) end |
#button_exact(text) ⇒ Button
Get the first button element that exactly matches text.
39 40 41 |
# File 'lib/appium_lib/common/element/button.rb', line 39 def text find_ele_by_text :button, text end |
#button_num(text, number = 1) ⇒ Button
Expected to be called via button method.
Get the button element exactly matching text and occurrence. number=2 means the 2nd occurrence.
find the second Sign In button
b = e_button ‘Sign In’, 2
Button order will change in iOS vs Android so if there’s no button found at number then return the first button.
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/appium_lib/common/element/button.rb', line 72 def text, number=1 raise 'Number must be >= 1' if number <= 0 number = number - 1 # zero indexed result = nil elements = text elements.size > number ? result = elements[number] : result = elements.first result end |
#buttons(text = nil) ⇒ Array<String>, Array<Buttons>
Get an array of button texts or button elements if text is provided.
19 20 21 22 |
# File 'lib/appium_lib/common/element/button.rb', line 19 def text=nil text == nil ? find_eles_attr( :button, :text ) : find_eles_by_text_include( :button, text ) end |
#buttons_exact(text) ⇒ Array<Button>
Get all button elements that exactly match text.
46 47 48 |
# File 'lib/appium_lib/common/element/button.rb', line 46 def text find_eles_by_text :button, text end |
#e_buttons ⇒ Array<Button>
Get an array of button elements.
52 53 54 |
# File 'lib/appium_lib/common/element/button.rb', line 52 def find_eles :button end |
#e_s_texts ⇒ Array<Text>
Get an array of text elements.
14 15 16 |
# File 'lib/appium_lib/common/element/text.rb', line 14 def e_s_texts find_eles :text end |
#ele_index(tag_name, index) ⇒ Element
Get the element of type tag_name at matching index.
104 105 106 107 |
# File 'lib/appium_lib/common/helper.rb', line 104 def ele_index tag_name, index # XPath index starts at 1. ruby_lib index starts at 0 find_element :xpath, "//#{tag_name}[#{index + 1}]" end |
#find_ele_by_attr_include(tag, attr, value) ⇒ Element
Get the first tag by attribute that exactly matches value.
137 138 139 |
# File 'lib/appium_lib/common/helper.rb', line 137 def find_ele_by_attr_include tag, attr, value @driver.find_element :xpath, %Q(#{tag}[contains(@#{attr}, '#{value}')]) end |
#find_ele_by_text(tag, text) ⇒ Element
Get the first tag that exactly matches tag and text.
120 121 122 |
# File 'lib/appium_lib/common/helper.rb', line 120 def find_ele_by_text tag, text @driver.find_element :xpath, %Q(#{tag}[@text='#{text}']) end |
#find_ele_by_text_include(tag, text) ⇒ Element
Get the first tag that includes text. element.attribute(:text).include? text
155 156 157 |
# File 'lib/appium_lib/common/helper.rb', line 155 def find_ele_by_text_include tag, text find_ele_by_attr_include tag, :text, text end |
#find_eles(tag_name) ⇒ Array<Element>
Get all elements exactly matching tag name
112 113 114 |
# File 'lib/appium_lib/common/helper.rb', line 112 def find_eles tag_name @driver.find_elements :tag_name, tag_name end |
#find_eles_by_attr_include(tag, attr, value) ⇒ Array<Element>
Get tags by attribute that include value.
146 147 148 |
# File 'lib/appium_lib/common/helper.rb', line 146 def find_eles_by_attr_include tag, attr, value @driver.find_elements :xpath, %Q(#{tag}[contains(@#{attr}, '#{value}')]) end |
#find_eles_by_text(tag, text) ⇒ Array<Element>
Get all tags that exactly match tag and text.
128 129 130 |
# File 'lib/appium_lib/common/helper.rb', line 128 def find_eles_by_text tag, text @driver.find_elements :xpath, %Q(#{tag}[@text='#{text}']) end |
#find_eles_by_text_include(tag, text) ⇒ Array<Element>
Get the tags that include text. element.attribute(:text).include? text
164 165 166 |
# File 'lib/appium_lib/common/helper.rb', line 164 def find_eles_by_text_include tag, text find_eles_by_attr_include tag, :text, text end |
#find_name(name) ⇒ Element
Returns the first element that exactly matches name
200 201 202 |
# File 'lib/appium_lib/common/helper.rb', line 200 def find_name name find_element :name, name end |
#find_names(name) ⇒ Array<Element>
Returns all elements that exactly match name
208 209 210 |
# File 'lib/appium_lib/common/helper.rb', line 208 def find_names name find_elements :name, name end |
#first_button ⇒ Button
Get the first button element.
26 27 28 |
# File 'lib/appium_lib/common/element/button.rb', line 26 def first_ele :button end |
#first_ele(tag_name) ⇒ Element
Get the first tag that matches tag_name
171 172 173 174 |
# File 'lib/appium_lib/common/helper.rb', line 171 def first_ele tag_name # XPath index starts at 1 find_element :xpath, "//#{tag_name}[1]" end |
#first_s_text ⇒ Text
Get the first text element.
20 21 22 |
# File 'lib/appium_lib/common/element/text.rb', line 20 def first_s_text first_ele :text end |
#get_source ⇒ JSON
Gets a JSON view of the current page
191 192 193 194 |
# File 'lib/appium_lib/common/helper.rb', line 191 def get_source # must set max nesting. default limit of 20 is too low for selendroid JSON.parse @driver.page_source, max_nesting: 9999 end |
#id(id) ⇒ Element
Find by id. Useful for selendroid
69 70 71 |
# File 'lib/appium_lib/common/helper.rb', line 69 def id id find_element :id, id end |
#ignore(&block) ⇒ Object
Return block.call and ignore any exceptions.
45 46 47 |
# File 'lib/appium_lib/common/helper.rb', line 45 def ignore &block begin; block.call; rescue; end end |
#last_button ⇒ Button
Get the last button element.
32 33 34 |
# File 'lib/appium_lib/common/element/button.rb', line 32 def last_ele :button end |
#last_ele(tag_name) ⇒ Element
Get the last tag that matches tag_name
179 180 181 |
# File 'lib/appium_lib/common/helper.rb', line 179 def last_ele tag_name xpath "//#{tag_name}[last()]" end |
#last_s_text ⇒ Text
Get the last text element
26 27 28 |
# File 'lib/appium_lib/common/element/text.rb', line 26 def last_s_text last_ele :text end |
#px_to_window_rel(opts = {}) ⇒ Object
Converts pixel values to window relative values
“‘ruby px_to_window_rel x: 50, y: 150 “`
226 227 228 229 230 231 232 233 234 |
# File 'lib/appium_lib/common/helper.rb', line 226 def px_to_window_rel opts={} w = $driver.window_size x = opts.fetch :x, 0 y = opts.fetch :y, 0 OpenStruct.new( x: x.to_f / w.width.to_f, y: y.to_f / w.height.to_f ) end |
#s_text(text) ⇒ Text
Get the first element that includes text.
33 34 35 36 |
# File 'lib/appium_lib/common/element/text.rb', line 33 def s_text text return ele_index :text, text if text.is_a? Numeric find_ele_by_text_include :text, text end |
#s_text_exact(text) ⇒ Text
Get the first textfield that matches text.
41 42 43 |
# File 'lib/appium_lib/common/element/text.rb', line 41 def s_text_exact text find_ele_by_text :text, text end |
#s_texts ⇒ Array<String>
Get an array of text texts.
8 9 10 |
# File 'lib/appium_lib/common/element/text.rb', line 8 def s_texts find_eles_attr :text, :text end |
#session_id ⇒ Object
For Sauce Labs reporting. Returns the current session id.
80 81 82 |
# File 'lib/appium_lib/common/helper.rb', line 80 def session_id @driver.session_id end |
#source ⇒ void
This method returns an undefined value.
Prints a JSON view of the current page
185 186 187 |
# File 'lib/appium_lib/common/helper.rb', line 185 def source ap get_source end |
#tag(tag_name) ⇒ Element
Returns the first element matching tag_name
216 217 218 |
# File 'lib/appium_lib/common/helper.rb', line 216 def tag tag_name find_element :tag_name, tag_name end |
#wait(max_wait = 30, interval = 0.5, &block) ⇒ Object
Check every 0.5 seconds to see if block.call doesn’t raise an exception. if .call raises an exception then it will be tried again. if .call doesn’t raise an exception then it will stop waiting.
Example: wait { name(‘back’).click }
Give up after 30 seconds.
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/appium_lib/common/helper.rb', line 33 def wait max_wait=30, interval=0.5, &block # Rescue Timeout::Error: execution expired result = nil timeout max_wait do until (result = begin; block.call || true; rescue; end) sleep interval end end result end |
#wait_true(max_wait = 30, interval = 0.5, &block) ⇒ Object
Check every 0.5 seconds to see if block.call returns true. nil is considered a failure. Give up after 30 seconds.
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/appium_lib/common/helper.rb', line 55 def wait_true max_wait=30, interval=0.5, &block # Rescue Timeout::Error: execution expired result = nil timeout max_wait do until (result = begin; block.call; rescue; end) sleep interval end end result end |
#window_size ⇒ Object
Get the window’s size
5 6 7 8 |
# File 'lib/appium_lib/common/element/window.rb', line 5 def window_size return nil if @driver.nil? @driver.manage.window.size end |
#xpath(xpath_str) ⇒ Element
Returns the first element that matches the provided xpath.
88 89 90 |
# File 'lib/appium_lib/common/helper.rb', line 88 def xpath xpath_str find_element :xpath, xpath_str end |
#xpaths(xpath_str) ⇒ Array<Element>
Returns all elements that match the provided xpath.
96 97 98 |
# File 'lib/appium_lib/common/helper.rb', line 96 def xpaths xpath_str find_elements :xpath, xpath_str end |