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.



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

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.



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

def endpoint
  @endpoint
end

#global_scopeObject

Returns the value of attribute global_scope.



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

def global_scope
  @global_scope
end

#runningObject

Returns the value of attribute running.



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

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



157
158
159
160
161
162
163
164
165
166
167
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 157

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



136
137
138
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 136

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



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 183

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

.appium_local_capabilitiesObject



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 410

def self.appium_local_capabilities
  # specify endpoint url
  if @endpoint.nil?
    @endpoint = if ENV['APPIUM_SERVER_VERSION'] && ENV['APPIUM_SERVER_VERSION'].to_i == 1
                  'http://127.0.0.1:4723/wd/hub'
                else
                  'http://127.0.0.1:4723'
                end
  end
  # define local Appium capabilities
  if @capabilities.nil?
    Environ.device_name = ENV['APP_DEVICE']
    Environ.device_os = ENV['APP_PLATFORM_NAME']
    Environ.device_os_version = ENV['APP_VERSION']
    Environ.device = ENV['UDID'] ? :device : :simulator

    caps = {
      platformName: ENV['APP_PLATFORM_NAME'],
      'appium:platformVersion': ENV['APP_VERSION'],
      'appium:deviceName': ENV['APP_DEVICE'],
      'appium:automationName': ENV['AUTOMATION_ENGINE']
    }
    caps[:'appium:avd'] = ENV['APP_DEVICE'] if ENV['APP_PLATFORM_NAME'].downcase.to_sym == :android
    caps[:'appium:orientation'] = ENV['ORIENTATION'].upcase if ENV['ORIENTATION']
    if ENV['LOCALE'] && ENV['LANGUAGE']
      caps[:'appium:language'] = ENV['LANGUAGE']
      caps[:'appium:locale'] = if Environ.is_android?
                                 locale = ENV['LOCALE'].gsub('-', '_')
                                 locale.split('_')[1]
                               else
                                 ENV['LOCALE'].gsub('-', '_')
                               end
    end
    caps[:'appium:newCommandTimeout'] = ENV['NEW_COMMAND_TIMEOUT'] if ENV['NEW_COMMAND_TIMEOUT']
    caps[:'appium:noReset'] = ENV['APP_NO_RESET'] if ENV['APP_NO_RESET']
    caps[:'appium:fullReset'] = ENV['APP_FULL_RESET'] if ENV['APP_FULL_RESET']
    caps[:'appium:autoLaunch'] = ENV['AUTO_LAUNCH'] if ENV['AUTO_LAUNCH']
    caps[:'appium:webkitDebugProxyPort'] = ENV['WEBKIT_DEBUG_PROXY_PORT'] if ENV['WEBKIT_DEBUG_PROXY_PORT']
    caps[:'appium:webDriverAgentUrl'] = ENV['WEBDRIVER_AGENT_URL'] if ENV['WEBDRIVER_AGENT_URL']
    caps[:'appium:wdaLocalPort'] = ENV['WDA_LOCAL_PORT'] if ENV['WDA_LOCAL_PORT']
    caps[:'appium:usePrebuiltWDA'] = ENV['USE_PREBUILT_WDA'] if ENV['USE_PREBUILT_WDA']
    caps[:'appium:useNewWDA'] = ENV['USE_NEW_WDA'] if ENV['USE_NEW_WDA']
    caps[:'appium:autoWebview'] = ENV['AUTO_WEBVIEW'] if ENV['AUTO_WEBVIEW']
    caps[:'appium:chromedriverExecutable'] = ENV['CHROMEDRIVER_EXECUTABLE'] if ENV['CHROMEDRIVER_EXECUTABLE']
    caps[:'appium:autoWebviewTimeout'] = ENV['AUTO_WEBVIEW_TIMEOUT'] if ENV['AUTO_WEBVIEW_TIMEOUT']
    caps[:'appium:udid'] = ENV['UDID'] if ENV['UDID']
    caps[:'appium:xcodeOrgId'] = ENV['TEAM_ID'] if ENV['TEAM_ID']
    caps[:'appium:xcodeSigningId'] = ENV['TEAM_NAME'] if ENV['TEAM_NAME']
    caps[:'appium:appActivity'] = ENV['APP_ACTIVITY'] if ENV['APP_ACTIVITY']
    caps[:'appium:appPackage'] = ENV['APP_PACKAGE'] if ENV['APP_PACKAGE']
    caps[:'appium:forceSimulatorSoftwareKeyboardPresence'] = ENV['SHOW_SIM_KEYBOARD'] if ENV['SHOW_SIM_KEYBOARD']
    if Environ.is_ios?
      caps[:'appium:webviewConnectTimeout'] = 90000
      caps[:'appium:maxTypingFrequency'] = 15
      caps[:'appium:respectSystemAlerts'] = true
    end

    if ENV['BUNDLE_ID']
      caps[:'appium:bundleId'] = ENV['BUNDLE_ID']
    else
      app_id = get_app_id
      caps[:'appium:bundleId'] = app_id unless app_id.nil?
    end

    caps[:'appium:app'] = if ENV['APP']
                            ENV['APP']
                          else
                            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
                          end
    caps
  else
    Environ.device_os = @capabilities[:platformName]
    Environ.device_name = @capabilities[:'appium:deviceName']
    Environ.device_os_version = @capabilities[:'appium:platformVersion']
    Environ.device_orientation = @capabilities[:'appium:orientation']
    Environ.device = @capabilities[:'appium:udid'] ? :device : :simulator
    @capabilities
  end
end

.available_contextsObject



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

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



146
147
148
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 146

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

.biometric_match(type, match) ⇒ Object



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

def self.biometric_match(type, match)
  raise 'biometric_match is not supported for this platform' unless Environ.is_ios?

  @driver.execute_script('mobile: sendBiometricMatch', { type: type, match: match })
end

.browserstack_capabilitiesObject



506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 506

def self.browserstack_capabilities
  # specify endpoint url
  @endpoint = "https://#{ENV['BS_USERNAME']}:#{ENV['BS_AUTHKEY']}@hub-cloud.browserstack.com/wd/hub" if @endpoint.nil?
  # define BrowserStack options
  options = if @capabilities.nil?
              Environ.device_name = ENV['BS_DEVICE']
              Environ.device_os = ENV['BS_OS']
              Environ.device_os_version = ENV['BS_OS_VERSION']
              # define the required set of BrowserStack options
              bs_options = {
                userName: ENV['BS_USERNAME'],
                accessKey: ENV['BS_AUTHKEY'],
                sessionName: test_context_message
              }
              # define the optional BrowserStack options
              bs_options[:projectName] = ENV['AUTOMATE_PROJECT'] if ENV['AUTOMATE_PROJECT']
              bs_options[:buildName] = ENV['AUTOMATE_BUILD'] if ENV['AUTOMATE_BUILD']
              bs_options[:geoLocation] = ENV['IP_GEOLOCATION'] if ENV['IP_GEOLOCATION']
              bs_options[:timezone] = ENV['TIME_ZONE'] if ENV['TIME_ZONE']
              bs_options[:video] = ENV['RECORD_VIDEO'] if ENV['RECORD_VIDEO']
              bs_options[:debug] = ENV['SCREENSHOTS'] if ENV['SCREENSHOTS']
              bs_options[:local] = ENV['TUNNELING'] if ENV['TUNNELING']
              bs_options[:deviceOrientation] = ENV['ORIENTATION'] if ENV['ORIENTATION']
              bs_options[:appiumLogs] = ENV['APPIUM_LOGS'] if ENV['APPIUM_LOGS']
              bs_options[:networkLogs] = ENV['NETWORK_LOGS'] if ENV['NETWORK_LOGS']
              bs_options[:deviceLogs] = ENV['DEVICE_LOGS'] if ENV['DEVICE_LOGS']
              bs_options[:networkProfile] = ENV['NETWORK_PROFILE'] if ENV['NETWORK_PROFILE']
              bs_options[:idleTimeout] = ENV['IDLE_TIMEOUT'] if ENV['IDLE_TIMEOUT']
              bs_options[:resignApp] = ENV['RESIGN_APP'] if ENV['RESIGN_APP']
              bs_options[:gpsLocation] = ENV['GPS_LOCATION'] if ENV['GPS_LOCATION']
              bs_options[:acceptInsecureCerts] = ENV['ACCEPT_INSECURE_CERTS'] if ENV['ACCEPT_INSECURE_CERTS']
              bs_options[:disableAnimations] = ENV['DISABLE_ANIMATION'] if ENV['DISABLE_ANIMATION']
              bs_options[:appiumVersion] = ENV['APPIUM_VERSION'] ? ENV['APPIUM_VERSION'] : '2.19.0'

              capabilities = {
                platformName: ENV['BS_OS'],
                'appium:platformVersion': ENV['BS_OS_VERSION'],
                'appium:deviceName': ENV['BS_DEVICE'],
                'appium:automationName': ENV['AUTOMATION_ENGINE'],
                'appium:app': ENV['APP'],
                'bstack:options': bs_options
              }
              capabilities[:language] = ENV['LANGUAGE'] if ENV['LANGUAGE']
              capabilities[:locale] = ENV['LOCALE'] if ENV['LOCALE']
              capabilities
            else
              Environ.device_os = @capabilities[:platformName]
              Environ.device_name = @capabilities[:'appium:deviceName']
              Environ.device_os_version = @capabilities[:'appium:platformVersion']
              @capabilities
            end
  # BrowserStack uses only real devices
  Environ.device = :device
  upload_app(:browserstack) if ENV['UPLOAD_APP']
  options
end

.current_contextObject



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

def self.current_context
  driver.current_context
end

.custom_capabilitiesObject



496
497
498
499
500
501
502
503
504
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 496

def self.custom_capabilities
  raise 'User-defined cloud driver requires that you provide capabilities' if @capabilities.nil?
  raise 'User-defined cloud driver requires that you provide an endpoint' if @endpoint.nil?

  Environ.device_os = @capabilities[:platformName]
  Environ.device_name = @capabilities[:'appium:deviceName']
  Environ.device_os_version = @capabilities[:'appium:platformVersion']
  @capabilities
end

.driverObject

Return a reference to last created Appium driver



91
92
93
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 91

def self.driver
  @driver
end

.geolocationObject



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

def self.geolocation
  driver.driver.location
end

.get_app_id(bundle_id = nil) ⇒ Object



395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 395

def self.get_app_id(bundle_id = nil)
  return bundle_id unless bundle_id.nil?

  case
  when Environ.is_ios?
    Environ.current.ios_bundle_id
  when Environ.is_android?
    Environ.current.android_app_id
  when Environ.is_macos?
    Environ.current.mac_bundle_id
  else
    nil
  end
end

.hide_keyboardObject

Hide the onscreen keyboard



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

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



222
223
224
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 222

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.



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

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
  Environ.driver_name = "#{Environ.driver}_#{Environ.device_os}_#{Environ.device_type}".downcase.to_sym unless Environ.driver_name
  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



113
114
115
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 113

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

.is_biometric_enrolled?Boolean

Returns:

  • (Boolean)


309
310
311
312
313
314
315
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 309

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)


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

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

.is_webview?Boolean

Returns:

  • (Boolean)


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

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.



236
237
238
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 236

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

.orientationSymbol

Get the current screen orientation

Returns:

  • (Symbol)

    :landscape or :portrait



244
245
246
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 244

def self.orientation
  driver.driver.orientation
end

.quit_driverObject

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



81
82
83
84
85
86
87
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 81

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



124
125
126
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 124

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



252
253
254
255
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 252

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

.sauce_capabilitiesObject

:nocov:



608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 608

def self.sauce_capabilities
  # specify endpoint url
  @endpoint = "https://#{ENV['SL_USERNAME']}:#{ENV['SL_AUTHKEY']}@ondemand.#{ENV['SL_DATA_CENTER']}.saucelabs.com:443/wd/hub" if @endpoint.nil?
  # define SauceLabs options
  if @capabilities.nil?
    Environ.device_name = ENV['SL_DEVICE']
    Environ.device_os = ENV['SL_OS']
    Environ.device_os_version = ENV['SL_OS_VERSION']
    # define the required set of SauceLabs options
    sl_options = { build: test_context_message }
    # define the optional SauceLabs options
    sl_options[:name] = ENV['AUTOMATE_PROJECT'] if ENV['AUTOMATE_PROJECT']
    sl_options[:deviceOrientation] = ENV['ORIENTATION'].upcase if ENV['ORIENTATION']
    sl_options[:recordVideo] = ENV['RECORD_VIDEO'] if ENV['RECORD_VIDEO']
    sl_options[:recordScreenshots] = ENV['SCREENSHOTS'] if ENV['SCREENSHOTS']
    sl_options[:appiumVersion] = ENV['APPIUM_VERSION'] ? ENV['APPIUM_VERSION'] : '2.1.3'
    {
      platformName: ENV['SL_OS'],
      'appium:platformVersion': ENV['SL_OS_VERSION'],
      'appium:deviceName': ENV['SL_DEVICE'],
      'appium:automationName': ENV['AUTOMATION_ENGINE'],
      'appium:app': ENV['APP'],
      'sauce:options': sl_options
    }
  else
    Environ.device_os = @capabilities[:platformName]
    Environ.device_name = @capabilities[:'appium:deviceName']
    Environ.device_os_version = @capabilities[:'appium:platformVersion']
    @capabilities
  end
end

.set_biometric_enrollment(state) ⇒ Object



317
318
319
320
321
322
323
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 317

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



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

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

.set_geolocation(location_data) ⇒ Object



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

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



101
102
103
104
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 101

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



210
211
212
213
214
215
216
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 210

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

.test_context_messageObject



640
641
642
643
644
645
646
647
648
649
650
651
652
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 640

def self.test_context_message
  context_message = if ENV['TEST_CONTEXT']
                      "#{Environ.test_environment.to_s.upcase} - #{ENV['TEST_CONTEXT']}"
                    else
                      Environ.test_environment.to_s.upcase
                    end
  if ENV['PARALLEL']
    thread_num = ENV['TEST_ENV_NUMBER']
    thread_num = 1 if thread_num.blank?
    context_message = "#{context_message} - Thread ##{thread_num}"
  end
  context_message
end

.testingbot_capabilitiesObject



563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 563

def self.testingbot_capabilities
  Environ.device = :simulator
  # specify endpoint url
  @endpoint = "https://#{ENV['TB_USERNAME']}:#{ENV['TB_AUTHKEY']}@hub.testingbot.com/wd/hub" if @endpoint.nil?
  # define TestingBot options
  options = if @capabilities.nil?
              Environ.device_name = ENV['TB_DEVICE']
              Environ.device_os = ENV['TB_OS']
              Environ.device_os_version = ENV['TB_OS_VERSION']
              Environ.device = :device if ENV['REAL_DEVICE'] == 'true'
              # define the required set of TestingBot options
              tb_options = { build: test_context_message }
              # define the optional TestingBot options
              tb_options[:name] = ENV['AUTOMATE_PROJECT'] if ENV['AUTOMATE_PROJECT']
              tb_options[:timeZone] = ENV['TIME_ZONE'] if ENV['TIME_ZONE']
              tb_options['testingbot.geoCountryCode'] = ENV['IP_GEOLOCATION'] if ENV['IP_GEOLOCATION']
              tb_options[:screenrecorder] = ENV['RECORD_VIDEO'] if ENV['RECORD_VIDEO']
              tb_options[:screenshot] = ENV['SCREENSHOTS'] if ENV['SCREENSHOTS']
              tb_options[:appiumVersion] = ENV['APPIUM_VERSION'] ? ENV['APPIUM_VERSION'] : 'latest'

              capabilities = {
                platformName: ENV['TB_OS'],
                'appium:platformVersion': ENV['TB_OS_VERSION'],
                'appium:deviceName': ENV['TB_DEVICE'],
                'appium:automationName': ENV['AUTOMATION_ENGINE'],
                'appium:app': ENV['APP'],
                'tb:options': tb_options
              }
              capabilities[:'appium:realDevice'] = ENV['REAL_DEVICE'] if ENV['REAL_DEVICE']
              capabilities
            else
              Environ.device_os = @capabilities[:platformName]
              Environ.device_name = @capabilities[:'appium:deviceName']
              Environ.device_os_version = @capabilities[:'appium:platformVersion']
              if @capabilities.key?(:'appium:realDevice') && @capabilities[:'appium:realDevice'] == true
                Environ.device = :device
              end
              @capabilities
            end

  upload_app(:testingbot) if ENV['UPLOAD_APP']
  options
end

.upload_app(service) ⇒ Object

:nocov:



332
333
334
335
336
337
338
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
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 332

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)
  raise "An error has occurred while attempting to upload #{file_path} to the #{service} service\n#{result}" if response.code.to_i > 202

  puts "Successfully uploaded #{file_path} to the #{service} service\n#{result}"
end

.webview_contextObject



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

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)


270
271
272
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 270

def self.window_rect
  driver.window_rect
end

.window_sizeArray

Get the device's window size.

Returns:

  • (Array)

    window size as [width, height]



261
262
263
264
# File 'lib/testcentricity_apps/app_core/appium_connect_helper.rb', line 261

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