Class: Maze::Driver::Appium

Inherits:
Appium::Driver
  • Object
show all
Defined in:
lib/maze/driver/appium.rb

Overview

Provide a thin layer of abstraction above @see Appium::Driver

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server_url, capabilities, locator = :id) ⇒ Appium

Creates the Appium driver

Parameters:

  • server_url (String)

    URL of the Appium server

  • capabilities (Hash)

    a hash of capabilities to be used in this test run

  • locator (Symbol) (defaults to: :id)

    the primary locator strategy Appium should use to find elements



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/maze/driver/appium.rb', line 30

def initialize(server_url, capabilities, locator = :id)
  # Sets up identifiers for ease of connecting jobs
  capabilities ||= {}

  @element_locator = locator
  @capabilities = capabilities

  # Timers
  @find_element_timer = Maze.timers.add 'Appium - find element'
  @click_element_timer = Maze.timers.add 'Appium - click element'
  @clear_element_timer = Maze.timers.add 'Appium - clear element'
  @send_keys_timer = Maze.timers.add 'Appium - send keys to element'

  super({
    'caps' => @capabilities,
    'appium_lib' => {
      server_url: server_url
    }
  }, true)
end

Instance Attribute Details

#app_idString

Returns The app_id derived from session_capabilities (appPackage on Android, bundleID on iOS).

Returns:

  • (String)

    The app_id derived from session_capabilities (appPackage on Android, bundleID on iOS)



15
16
17
# File 'lib/maze/driver/appium.rb', line 15

def app_id
  @app_id
end

#capabilitiesObject (readonly)

Returns the value of attribute capabilities.



23
24
25
# File 'lib/maze/driver/appium.rb', line 23

def capabilities
  @capabilities
end

#device_typeObject (readonly)

Returns the value of attribute device_type.



19
20
21
# File 'lib/maze/driver/appium.rb', line 19

def device_type
  @device_type
end

Instance Method Details

#clear_and_send_keys_to_element(element_id, text) ⇒ Object

Sends keys to a given element, clearing it first

Parameters:

  • element_id (String)

    the element to clear and send text to

  • text (String)

    the text to send



170
171
172
173
174
175
176
177
178
179
# File 'lib/maze/driver/appium.rb', line 170

def clear_and_send_keys_to_element(element_id, text)
  element = find_element_timed(element_id)
  @clear_element_timer.time do
    element.clear
  end

  @send_keys_timer.time do
    element.send_keys(text)
  end
end

#clear_element(element_id) ⇒ Object

Clears a given element

Parameters:

  • element_id (String)

    the element to clear



120
121
122
123
124
125
# File 'lib/maze/driver/appium.rb', line 120

def clear_element(element_id)
  element = find_element_timed(element_id)
  @clear_element_timer.time do
    element.clear
  end
end

#click_element(element_id) ⇒ Object

Clicks a given element

Parameters:

  • element_id (String)

    the element to click



96
97
98
99
100
101
# File 'lib/maze/driver/appium.rb', line 96

def click_element(element_id)
  element = find_element_timed(element_id)
  @click_element_timer.time do
    element.click
  end
end

#click_element_if_present(element_id) ⇒ Boolean

Clicks a given element, ignoring any NoSuchElementError

Parameters:

  • element_id (String)

    the element to click

Returns:

  • (Boolean)

    True is the element was clicked



107
108
109
110
111
112
113
114
115
# File 'lib/maze/driver/appium.rb', line 107

def click_element_if_present(element_id)
  element = find_element_timed(element_id)
  @click_element_timer.time do
    element.click
  end
  true
rescue Selenium::WebDriver::Error::NoSuchElementError
  false
end

#device_infoObject



189
190
191
# File 'lib/maze/driver/appium.rb', line 189

def device_info
  driver.execute_script('mobile:deviceInfo')
end

#find_element_timed(element_id) ⇒ Object

A wrapper around find_element adding timer functionality



87
88
89
90
91
# File 'lib/maze/driver/appium.rb', line 87

def find_element_timed(element_id)
  @find_element_timer.time do
    find_element(@element_locator, element_id)
  end
end

#page_sourceObject

Gets the application hierarchy XML



128
129
130
# File 'lib/maze/driver/appium.rb', line 128

def page_source
  @driver.page_source
end

#reset_with_timeout(timeout = 0.1) ⇒ Object

Reset the currently running application after a given timeout

Parameters:

  • timeout (Number) (defaults to: 0.1)

    the amount of time in seconds to wait before resetting



184
185
186
187
# File 'lib/maze/driver/appium.rb', line 184

def reset_with_timeout(timeout = 0.1)
  sleep(timeout)
  reset
end

#send_keys(text) ⇒ Object

Send keys to the device without a specific element

Parameters:

  • text (String)

    the text to send



162
163
164
# File 'lib/maze/driver/appium.rb', line 162

def send_keys(text)
  @driver.send_keys(text)
end

#send_keys_to_element(element_id, text) ⇒ Object

Sends keys to a given element

Parameters:

  • element_id (String)

    the element to send text to

  • text (String)

    the text to send



141
142
143
144
145
146
# File 'lib/maze/driver/appium.rb', line 141

def send_keys_to_element(element_id, text)
  element = find_element_timed(element_id)
  @send_keys_timer.time do
    element.send_keys(text)
  end
end

#session_capabilitiesObject



193
194
195
# File 'lib/maze/driver/appium.rb', line 193

def session_capabilities
  driver.session_capabilities
end

#set_rotation(orientation) ⇒ Object

Sets the rotation of the device

Parameters:

  • orientation (Symbol)

    :portrait or :landscape



151
152
153
# File 'lib/maze/driver/appium.rb', line 151

def set_rotation(orientation)
  @driver.rotation = orientation
end

#start_driverObject

Starts the Appium driver



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/maze/driver/appium.rb', line 52

def start_driver
  begin
    $logger.info 'Starting Appium driver...'
    time = Time.now
    super
    $logger.info "Appium driver started in #{(Time.now - time).to_i}s"
  rescue => error
    $logger.warn "Appium driver failed to start in #{(Time.now - time).to_i}s"
    $logger.warn "#{error.class} occurred with message: #{error.message}"
    raise error
  end
end

#unlockObject

Unlocks the device



133
134
135
# File 'lib/maze/driver/appium.rb', line 133

def unlock
  @driver.unlock
end

#wait_for_element(element_id, timeout = 15, retry_if_stale = true) ⇒ Object

Checks for an element, waiting until it is present or the method times out

Parameters:

  • element_id (String)

    the element to search for

  • timeout (Integer) (defaults to: 15)

    the maximum time to wait for an element to be present in seconds

  • retry_if_stale (Boolean) (defaults to: true)

    enables the method to retry acquiring the element if a StaleObjectException occurs



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/maze/driver/appium.rb', line 70

def wait_for_element(element_id, timeout = 15, retry_if_stale = true)
  wait = Selenium::WebDriver::Wait.new(timeout: timeout)
  wait.until { find_element(@element_locator, element_id).displayed? }
rescue Selenium::WebDriver::Error::TimeoutError
  false
rescue Selenium::WebDriver::Error::StaleElementReferenceError => e
  if retry_if_stale
    wait_for_element(element_id, timeout, false)
  else
    $logger.warn "StaleElementReferenceError occurred: #{e}"
    false
  end
else
  true
end

#window_sizeObject



155
156
157
# File 'lib/maze/driver/appium.rb', line 155

def window_size
  @driver.window_size
end