Module: TestCentricity::AppiumConnect

Defined in:
lib/testcentricity/app_core/appium_connect_helper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#capabilitiesObject

Returns the value of attribute capabilities.



11
12
13
# File 'lib/testcentricity/app_core/appium_connect_helper.rb', line 11

def capabilities
  @capabilities
end

#endpointObject

Returns the value of attribute endpoint.



10
11
12
# File 'lib/testcentricity/app_core/appium_connect_helper.rb', line 10

def endpoint
  @endpoint
end

#runningObject

Returns the value of attribute running.



9
10
11
# File 'lib/testcentricity/app_core/appium_connect_helper.rb', line 9

def running
  @running
end

Class Method Details

.activate_app(bundle_id = nil) ⇒ Object



127
128
129
# File 'lib/testcentricity/app_core/appium_connect_helper.rb', line 127

def self.activate_app(bundle_id = nil)
  $driver.activate_app(get_app_id(bundle_id))
end

.app_installed?(bundle_id = nil) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/testcentricity/app_core/appium_connect_helper.rb', line 103

def self.app_installed?(bundle_id = nil)
  $driver.app_installed?(get_app_id(bundle_id))
end

.app_state(bundle_id = nil) ⇒ Object



131
132
133
# File 'lib/testcentricity/app_core/appium_connect_helper.rb', line 131

def self.app_state(bundle_id = nil)
  $driver.app_state(get_app_id(bundle_id))
end

.available_contextsObject



175
176
177
# File 'lib/testcentricity/app_core/appium_connect_helper.rb', line 175

def self.available_contexts
  $driver.available_contexts
end

.background_app(duration) ⇒ Object



111
112
113
# File 'lib/testcentricity/app_core/appium_connect_helper.rb', line 111

def self.background_app(duration)
  $driver.background_app(duration)
end

.close_appObject



115
116
117
# File 'lib/testcentricity/app_core/appium_connect_helper.rb', line 115

def self.close_app
  $driver.close_app
end

.current_contextObject



167
168
169
# File 'lib/testcentricity/app_core/appium_connect_helper.rb', line 167

def self.current_context
  $driver.current_context
end

.default_contextObject



179
180
181
182
# File 'lib/testcentricity/app_core/appium_connect_helper.rb', line 179

def self.default_context
  $driver.switch_to_default_context
  puts "Current context = #{$driver.current_context}" if ENV['DEBUG']
end

.driverObject



90
91
92
# File 'lib/testcentricity/app_core/appium_connect_helper.rb', line 90

def self.driver
  $driver
end

.geolocationObject



159
160
161
# File 'lib/testcentricity/app_core/appium_connect_helper.rb', line 159

def self.geolocation
  $driver.location
end

.hide_keyboardObject



143
144
145
# File 'lib/testcentricity/app_core/appium_connect_helper.rb', line 143

def self.hide_keyboard
  $driver.hide_keyboard
end

.implicit_wait(timeout) ⇒ Object



139
140
141
# File 'lib/testcentricity/app_core/appium_connect_helper.rb', line 139

def self.implicit_wait(timeout)
  $driver.manage.timeouts.implicit_wait = timeout
end

.initialize_appium(options = nil) ⇒ Object



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
46
47
48
49
50
51
# File 'lib/testcentricity/app_core/appium_connect_helper.rb', line 13

def self.initialize_appium(options = nil)
  @endpoint = nil
  @capabilities = nil
  if options.is_a?(Hash)
    @endpoint = options[:endpoint] if options.key?(:endpoint)
    @capabilities = options[:desired_capabilities] if options.key?(:desired_capabilities)
  end
  Environ.platform = :mobile
  Environ.device_type = ENV['DEVICE_TYPE'] if ENV['DEVICE_TYPE']

  Environ.device_orientation = ENV['ORIENTATION'] if ENV['ORIENTATION']
  Environ.device_os_version = ENV['APP_VERSION']
  Environ.driver = ENV['DRIVER'].downcase.to_sym
  case Environ.driver
  when :appium
    Environ.device_name = ENV['APP_DEVICE']
    Environ.device_os = ENV['APP_PLATFORM_NAME']
    Environ.device = ENV['UDID'] ? :device : :simulator
  when :browserstack
    Environ.device_name = ENV['BS_DEVICE']
    Environ.device_os = ENV['BS_OS']
    Environ.device = :device
    upload_app(:browserstack) if ENV['UPLOAD_APP']
  when :saucelabs
    Environ.device_name = ENV['SL_DEVICE']
    Environ.device_os = ENV['SL_OS']
  when :testingbot
    Environ.device_name = ENV['TB_DEVICE']
    Environ.device_os = ENV['TB_OS']
    Environ.device = :device
    upload_app(:testingbot) if ENV['UPLOAD_APP']
  when :lambdatest
    Environ.device_name = ENV['LT_DEVICE']
    Environ.device_os = ENV['LT_OS']
  else
    raise "#{Environ.driver} is not a valid selector"
  end
  @running = false
end

.install_app(app_path) ⇒ Object



99
100
101
# File 'lib/testcentricity/app_core/appium_connect_helper.rb', line 99

def self.install_app(app_path)
  $driver.install_app(app_path)
end

.is_native_app?Boolean

Returns:

  • (Boolean)


188
189
190
# File 'lib/testcentricity/app_core/appium_connect_helper.rb', line 188

def self.is_native_app?
  $driver.current_context.start_with?('NATIVE_APP')
end

.is_webview?Boolean

Returns:

  • (Boolean)


184
185
186
# File 'lib/testcentricity/app_core/appium_connect_helper.rb', line 184

def self.is_webview?
  $driver.current_context.start_with?('WEBVIEW')
end

.keyboard_shown?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/testcentricity/app_core/appium_connect_helper.rb', line 147

def self.keyboard_shown?
  $driver.is_keyboard_shown
end

.launch_appObject



107
108
109
# File 'lib/testcentricity/app_core/appium_connect_helper.rb', line 107

def self.launch_app
  $driver.launch_app
end

.orientationObject



151
152
153
# File 'lib/testcentricity/app_core/appium_connect_helper.rb', line 151

def self.orientation
  $driver.orientation
end

.quit_driverObject



83
84
85
86
87
88
# File 'lib/testcentricity/app_core/appium_connect_helper.rb', line 83

def self.quit_driver
  if @running
    $driver.driver_quit
    @running = false
  end
end

.remove_app(bundle_id = nil) ⇒ Object



123
124
125
# File 'lib/testcentricity/app_core/appium_connect_helper.rb', line 123

def self.remove_app(bundle_id = nil)
  $driver.remove_app(get_app_id(bundle_id))
end

.reset_appObject



119
120
121
# File 'lib/testcentricity/app_core/appium_connect_helper.rb', line 119

def self.reset_app
  $driver.reset
end

.set_context(context) ⇒ Object



171
172
173
# File 'lib/testcentricity/app_core/appium_connect_helper.rb', line 171

def self.set_context(context)
  $driver.set_context(context)
end

.set_geolocation(latitude, longitude, altitude) ⇒ Object



163
164
165
# File 'lib/testcentricity/app_core/appium_connect_helper.rb', line 163

def self.set_geolocation(latitude, longitude, altitude)
  $driver.set_location(latitude, longitude, altitude)
end

.set_orientation(orientation) ⇒ Object



155
156
157
# File 'lib/testcentricity/app_core/appium_connect_helper.rb', line 155

def self.set_orientation(orientation)
  $driver.rotation = orientation.downcase.to_sym
end

.start_driverObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/testcentricity/app_core/appium_connect_helper.rb', line 53

def self.start_driver
  capabilities = case Environ.driver
                 when :appium
                   appium_local_capabilities
                 when :browserstack
                   browserstack_capabilities
                 when :saucelabs
                   sauce_capabilities
                 when :testingbot
                   testingbot_capabilities
                 when :lambdatest
                   lambdatest_capabilities
                 else
                   raise "#{Environ.driver} is not a valid selector"
                 end
  puts "Appium desired_capabilities = #{capabilities}" if ENV['DEBUG']

  desired_capabilities = {
    caps: capabilities,
    appium_lib: { server_url: @endpoint }
  }
  Appium::Driver.new(desired_capabilities, true).start_driver
  @running = true
  Appium.promote_appium_methods(TestCentricity::ScreenObject)
  Appium.promote_appium_methods(TestCentricity::ScreenSection)
  Appium.promote_appium_methods(TestCentricity::AppUIElement)

  Environ.screen_size = $driver.window_size
end

.take_screenshot(png_save_path) ⇒ Object



94
95
96
97
# File 'lib/testcentricity/app_core/appium_connect_helper.rb', line 94

def self.take_screenshot(png_save_path)
  FileUtils.mkdir_p(File.dirname(png_save_path))
  $driver.driver.save_screenshot(png_save_path)
end

.terminate_app(bundle_id = nil) ⇒ Object



135
136
137
# File 'lib/testcentricity/app_core/appium_connect_helper.rb', line 135

def self.terminate_app(bundle_id = nil)
  $driver.terminate_app(get_app_id(bundle_id))
end

.upload_app(service) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/testcentricity/app_core/appium_connect_helper.rb', line 199

def self.upload_app(service)
  case service
  when :browserstack
    url = 'https://api-cloud.browserstack.com/app-automate/upload'
    user_id = ENV['BS_USERNAME']
    auth_key = ENV['BS_AUTHKEY']
  when :testingbot
    url = 'https://api.testingbot.com/v1/storage'
    user_id = ENV['TB_USERNAME']
    auth_key = ENV['TB_AUTHKEY']
  else
    raise "#{service} is not a valid selector"
  end

  file_path = if Environ.is_android?
                Environ.current.android_apk_path
              elsif Environ.is_ios?
                Environ.is_device? ?
                  Environ.current.ios_ipa_path :
                  Environ.current.ios_app_path
              end

  c = Curl::Easy.new(url)
  c.http_auth_types = :basic
  c.username = user_id
  c.password = auth_key
  c.multipart_form_post = true
  c.http_post(Curl::PostField.file('file', file_path))
  result = JSON.parse(c.body_str)
  puts "app_url = #{result['app_url']}"
  ENV['APP'] = result['app_url']
end

.webview_contextObject



192
193
194
195
196
197
# File 'lib/testcentricity/app_core/appium_connect_helper.rb', line 192

def self.webview_context
  contexts = $driver.available_contexts
  puts "Contexts = #{contexts}" if ENV['DEBUG']
  set_context(contexts.last)
  puts "Current context = #{$driver.current_context}" if ENV['DEBUG']
end