Module: Appium::Common
- Defined in:
- lib/appium_lib/common/wait.rb,
lib/appium_lib/common/patch.rb,
lib/appium_lib/common/helper.rb,
lib/appium_lib/common/element/window.rb
Defined Under Namespace
Classes: CountElements, HTMLElements
Instance Method Summary collapse
-
#_generic_wait(opts = {}) ⇒ Object
Wait code from the selenium Ruby gem github.com/SeleniumHQ/selenium/blob/cf501dda3f0ed12233de51ce8170c0e8090f0c20/rb/lib/selenium/webdriver/common/wait.rb.
- #_no_such_element ⇒ Object
- #_print_source(source) ⇒ Object
-
#_process_wait_opts(opts) ⇒ Object
process opts before calling _generic_wait.
-
#back ⇒ void
Navigate back.
-
#get_page_class ⇒ Object
Returns a string of class counts of visible elements.
-
#ignore ⇒ Object
Return yield and ignore any exceptions.
- #lazy_load_strings ⇒ Object
-
#page_class ⇒ Object
Count all classes on screen and print to stdout.
-
#px_to_window_rel(opts = {}) ⇒ Object
Converts pixel values to window relative values.
-
#resolve_id(id) ⇒ String
Resolve id in strings.xml and return the value.
-
#session_id ⇒ Object
For Sauce Labs reporting.
-
#wait(opts = {}, &block) ⇒ Object
Check every interval seconds to see if yield doesn’t raise an exception.
-
#wait_true(opts = {}, &block) ⇒ Object
Check every interval seconds to see if yield returns a truthy value.
-
#window_size ⇒ Object
Get the window’s size.
-
#xml_keys(target) ⇒ Array
Search strings.xml’s values for target.
-
#xml_values(target) ⇒ Array
Search strings.xml’s keys for target.
-
#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
#_generic_wait(opts = {}) ⇒ Object
Wait code from the selenium Ruby gem github.com/SeleniumHQ/selenium/blob/cf501dda3f0ed12233de51ce8170c0e8090f0c20/rb/lib/selenium/webdriver/common/wait.rb
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/appium_lib/common/wait.rb', line 9 def _generic_wait(opts = {}) valid_keys = [:timeout, :interval, :message, :ignore, :return_if_true] invalid_keys = [] opts.keys.each { |key| invalid_keys << key unless valid_keys.include?(key) } # [:one, :two] => :one, :two raise "Invalid keys #{invalid_keys.to_s[1..-2]}. Valid keys are #{valid_keys.to_s[1..-2]}" unless invalid_keys.empty? timeout = opts.fetch(:timeout, @appium_wait_timeout) interval = opts.fetch(:interval, @appium_wait_interval) = opts[:message] ignored = Array(opts[:ignore] || ::Exception) return_if_true = opts[:return_if_true] end_time = Time.now + timeout last_error = nil until Time.now > end_time begin return yield unless return_if_true result = yield return result if result rescue ::Errno::ECONNREFUSED => e raise e rescue *ignored => last_error # rubocop:disable Lint/HandleExceptions # swallowed end sleep interval end msg = ? .dup : "timed out after #{timeout} seconds" msg << " (#{last_error.message})" if last_error raise Selenium::WebDriver::Error::TimeOutError, msg end |
#_no_such_element ⇒ Object
217 218 219 220 |
# File 'lib/appium_lib/common/helper.rb', line 217 def _no_such_element = 'An element could not be located on the page using the given search parameters.' raise Selenium::WebDriver::Error::NoSuchElementError, end |
#_print_source(source) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/appium_lib/common/helper.rb', line 56 def _print_source(source) opts = Nokogiri::XML::ParseOptions::NOBLANKS | Nokogiri::XML::ParseOptions::NONET doc = if source.start_with? '<html' Nokogiri::HTML(source) { |cfg| cfg. = opts } else Nokogiri::XML(source) { |cfg| cfg. = opts } end puts doc.to_xml indent: 2 end |
#_process_wait_opts(opts) ⇒ Object
process opts before calling _generic_wait
48 49 50 51 52 |
# File 'lib/appium_lib/common/wait.rb', line 48 def _process_wait_opts(opts) opts = { timeout: opts } if opts.is_a?(Numeric) raise 'opts must be a hash' unless opts.is_a? Hash opts end |
#back ⇒ void
This method returns an undefined value.
Navigate back.
31 32 33 |
# File 'lib/appium_lib/common/helper.rb', line 31 def back @driver.navigate.back end |
#get_page_class ⇒ Object
Returns a string of class counts of visible elements.
97 98 99 100 101 102 103 104 |
# File 'lib/appium_lib/common/helper.rb', line 97 def get_page_class parser = @count_elements_parser ||= Nokogiri::XML::SAX::Parser.new(CountElements.new) parser.document.reset parser.parse get_source parser.document.formatted_result end |
#ignore ⇒ Object
Return yield and ignore any exceptions.
24 25 26 27 |
# File 'lib/appium_lib/common/helper.rb', line 24 def ignore yield rescue Exception # rubocop:disable Lint/HandleExceptions, Lint/RescueException end |
#lazy_load_strings ⇒ Object
128 129 130 131 132 |
# File 'lib/appium_lib/common/helper.rb', line 128 def lazy_load_strings # app strings only works on local apps. # on disk apps (ex: com.android.settings) will error @strings_xml ||= ignore { app_strings } || {} end |
#page_class ⇒ Object
Count all classes on screen and print to stdout. Useful for appium_console.
108 109 110 111 |
# File 'lib/appium_lib/common/helper.rb', line 108 def page_class puts get_page_class nil end |
#px_to_window_rel(opts = {}) ⇒ Object
Converts pixel values to window relative values
“‘ruby px_to_window_rel x: 50, y: 150 “`
118 119 120 121 122 123 124 125 |
# File 'lib/appium_lib/common/helper.rb', line 118 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 |
#resolve_id(id) ⇒ String
Resolve id in strings.xml and return the value.
153 154 155 156 |
# File 'lib/appium_lib/common/helper.rb', line 153 def resolve_id(id) lazy_load_strings @strings_xml[id] end |
#session_id ⇒ Object
For Sauce Labs reporting. Returns the current session id.
36 37 38 |
# File 'lib/appium_lib/common/helper.rb', line 36 def session_id @driver.session_id end |
#wait(opts = {}, &block) ⇒ Object
Check every interval seconds to see if yield doesn’t raise an exception. Give up after timeout seconds.
Wait code from the selenium Ruby gem github.com/SeleniumHQ/selenium/blob/cf501dda3f0ed12233de51ce8170c0e8090f0c20/rb/lib/selenium/webdriver/common/wait.rb
If only a number is provided then it’s treated as the timeout value.
87 88 89 90 |
# File 'lib/appium_lib/common/wait.rb', line 87 def wait(opts = {}, &block) opts = _process_wait_opts(opts).merge(return_if_true: false) _generic_wait opts, &block end |
#wait_true(opts = {}, &block) ⇒ Object
Check every interval seconds to see if yield returns a truthy value. Note this isn’t a strict boolean true, any truthy value is accepted. false and nil are considered failures. Give up after timeout seconds.
Wait code from the selenium Ruby gem github.com/SeleniumHQ/selenium/blob/cf501dda3f0ed12233de51ce8170c0e8090f0c20/rb/lib/selenium/webdriver/common/wait.rb
If only a number is provided then it’s treated as the timeout value.
69 70 71 72 |
# File 'lib/appium_lib/common/wait.rb', line 69 def wait_true(opts = {}, &block) opts = _process_wait_opts(opts).merge(return_if_true: true) _generic_wait opts, &block 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 |
#xml_keys(target) ⇒ Array
Search strings.xml’s values for target.
137 138 139 140 |
# File 'lib/appium_lib/common/helper.rb', line 137 def xml_keys(target) lazy_load_strings @strings_xml.select { |key, _value| key.downcase.include? target.downcase } end |
#xml_values(target) ⇒ Array
Search strings.xml’s keys for target.
145 146 147 148 |
# File 'lib/appium_lib/common/helper.rb', line 145 def xml_values(target) lazy_load_strings @strings_xml.select { |_key, value| value.downcase.include? target.downcase } end |
#xpath(xpath_str) ⇒ Element
Returns the first element that matches the provided xpath.
44 45 46 |
# File 'lib/appium_lib/common/helper.rb', line 44 def xpath(xpath_str) find_element :xpath, xpath_str end |
#xpaths(xpath_str) ⇒ Array<Element>
Returns all elements that match the provided xpath.
52 53 54 |
# File 'lib/appium_lib/common/helper.rb', line 52 def xpaths(xpath_str) find_elements :xpath, xpath_str end |