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

Instance Method Details

#_no_such_elementObject

Raises:

  • (Selenium::WebDriver::Error::NoSuchElementError)


268
269
270
271
# File 'lib/appium_lib/common/helper.rb', line 268

def _no_such_element
  error_message = 'An element could not be located on the page using the given search parameters.'
  raise Selenium::WebDriver::Error::NoSuchElementError, error_message
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.options = opts }
        else
          Nokogiri::XML(source)  { |cfg| cfg.options = opts }
        end
  puts doc.to_xml indent: 2
end

#backvoid

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

Examples:


@driver.get_available_log_types #=> [:syslog, :crashlog, :performance]

Returns:

  • ([String])

    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.

Examples:


@driver.get_log("syslog") #=> [[Selenium::WebDriver::LogEntry]]
@driver.get_log(:syslog)  #=> [[Selenium::WebDriver::LogEntry]]

Parameters:

  • type (String|Hash)

    You can get particular type’s logs.

Returns:

  • ([Selenium::WebDriver::LogEntry])

    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_classString

Returns a string of class counts of visible elements.

Examples:


get_page_class #=> "24x XCUIElementTypeStaticText\n12x XCUIElementTypeCell\n8x XCUIElementTypeOther\n
               #    2x XCUIElementTypeWindow\n1x XCUIElementTypeStatusBar\n1x XCUIElementTypeTable\n1
               #    x XCUIElementTypeNavigationBar\n1x XCUIElementTypeApplication"

Returns:

  • (String)


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_sourceString

Returns XML string for the current page Same as driver.page_source

Returns:

  • (String)


155
156
157
# File 'lib/appium_lib/common/helper.rb', line 155

def get_source
  @driver.page_source
end

#ignoreObject

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_stringsObject



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_classnil

Count all classes on screen and print to stdout. Useful for appium_console.

Examples:


page_class
  # 24x XCUIElementTypeStaticText
  # 12x XCUIElementTypeCell
  # 8x XCUIElementTypeOther
  # 2x XCUIElementTypeWindow
  # 1x XCUIElementTypeStatusBar
  # 1x XCUIElementTypeTable
  # 1x XCUIElementTypeNavigationBar
  # 1x XCUIElementTypeApplication

Returns:

  • (nil)


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

Examples:


px_to_window_rel x: 50, y: 150 #=> #<OpenStruct x="50.0 / 375.0", y="150.0 / 667.0">


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.

Parameters:

  • id (String)

    the id to resolve

Returns:

  • (String)


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_idString

For Sauce Labs reporting. Returns the current session id.

Examples:


@driver.session_id #=> "some-session-ids"

Returns:

  • (String)


49
50
51
# File 'lib/appium_lib/common/helper.rb', line 49

def session_id
  @driver.session_id
end

#sourcevoid

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.

Examples:


wait(timeout: 20, interval: 0.2, message: 'custom message') { button_exact('Back') }.click
wait(20) { button_exact('Back') }.click

Parameters:

  • opts (Hash|Numeric) (defaults to: {})

    Options. If the value is Numeric, the value is set as ‘{ timeout: value }`

Options Hash (opts):

  • :timeout (Numeric)

    Seconds to wait before timing out. Set default by ‘appium_wait_timeout` (30).

  • :interval (Numeric)

    Seconds to sleep between polls. Set default by ‘appium_wait_interval` (0.5).

  • :message (String)

    Exception message if timed out.

  • :ignore (Array, Exception)

    Exceptions to ignore while polling (default: Exception)



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.

Examples:


wait_true(timeout: 20, interval: 0.2, message: 'custom message') { button_exact('Back') }.click
wait_true(20) { button_exact('Back') }.click

Parameters:

  • opts (Hash|Numeric) (defaults to: {})

    Options. If the value is Numeric, the value is set as ‘{ timeout: value }`

Options Hash (opts):

  • :timeout (Numeric)

    Seconds to wait before timing out. Set default by ‘appium_wait_timeout` (30).

  • :interval (Numeric)

    Seconds to sleep between polls. Set default by ‘appium_wait_interval` (0.5).

  • :message (String)

    Exception message if timed out.

  • :ignore (Array, Exception)

    Exceptions to ignore while polling (default: Exception)



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.

Parameters:

  • target (String)

    the target to search for in strings.xml values

Returns:

  • (Array)


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.

Parameters:

  • target (String)

    the target to search for in strings.xml keys

Returns:

  • (Array)


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.

Parameters:

  • xpath_str (String)

    the XPath string

Returns:

  • (Element)


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.

Parameters:

  • xpath_str (String)

    the XPath string

Returns:

  • (Array<Element>)


65
66
67
# File 'lib/appium_lib/common/helper.rb', line 65

def xpaths(xpath_str)
  @driver.find_elements :xpath, xpath_str
end