class KrakenMobile::DevicesHelper::AdbHelper

Public Instance Methods

adb_devices_l() click to toggle source

ADB command that returns all phones and emulators connected to the computer.

# File lib/kraken-mobile/helpers/devices_helper/adb_helper.rb, line 10
def adb_devices_l
  `adb devices -l`
end
connected_devices() click to toggle source

Returns an array with all the devices and emulators connected to the computer.

# File lib/kraken-mobile/helpers/devices_helper/adb_helper.rb, line 50
def connected_devices
  devices = []
  adb_devices_l.split("\n").each do |line|
    line_id = extract_device_id(line)
    line_model = extract_device_model(line)
    if line_id && line_model
      device = Models::Device.new(line_id, line_model, devices.size + 1)
      devices << device
    end
  end
  devices
rescue StandardError => _e
  []
end
create_file(file_name, device_id) click to toggle source
# File lib/kraken-mobile/helpers/devices_helper/adb_helper.rb, line 87
def create_file(file_name, device_id)
  unless device_connected?(device_id)
    raise "Device #{device_id} not found"
  end

  create_file_in_device("#{file_name}.txt", device_id)
  true
rescue StandardError => _e
  false
end
create_file_in_device(file_name, device_id) click to toggle source
# File lib/kraken-mobile/helpers/devices_helper/adb_helper.rb, line 22
def create_file_in_device(file_name, device_id)
  `adb -s #{device_id} shell "> /sdcard/#{file_name}"`
end
delete_file(file_name, device_id) click to toggle source
# File lib/kraken-mobile/helpers/devices_helper/adb_helper.rb, line 98
def delete_file(file_name, device_id)
  unless device_connected?(device_id)
    raise "Device #{device_id} not found"
  end

  delete_file_in_device("#{file_name}.txt", device_id)
  true
rescue StandardError => _e
  false
end
delete_file_in_device(file_name, device_id) click to toggle source
# File lib/kraken-mobile/helpers/devices_helper/adb_helper.rb, line 26
def delete_file_in_device(file_name, device_id)
  `adb -s #{device_id} shell "rm -rf /sdcard/#{file_name}"`
end
device_connected?(device_id) click to toggle source
# File lib/kraken-mobile/helpers/devices_helper/adb_helper.rb, line 42
def device_connected?(device_id)
  adb_devices_l.include?(device_id)
rescue StandardError => _e
  false
end
device_orientation(device_id) click to toggle source
# File lib/kraken-mobile/helpers/devices_helper/adb_helper.rb, line 38
def device_orientation(device_id)
  `adb -s #{device_id} shell dumpsys input | grep 'SurfaceOrientation' | awk '{ print $2 }'`
end
device_screen_size(device_id) click to toggle source
# File lib/kraken-mobile/helpers/devices_helper/adb_helper.rb, line 30
def device_screen_size(device_id)
  `adb -s #{device_id} shell wm size`
end
device_sdk_version(device_id) click to toggle source
# File lib/kraken-mobile/helpers/devices_helper/adb_helper.rb, line 34
def device_sdk_version(device_id)
  `adb -s #{device_id} shell getprop ro.build.version.sdk`
end
extract_device_id(line) click to toggle source

Parses the device id from the ADB devices command.

# File lib/kraken-mobile/helpers/devices_helper/adb_helper.rb, line 140
def extract_device_id(line)
  return line.split(' ').first if line.match(/device(?!s)/)
end
extract_device_model(line) click to toggle source

Parses the device model from the ADB devices command.

# File lib/kraken-mobile/helpers/devices_helper/adb_helper.rb, line 145
def extract_device_model(line)
  return unless line.match(/device(?!s)/)

  line.scan(/model:(.*) device/).flatten.first
end
file_content(file_name, device_id) click to toggle source
# File lib/kraken-mobile/helpers/devices_helper/adb_helper.rb, line 14
def file_content(file_name, device_id)
  `adb -s #{device_id} shell "cat /sdcard/#{file_name} 2> /dev/null"`
end
orientation(device_id) click to toggle source
# File lib/kraken-mobile/helpers/devices_helper/adb_helper.rb, line 132
def orientation(device_id)
  adb_orientation = device_orientation(device_id).strip!
  adb_orientation.to_i
rescue StandardError => _e
  KrakenMobile::Constants::PORTRAIT
end
read_file_content(file_name, device_id) click to toggle source
# File lib/kraken-mobile/helpers/devices_helper/adb_helper.rb, line 65
def read_file_content(file_name, device_id)
  unless device_connected?(device_id)
    raise "Device #{device_id} not found"
  end

  content = file_content("#{file_name}.txt", device_id)
  content.strip
rescue StandardError => _e
  ''
end
screen_size(device_id) click to toggle source

Returns height, width

# File lib/kraken-mobile/helpers/devices_helper/adb_helper.rb, line 110
def screen_size(device_id)
  adb_size = device_screen_size device_id
  parts = adb_size.strip!.split(' ')
  size = parts[parts.count - 1]
  return [0, 0] unless size.include?('x')

  size_parts = size.split('x')
  if orientation(device_id) == KrakenMobile::Constants::PORTRAIT
    return size_parts[1].to_i, size_parts[0].to_i
  end

  [size_parts[0].to_i, size_parts[1].to_i]
rescue StandardError => _e
  [0, 0]
end
sdk_version(device_id) click to toggle source
# File lib/kraken-mobile/helpers/devices_helper/adb_helper.rb, line 126
def sdk_version(device_id)
  device_sdk_version device_id
rescue StandardError => _e
  'N/A'
end
write_content_to_device(content, file_name, device_id) click to toggle source
# File lib/kraken-mobile/helpers/devices_helper/adb_helper.rb, line 18
def write_content_to_device(content, file_name, device_id)
  `adb -s #{device_id} shell "echo "#{content}" > /sdcard/#{file_name}"`
end
write_content_to_file(content, file_name, device_id) click to toggle source
# File lib/kraken-mobile/helpers/devices_helper/adb_helper.rb, line 76
def write_content_to_file(content, file_name, device_id)
  unless device_connected?(device_id)
    raise "Device #{device_id} not found"
  end

  write_content_to_device(content, "#{file_name}.txt", device_id)
  true
rescue StandardError => _e
  false
end