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

.caps_prefix(appium_version) ⇒ Object



126
127
128
# File 'lib/maze/client/appium/bb_devices.rb', line 126

def caps_prefix(appium_version)
  appium_version.nil? || (appium_version.to_i < 2) ? '' : 'appium:'
end

.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
52
53
54
55
56
57
58
59
# 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.debug "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?
      # TODO: Retry rather than fail, see PLAT-7377
      Maze::Helper.error_exit '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

  prefix = caps_prefix(Maze.config.appium_version)

  case platform
  when 'android'
    automation_name = if platform_version.start_with?('5')
                        'UiAutomator1'
                      else
                        'UiAutomator2'
                      end
    make_android_hash(device_name, automation_name)
  when 'ios'
    make_ios_hash(device_name, prefix)
  else
    throw "Invalid device platform specified #{platform}"
  end
end

.list_device_groups(access_key) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/maze/client/appium/bb_devices.rb', line 61

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/maze/client/appium/bb_devices.rb', line 77

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, automation_name) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/maze/client/appium/bb_devices.rb', line 101

def make_android_hash(device, automation_name)
  hash = {
    'automationName' => automation_name,
    'platformName' => 'Android',
    'deviceName' => 'Android Phone',
    'bitbar:options' => {
      'autoGrantPermissions' => true,
      'device' => device,
    }
  }
  hash.freeze
end

.make_ios_hash(device, prefix = '') ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/maze/client/appium/bb_devices.rb', line 114

def make_ios_hash(device, prefix='')
  {
    'automationName' => 'XCUITest',
    'deviceName' => 'iPhone device',
    'platformName' => 'iOS',
    "#{prefix}shouldTerminateApp" => 'true',
    'bitbar:options' => {
      'device' => device
    }
  }.freeze
end