Class: Maze::Client::Appium::BitBarDevices

Inherits:
Object
  • Object
show all
Defined in:
lib/maze/client/appium/bb_devices.rb

Overview

Provides a source of capabilities used to run tests against specific BitBar devices noinspection RubyStringKeysInHashInspection

Class Method Summary collapse

Class Method Details

.get_available_device(device_or_group_names) ⇒ Object

Uses the BitBar API to find an available device from the group name given, or a device of that name device. Multiple device group names can be separated by a pipe.

Parameters:

  • device_or_group_names (String)

    Name of the device, or device group(s) for which to find an available

Returns:

  • Capabilities hash for the available device



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/maze/client/appium/bb_devices.rb', line 15

def get_available_device(device_or_group_names)
  api_client = BitBarApiClient.new(Maze.config.access_key)
  device_group_ids = api_client.get_device_group_ids(device_or_group_names)
  if device_group_ids
    # Device group found - find a free device in it
    $logger.trace "Got group ids #{device_group_ids} for #{device_or_group_names}"
    group_count, device = api_client.find_device_in_groups(device_group_ids)
    if device.nil?
      raise 'There are no devices available'
    else
      $logger.info "#{group_count} device(s) currently available in group(s) '#{device_or_group_names}'"
    end
  else
    # See if there is a device with the given name
    device = api_client.find_device device_or_group_names
  end

  device_name = device['displayName']
  platform = device['platform'].downcase
  platform_version = device['softwareVersion']['releaseVersion']

  $logger.info "Selected device: #{device_name} (#{platform} #{platform_version})"

  # TODO: Setting the config here is rather a side effect and factoring it out would be better.
  #   For now, though, it means not having to provide the --os and --os-version options on the command line.
  Maze.config.os = platform
  Maze.config.os_version = platform_version.to_f.floor

  case platform
  when 'android'
    make_android_hash(device_name)
  when 'ios'
    make_ios_hash(device_name)
  else
    throw "Invalid device platform specified #{platform}"
  end
end

.list_device_groups(access_key) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/maze/client/appium/bb_devices.rb', line 53

def list_device_groups(access_key)
  api_client = BitBarApiClient.new(access_key)
  device_groups = api_client.get_device_group_list
  unless device_groups['data'] && !device_groups['data'].empty?
    puts 'There are no device groups available for the given user access key'
    exit 1
  end
  puts "BitBar device groups available:"
  device_groups['data'].sort_by{|g| g['displayName']}.each do |group|
    puts '------------------------------'
    puts "Group name   : #{group['displayName']}"
    puts "OS           : #{group['osType']}"
    puts "Device count : #{group['deviceCount']}"
  end
end

.list_devices_for_group(device_group, access_key) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/maze/client/appium/bb_devices.rb', line 69

def list_devices_for_group(device_group, access_key)
  api_client = BitBarApiClient.new(access_key)
  group_id = api_client.get_device_group_id(device_group)
  unless group_id
    puts "No device groups were found with the given name #{device_group}"
    return
  end
  devices = api_client.get_device_list_for_group(group_id)
  if devices['data'].empty?
    puts "There are no devices available for the #{device_group} device group"
    return
  end
  puts "BitBar devices available for device group #{device_group}:"
  devices['data'].sort_by{|d| d['displayName']}.each do |device|
    puts '------------------------------'
    puts "Device name : #{device['displayName']}"
    puts "OS          : #{device['platform']} #{device['softwareVersion']['releaseVersion']}"

    if device['platform'].eql? 'ANDROID'
      puts "API level   : #{device['softwareVersion']['apiLevel']}"
    end
  end
end

.make_android_hash(device) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/maze/client/appium/bb_devices.rb', line 93

def make_android_hash(device)
  # Tripling up on capabilities in the `appium:options`, `appium` sub dictionaries and base dictionary.
  # See PLAT-11087
  appium_options = {
    'automationName' => 'UiAutomator2',
    'autoGrantPermissions' => true,
    'uiautomator2ServerInstallTimeout' => 60000,
    'uiautomator2ServerLaunchTimeout' => 60000
  }
  appium_options['appActivity'] = Maze.config.app_activity unless Maze.config.app_activity.nil?
  appium_options['appPackage'] = Maze.config.app_package unless Maze.config.app_package.nil?
  hash = {
    'platformName' => 'Android',
    'deviceName' => 'Android Phone',
    'appium:options' => appium_options,
    'appium' => appium_options,
    'bitbar:options' => {
      'device' => device,
    }
  }
  hash.merge!(appium_options)
  hash.freeze
end

.make_ios_hash(device) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/maze/client/appium/bb_devices.rb', line 117

def make_ios_hash(device)
  # Tripling up on capabilities in the `appium:options`, `appium` sub dictionaries and base dictionary.
  # See PLAT-11087
  appium_options = {
    'automationName' => 'XCUITest',
    'shouldTerminateApp' => 'true',
    'autoAcceptAlerts' => 'true'
  }
  hash = {
    'platformName' => 'iOS',
    'deviceName' => 'iPhone device',
    'appium:options' => appium_options,
    'appium' => appium_options,
    'bitbar:options' => {
      'device' => device
    }
  }
  hash.merge!(appium_options)
  hash.freeze
end