Module: TestCentricity::AppiumConnect

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#capabilitiesObject

Returns the value of attribute capabilities.



13
14
15
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 13

def capabilities
  @capabilities
end

#driverObject

Returns the value of attribute driver.



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

def driver
  @driver
end

#endpointObject

Returns the value of attribute endpoint.



12
13
14
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 12

def endpoint
  @endpoint
end

#global_scopeObject

Returns the value of attribute global_scope.



14
15
16
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 14

def global_scope
  @global_scope
end

#runningObject

Returns the value of attribute running.



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

def running
  @running
end

Class Method Details

.activate_app(bundle_id = nil) ⇒ Object

Launch the app. If bundle_id is not specified, then bundle id will be retrieved from Environ.current.ios_bundle_id, Environ.current.android_app_id, or Environ.current.mac_bundle_id dependent on which platform is being tested.

Examples:

AppiumConnect.activate_app('com.saucelabs.mydemoapp.rn')

Parameters:

  • bundle_id (String) (defaults to: nil)

    OPTIONAL bundle id of app



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 162

def self.activate_app(bundle_id = nil)
  if Environ.is_macos?
    @driver.execute_script('macos: activateApp', { bundleId: get_app_id(bundle_id) })
  else
    driver.activate_app(get_app_id(bundle_id))
    if Environ.is_android?
      sleep(1.5) if app_state == :running_in_foreground
    end
  end
  Environ.new_app_session
end

.app_installed?(bundle_id = nil) ⇒ Boolean

Is the app installed? If bundle_id is not specified, then bundle id will be retrieved from Environ.current.ios_bundle_id or Environ.current.android_app_id dependent on which platform is being tested.

Examples:

AppiumConnect.app_installed?('com.saucelabs.mydemoapp.rn')

Parameters:

  • bundle_id (String) (defaults to: nil)

    OPTIONAL bundle id of app

Returns:

  • (Boolean)

    TRUE if app is installed



141
142
143
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 141

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

.app_state(bundle_id = nil) ⇒ Symbol

Get status of app. If bundle_id is not specified, then bundle id will be retrieved from Environ.current.ios_bundle_id, Environ.current.android_app_id, or Environ.current.mac_bundle_id dependent on which platform is being tested. Returns the following statuses: :not_installed - The current application state cannot be determined/is unknown :not_running - The application is not running :running_in_background_suspended - The application is running in the background and is suspended :running_in_background - The application is running in the background and is not suspended :running_in_foreground - The application is running in the foreground

Examples:

AppiumConnect.app_state('com.saucelabs.mydemoapp.rn')

Parameters:

  • bundle_id (String) (defaults to: nil)

    OPTIONAL bundle id of app

Returns:

  • (Symbol)

    status of app



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 188

def self.app_state(bundle_id = nil)
  if Environ.is_macos?
    state = @driver.execute_script('macos: queryAppState', { bundleId: get_app_id(bundle_id) })
    case state
    when 0
      :not_installed
    when 1
      :not_running
    when 2
      :running_in_background_suspended
    when 3
      :running_in_background
    when 4
      :running_in_foreground
    end
  else
    driver.app_state(get_app_id(bundle_id))
  end
end

.available_contextsObject



295
296
297
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 295

def self.available_contexts
  driver.available_contexts
end

.background_app(duration = 0) ⇒ Object

Backgrounds the app for a specified number of seconds.

Examples:

AppiumConnect.background_app(20)

Parameters:

  • duration (Integer) (defaults to: 0)

    number of seconds to background the app for



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

def self.background_app(duration = 0)
  driver.background_app(duration)
end

.biometric_match(type, match) ⇒ Object



330
331
332
333
334
335
336
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 330

def self.biometric_match(type, match)
  if Environ.is_ios?
    @driver.execute_script('mobile: sendBiometricMatch', { type: type, match: match })
  else
    raise 'biometric_match is not supported for this platform'
  end
end

.current_contextObject



287
288
289
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 287

def self.current_context
  driver.current_context
end

.driverObject

Return a reference to last created Appium driver



96
97
98
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 96

def self.driver
  @driver
end

.geolocationObject



279
280
281
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 279

def self.geolocation
  driver.driver.location
end

.hide_keyboardObject

Hide the onscreen keyboard



233
234
235
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 233

def self.hide_keyboard
  driver.hide_keyboard
end

.implicit_wait(timeout) ⇒ Object

Set the amount of time the driver should wait when searching for elements.

Parameters:

  • timeout (Integer)

    number of seconds to wait



227
228
229
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 227

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

.initialize_appium(options = nil) ⇒ Object

Create a new driver instance with capabilities specified in the options parameter, or via Environment Variables. Refer to the Connecting to a Mobile Simulator or Device section of the ruby docs for this gem.



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
52
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
82
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 19

def self.initialize_appium(options = nil)
  @endpoint = nil
  @capabilities = nil
  @running = false
  @global_scope = false
  Environ.driver_name = nil
  Environ.device_orientation = nil
  Environ.platform = :mobile
  Environ.device = :simulator

  if options.is_a?(Hash)
    @endpoint = options[:endpoint] if options.key?(:endpoint)
    @capabilities = options[:capabilities] if options.key?(:capabilities)
    @global_scope = options[:global_driver] if options.key?(:global_driver)
    Environ.driver = options[:driver] if options.key?(:driver)
    Environ.driver_name = options[:driver_name] if options.key?(:driver_name)
    Environ.device_type = options[:device_type] if options.key?(:device_type)
  end
  if @capabilities.nil?
    Environ.driver = ENV['DRIVER'].downcase.to_sym if ENV['DRIVER']
    Environ.device_type = ENV['DEVICE_TYPE'] if ENV['DEVICE_TYPE']
    Environ.device_orientation = ENV['ORIENTATION'] if ENV['ORIENTATION']
  else
    raise ':browserName is specified in capabilities' if @capabilities[:browserName]
  end

  raise 'You must specify a driver' if Environ.driver.nil?
  raise 'You must specify a device type - :phone or :tablet' if Environ.device_type.nil?

  driver_caps = case Environ.driver
                when :appium
                  appium_local_capabilities
                when :custom
                  raise 'User-defined cloud driver requires that you define options' if options.nil?
                  custom_capabilities
                when :browserstack
                  browserstack_capabilities
                when :testingbot
                  testingbot_capabilities
                  # :nocov:
                when :saucelabs
                  sauce_capabilities
                else
                  raise "#{Environ.driver} is not a supported driver"
                  # :nocov:
                end
  driver_opts = {
    caps: driver_caps,
    appium_lib: { server_url: @endpoint }
  }
  @driver = Appium::Driver.new(driver_opts, global_driver = @global_scope)
  @driver.start_driver
  Environ.appium_driver = @driver
  @running = true
  Appium.promote_appium_methods(TestCentricity::ScreenObject, driver = @driver)
  Appium.promote_appium_methods(TestCentricity::ScreenSection, driver = @driver)
  Appium.promote_appium_methods(TestCentricity::AppElements::AppUIElement, driver = @driver)

  Environ.screen_size = window_size
  unless Environ.driver_name
    Environ.driver_name = "#{Environ.driver}_#{Environ.device_os}_#{Environ.device_type}".downcase.to_sym
  end
  Environ.session_state = :running
end

.install_app(app_path) ⇒ Object

Install app on device. If bundle_id is not specified, then bundle id will be retrieved from Environ.current.ios_bundle_id or Environ.current.android_app_id dependent on which platform is being tested.

Examples:

AppiumConnect.install_app('com.saucelabs.mydemoapp.rn')

Parameters:

  • bundle_id (String)

    OPTIONAL bundle id of app



118
119
120
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 118

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

.is_biometric_enrolled?Boolean

Returns:

  • (Boolean)


314
315
316
317
318
319
320
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 314

def self.is_biometric_enrolled?
  if Environ.is_ios?
    @driver.execute_script('mobile: isBiometricEnrolled')
  else
    puts 'biometric_enrollment is not supported for this platform'
  end
end

.is_native_app?Boolean

Returns:

  • (Boolean)


303
304
305
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 303

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

.is_webview?Boolean

Returns:

  • (Boolean)


299
300
301
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 299

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

.keyboard_shown?Boolean

Is onscreen keyboard displayed?

Returns:

  • (Boolean)

    TRUE if keyboard is shown. Return false if keyboard is hidden.



241
242
243
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 241

def self.keyboard_shown?
  @driver.execute_script('mobile: isKeyboardShown')
end

.orientationSymbol

Get the current screen orientation

Returns:

  • (Symbol)

    :landscape or :portrait



249
250
251
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 249

def self.orientation
  driver.driver.orientation
end

.quit_driverObject

Quit the running driver instance. Sets Environ.session_state to :quit.



86
87
88
89
90
91
92
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 86

def self.quit_driver
  if @running
    driver.driver_quit
    @running = false
  end
  Environ.session_state = :quit
end

.remove_app(bundle_id = nil) ⇒ Object

Remove app from device. If bundle_id is not specified, then bundle id will be retrieved from Environ.current.ios_bundle_id or Environ.current.android_app_id dependent on which platform is being tested.

Examples:

AppiumConnect.remove_app('com.saucelabs.mydemoapp.rn')

Parameters:

  • bundle_id (String) (defaults to: nil)

    OPTIONAL bundle id of app



129
130
131
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 129

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

.rotation(orientation) ⇒ Object

Change the screen orientation

Parameters:

  • orientation (Symbol or String)

    :landscape or :portrait



257
258
259
260
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 257

def self.rotation(orientation)
  orientation.downcase.to_sym if orientation.is_a?(String)
  driver.driver.rotation = orientation
end

.set_biometric_enrollment(state) ⇒ Object



322
323
324
325
326
327
328
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 322

def self.set_biometric_enrollment(state)
  if Environ.is_ios?
    @driver.execute_script('mobile: enrollBiometric', { isEnabled: state })
  else
    puts 'biometric_enrollment is not supported for this platform'
  end
end

.set_context(context) ⇒ Object



291
292
293
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 291

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

.set_geolocation(location_data) ⇒ Object



283
284
285
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 283

def self.set_geolocation(location_data)
  driver.set_location(location_data)
end

.take_screenshot(png_save_path) ⇒ Object

Save a screenshot in .png format to the specified file path.

Examples:

AppiumConnect.take_screenshot('reports/screenshots/login_screen.png')

Parameters:

  • png_save_path (String)

    path to save the screenshot



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

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

Terminate the app. If bundle_id is not specified, then bundle id will be retrieved from Environ.current.ios_bundle_id, Environ.current.android_app_id, or Environ.current.mac_bundle_id dependent on which platform is being tested.

Examples:

AppiumConnect.terminate_app('com.saucelabs.mydemoapp.rn')

Parameters:

  • bundle_id (String) (defaults to: nil)

    OPTIONAL bundle id of app



215
216
217
218
219
220
221
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 215

def self.terminate_app(bundle_id = nil)
  if Environ.is_macos?
    @driver.execute_script('macos: terminateApp', { bundleId: get_app_id(bundle_id) })
  else
    driver.terminate_app(get_app_id(bundle_id))
  end
end

.upload_app(service) ⇒ Object

:nocov:



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 339

def self.upload_app(service)
 # determine app custom test id (if specified)
 custom_id = if ENV['APP_ID']
               ENV['APP_ID']
             else
               Environ.is_android? ? Environ.current.android_test_id : Environ.current.ios_test_id
             end
 # determine endpoint url, user id, and auth key for specified cloud service provider
 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'
   url = "#{url}/#{custom_id}" unless custom_id.nil?
   user_id = ENV['TB_USERNAME']
   auth_key = ENV['TB_AUTHKEY']
 else
   raise "#{service} is not supported"
 end
 # determine file path of app to be uploaded to cloud service
 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

 request = Net::HTTP::Post.new(url)
 boundary = "WebAppBoundary"
 post_body = []
 post_body << "--#{boundary}\r\n"
 post_body << "Content-Disposition: form-data; name=\"file\"; filename=\"#{file_path}\"\r\n"
 post_body << "\r\n"
 post_body << File.open(file_path) {|io| io.read}
 # add custom id form data to post body if a custom test id has been provided
 if !custom_id.nil? && service == :browserstack
   post_body << "\r\n--#{boundary}\r\n"
   post_body << "Content-Disposition: form-data; name=\"custom_id\"\r\n"
   post_body << "\r\n"
   post_body << "#{custom_id}"
 end
 post_body << "\r\n--#{boundary}--\r\n"
 request.body = post_body.join
 request["Content-Type"] = "multipart/form-data; boundary=#{boundary}"
 request.basic_auth(user_id, auth_key)
 # send the request to upload to cloud service provider
 uri = URI.parse(url)
 conn = Net::HTTP.new(uri.host, uri.port)
 if uri.scheme == 'https'
   conn.use_ssl = true
   conn.verify_mode = OpenSSL::SSL::VERIFY_NONE
 end
 response = conn.request(request)
 result = JSON.parse(response.body)
 if response.code.to_i > 202
   raise "An error has occurred while attempting to upload #{file_path} to the #{service} service\n#{result}"
 else
   puts "Successfully uploaded #{file_path} to the #{service} service\n#{result}"
 end
end

.webview_contextObject



307
308
309
310
311
312
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 307

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

.window_rectSelenium::WebDriver::Rectangle

Get the device's window rectangle.

Returns:

  • (Selenium::WebDriver::Rectangle)


275
276
277
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 275

def self.window_rect
  driver.window_rect
end

.window_sizeArray

Get the device's window size.

Returns:

  • (Array)

    window size as [width, height]



266
267
268
269
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 266

def self.window_size
  size = driver.window_size
  [size.width, size.height]
end