Module: Appium::Common
- Defined in:
- lib/appium_lib/common/log.rb,
lib/appium_lib/common/wait.rb,
lib/appium_lib/common/helper.rb,
lib/appium_lib/common/command.rb,
lib/appium_lib/common/command/ws_logcat.rb
Defined Under Namespace
Modules: Command Classes: CountElements, HTMLElements, Wait
Instance Method Summary collapse
- #_no_such_element ⇒ Object
- #_print_source(source) ⇒ Object
-
#back ⇒ void
Navigate back.
-
#get_available_log_types ⇒ [String]
Get a list of available log types.
-
#get_log(type) ⇒ [Selenium::WebDriver::LogEntry]
A list of logs data.
-
#get_page_class ⇒ String
Returns a string of class counts of visible elements.
-
#get_source ⇒ String
Returns XML string for the current page Same as driver.page_source.
-
#ignore ⇒ Object
Return yield and ignore any exceptions.
- #lazy_load_strings ⇒ Object
-
#page_class ⇒ nil
Count all classes on screen and print to stdout.
-
#px_to_window_rel(opts = {}, driver = $driver) ⇒ Object
Converts pixel values to window relative values.
-
#resolve_id(id) ⇒ String
Resolve id in strings.xml and return the value.
-
#session_id ⇒ String
For Sauce Labs reporting.
-
#source ⇒ void
Prints xml of the current page.
-
#wait(opts = {}) ⇒ Object
Check every interval seconds to see if yield doesn’t raise an exception.
-
#wait_true(opts = {}) ⇒ Object
Check every interval seconds to see if yield returns a truthy value.
-
#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
#_no_such_element ⇒ Object
268 269 270 271 |
# File 'lib/appium_lib/common/helper.rb', line 268 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
274 275 276 277 278 279 280 281 282 |
# File 'lib/appium_lib/common/helper.rb', line 274 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 |
#back ⇒ void
This method returns an undefined value.
Navigate back.
38 39 40 |
# File 'lib/appium_lib/common/helper.rb', line 38 def back @driver.navigate.back end |
#get_available_log_types ⇒ [String]
Get a list of available log types
37 38 39 |
# File 'lib/appium_lib/common/log.rb', line 37 def get_available_log_types @driver.logs.available_types end |
#get_log(type) ⇒ [Selenium::WebDriver::LogEntry]
Returns A list of logs data.
25 26 27 |
# File 'lib/appium_lib/common/log.rb', line 25 def get_log(type) @driver.logs.get type end |
#get_page_class ⇒ String
Returns a string of class counts of visible elements.
116 117 118 119 120 121 122 123 |
# File 'lib/appium_lib/common/helper.rb', line 116 def get_page_class parser = @count_elements_parser ||= Nokogiri::XML::SAX::Parser.new(CountElements.new(@core.device)) parser.document.reset parser.parse get_source parser.document.formatted_result end |
#get_source ⇒ String
Returns XML string for the current page Same as driver.page_source
155 156 157 |
# File 'lib/appium_lib/common/helper.rb', line 155 def get_source @driver.page_source end |
#ignore ⇒ Object
Return yield and ignore any exceptions.
30 31 32 33 34 |
# File 'lib/appium_lib/common/helper.rb', line 30 def ignore yield rescue Exception # rubocop:disable Lint/RescueException # Ignored end |
#lazy_load_strings ⇒ Object
175 176 177 178 179 |
# File 'lib/appium_lib/common/helper.rb', line 175 def lazy_load_strings # app strings only works on local apps. # on disk apps (ex: com.android.settings) will error @lazy_load_strings ||= ignore { app_strings } || {} end |
#page_class ⇒ nil
Count all classes on screen and print to stdout. Useful for appium_console.
141 142 143 144 |
# File 'lib/appium_lib/common/helper.rb', line 141 def page_class puts get_page_class nil end |
#px_to_window_rel(opts = {}, driver = $driver) ⇒ Object
Converts pixel values to window relative values
165 166 167 168 169 170 171 172 |
# File 'lib/appium_lib/common/helper.rb', line 165 def px_to_window_rel(opts = {}, driver = $driver) 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.
200 201 202 203 |
# File 'lib/appium_lib/common/helper.rb', line 200 def resolve_id(id) lazy_load_strings @lazy_load_strings[id] end |
#session_id ⇒ String
For Sauce Labs reporting. Returns the current session id.
49 50 51 |
# File 'lib/appium_lib/common/helper.rb', line 49 def session_id @driver.session_id end |
#source ⇒ void
This method returns an undefined value.
Prints xml of the current page
148 149 150 |
# File 'lib/appium_lib/common/helper.rb', line 148 def source _print_source get_source end |
#wait(opts = {}) ⇒ 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.
73 74 75 76 77 78 79 80 |
# File 'lib/appium_lib/common/wait.rb', line 73 def wait(opts = {}) opts = { timeout: opts } if opts.is_a? Numeric if opts.is_a? Hash opts.empty? ? @core.wait { yield } : @core.wait(**opts) { yield } else ::Appium::Logger.warn('Arguments should be Hash like {timeout: 100}') end end |
#wait_true(opts = {}) ⇒ 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.
44 45 46 47 48 49 50 51 52 |
# File 'lib/appium_lib/common/wait.rb', line 44 def wait_true(opts = {}) opts = { timeout: opts } if opts.is_a? Numeric if opts.is_a? Hash opts.empty? ? @core.wait_true { yield } : @core.wait_true(**opts) { yield } else ::Appium::Logger.warn('Arguments should be Hash like {timeout: 100}') end end |
#xml_keys(target) ⇒ Array
Search strings.xml’s values for target.
184 185 186 187 |
# File 'lib/appium_lib/common/helper.rb', line 184 def xml_keys(target) lazy_load_strings @lazy_load_strings.select { |key, _value| key.downcase.include? target.downcase } end |
#xml_values(target) ⇒ Array
Search strings.xml’s keys for target.
192 193 194 195 |
# File 'lib/appium_lib/common/helper.rb', line 192 def xml_values(target) lazy_load_strings @lazy_load_strings.select { |_key, value| value.downcase.include? target.downcase } end |
#xpath(xpath_str) ⇒ Element
Returns the first element that matches the provided xpath.
57 58 59 |
# File 'lib/appium_lib/common/helper.rb', line 57 def xpath(xpath_str) @driver.find_element :xpath, xpath_str end |
#xpaths(xpath_str) ⇒ Array<Element>
Returns all elements that match the provided xpath.
65 66 67 |
# File 'lib/appium_lib/common/helper.rb', line 65 def xpaths(xpath_str) @driver.find_elements :xpath, xpath_str end |