Module: Adb

Extended by:
Adb, Timeout
Included in:
Adb
Defined in:
lib/adb_driver/adb.rb

Defined Under Namespace

Classes: Device

Instance Method Summary collapse

Instance Method Details

#android_5_or_greater?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/adb_driver/adb.rb', line 21

def android_5_or_greater?
  android_version[0].to_i >= 5
end

#android_6?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/adb_driver/adb.rb', line 29

def android_6?
  android_version.start_with?('6')
end

#android_6_or_greater?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/adb_driver/adb.rb', line 25

def android_6_or_greater?
  android_version[0].to_i >= 6
end

#android_7?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/adb_driver/adb.rb', line 33

def android_7?
  android_version.start_with?('7')
end

#app_version(package_name) ⇒ Object



11
12
13
14
15
# File 'lib/adb_driver/adb.rb', line 11

def app_version(package_name)
  output = execute_command("shell dumpsys package #{package_name}")
  version_line = output.lines.grep(/versionName/).first.strip
  version_line[/=(.*)/, 1]
end

#asus?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/adb_driver/adb.rb', line 55

def asus?
  brand == 'asus'
end

#brandObject



67
68
69
# File 'lib/adb_driver/adb.rb', line 67

def brand
  execute_command('shell getprop ro.product.brand').strip
end

#connected_device_udidObject



90
91
92
93
94
95
96
97
98
# File 'lib/adb_driver/adb.rb', line 90

def connected_device_udid
  case
  when devices.none?                            then fail('No devices detected')
  when devices.one?                             then devices.first.udid
  when devices.count > 1 && emulators.none?     then fail('Several devices detected. Set ANDROID_SERIAL to pick one')
  when devices.count > 1 && emulators.one?      then emulators.first.udid
  when devices.count > 1 && emulators.count > 1 then fail('Several emulators detected. Close all but one')
  end
end

#densityObject



63
64
65
# File 'lib/adb_driver/adb.rb', line 63

def density
  @density ||= execute_command('shell getprop ro.sf.lcd_density').to_i
end

#emulator?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/adb_driver/adb.rb', line 43

def emulator?
  !real_device?
end

#execute_command(command, timeout_in_seconds = 10) ⇒ Object



7
8
9
# File 'lib/adb_driver/adb.rb', line 7

def execute_command(command, timeout_in_seconds = 10)
  timeout(timeout_in_seconds) { `adb #{command}` }
end

#htc?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/adb_driver/adb.rb', line 51

def htc?
  brand == 'htc'
end

#lenovo?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/adb_driver/adb.rb', line 59

def lenovo?
  brand == 'Lenovo'
end

#modelObject



71
72
73
# File 'lib/adb_driver/adb.rb', line 71

def model
  execute_command('shell getprop ro.product.model').strip
end

#package_exists?(package_name) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/adb_driver/adb.rb', line 17

def package_exists?(package_name)
  execute_command('shell pm list packages').lines.grep(/#{package_name}\s*$/).one?
end

#portrait?Boolean

Returns:

  • (Boolean)


85
86
87
88
# File 'lib/adb_driver/adb.rb', line 85

def portrait?
  @orientation_enum ||= `adb shell dumpsys input`.lines.find { |l| l =~ /SurfaceOrientation/ }.strip[-1].to_i
  @orientation_enum.even?
end

#real_device?Boolean

Returns:

  • (Boolean)


37
38
39
40
41
# File 'lib/adb_driver/adb.rb', line 37

def real_device?
  output = execute_command('devices -l')
  output = output.lines.grep(/#{ENV['ANDROID_SERIAL']}/).first
  output.include?('usb')
end

#remove_package(name) ⇒ Object



75
76
77
78
79
# File 'lib/adb_driver/adb.rb', line 75

def remove_package(name)
  return unless package_exists?(name)
  `adb uninstall #{name}`
  fail "#{name} package wasn't removed" if package_exists?(name)
end

#restart_adbObject



81
82
83
# File 'lib/adb_driver/adb.rb', line 81

def restart_adb
  `adb kill-server; adb start-server`
end

#samsung?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/adb_driver/adb.rb', line 47

def samsung?
  brand == 'samsung'
end