Module: ADBDevices

Defined in:
lib/tabbyx/utils/adb_devices.rb

Class Method Summary collapse

Class Method Details

.adb_devicesObject



7
8
9
# File 'lib/tabbyx/utils/adb_devices.rb', line 7

def self.adb_devices
  Shell.adb_log("wait-for-device")
end

.awake_screenObject

唤醒屏幕



254
255
256
# File 'lib/tabbyx/utils/adb_devices.rb', line 254

def self.awake_screen
  send_key("26")
end

.bootload_deviceObject

重启设备



181
182
183
# File 'lib/tabbyx/utils/adb_devices.rb', line 181

def self.bootload_device
  Shell.adb("reboot bootloader")
end

.clear_app_data(package_name) ⇒ Object

清除应用的用户数据,清楚成功返回true, 否则返回false package_name 应用的包名



120
121
122
123
# File 'lib/tabbyx/utils/adb_devices.rb', line 120

def self.clear_app_data(package_name)
  outputs = Shell.adb_shell("pm clear " << package_name)
  (outputs == "Success") ? true : false
end

.get_android_versionObject

获取设备中Android的版本号



17
18
19
# File 'lib/tabbyx/utils/adb_devices.rb', line 17

def self.get_android_version
  Shell.adb_shell_log("getprop ro.build.version.release")
end

.get_app_startup_time(component) ⇒ Object

获取启动应用所花的时间,单位 ms component: 应用包名加主类名,packageName/Activity



193
194
195
196
197
198
199
200
# File 'lib/tabbyx/utils/adb_devices.rb', line 193

def self.get_app_startup_time(component)
  out = Shell.adb_shell("am start -W " << component << " | grep TotalTime").split(": ")[1]
  unless out.nil?
    out.rstrip << 'ms'
  else
    raise("ERROR: 没有获取到应用启动时间!")
  end
end

.get_battery_levelObject

返回设备电池电量



43
44
45
# File 'lib/tabbyx/utils/adb_devices.rb', line 43

def self.get_battery_level
  Shell.adb_shell("dumpsys battery | grep level").split(": ")[1]
end

.get_battery_statusObject

获取电池充电状态: 返回状态数值



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/tabbyx/utils/adb_devices.rb', line 54

def self.get_battery_status
  status = Shell.adb_shell("dumpsys battery | grep status").split(": ")[1].to_i
  case status
    when 1
      string = "1 : BATTERY_STATUS_UNKNOWN, 未知状态"
    when 2
      string = "2 : BATTERY_STATUS_CHARGING,充电状态"
    when 3
      string = "3 : BATTERY_STATUS_DISCHARGING, 放电状态"
    when 4
      string = "4 : BATTERY_STATUS_NOT_CHARGING, 未充电"
    when 5
      string = "5 : BATTERY_STATUS_FULL, 充电已满"
    else
      raise("错误的状态!")
  end
  string
end

.get_battery_temperatureObject

返回设备电池温度



48
49
50
51
# File 'lib/tabbyx/utils/adb_devices.rb', line 48

def self.get_battery_temperature
  temp = Shell.adb_shell("dumpsys battery | grep temperature").split(": ")[1]
  temp.to_i/10.0
end

.get_current_activityObject

获取设备上当前界面的activity



91
92
93
# File 'lib/tabbyx/utils/adb_devices.rb', line 91

def self.get_current_activity
  self.get_focused_package_activity[0].split("/")[1]
end

.get_current_package_nameObject

获取设备上当前界面的包名



86
87
88
# File 'lib/tabbyx/utils/adb_devices.rb', line 86

def self.get_current_package_name
  self.get_focused_package_activity[0].split("/")[0]
end

.get_device_idObject

获取设备的id号



12
13
14
# File 'lib/tabbyx/utils/adb_devices.rb', line 12

def self.get_device_id
  Shell.adb_shell_log("getprop ro.boot.serialno")
end

.get_focused_package_activityObject

获取设备上当前界面的package和activity



74
75
76
77
78
79
80
81
82
83
# File 'lib/tabbyx/utils/adb_devices.rb', line 74

def self.get_focused_package_activity
  pattern = "([a-zA-Z0-9.]+/.[a-zA-Z0-9.]+)"
  app = Shell.adb_shell("dumpsys input | grep FocusedApplication").scan(/#{pattern}/)[0]
  if app.nil?
    activity = Shell.adb_shell("dumpsys window w | grep \\/ | grep name=").scan(/#{pattern}/)[0]
    activity
  else
    app
  end
end

.get_installed_apps_listObject

获取设备中的第三方应用列表



148
149
150
151
152
153
# File 'lib/tabbyx/utils/adb_devices.rb', line 148

def self.get_installed_apps_list
  app = []
  outputs = Shell.adb_shell("pm list packages -3")
  outputs.split("\n").each {|out| app.push out.split(":")[1].strip}
  app
end

.get_pid(package_name) ⇒ Object

应用对应的包名, 返回pid值



96
97
98
99
100
# File 'lib/tabbyx/utils/adb_devices.rb', line 96

def self.get_pid(package_name)
  num = Shell.adb_shell("ps | grep -w " << package_name).scan(/([\" \"][0-9]+)/)
  raise("应用包名不存在或者进程未开启...") if num.nil?
  num[0]
end

.get_screen_resolutionObject

获取设备屏幕的分辨率 返回分辨率数组



28
29
30
31
32
33
34
35
# File 'lib/tabbyx/utils/adb_devices.rb', line 28

def self.get_screen_resolution
  resolution = []
  outputs = Shell.adb_shell("dumpsys display | grep PhysicalDisplayInfo").scan(/([0-9]+)/)
  puts outputs
  resolution.push(outputs[0])
  resolution.push(outputs[1])
  resolution
end

.get_sdk_versionObject

获取设备中SDK的版本号



22
23
24
# File 'lib/tabbyx/utils/adb_devices.rb', line 22

def self.get_sdk_version
  Shell.adb_shell_log("getprop ro.build.version.sdk")
end

.get_system_apps_listObject

获取设备中的系统应用列表



156
157
158
159
160
161
# File 'lib/tabbyx/utils/adb_devices.rb', line 156

def self.get_system_apps_list
  app = []
  outputs = Shell.adb_shell("pm list packages -s")
  outputs.split("\n").each {|out| app.push out.split(":")[1].strip}
  app
end

.install_app(app_path) ⇒ Object

安装应用 app_path, app的存放路径



132
133
134
# File 'lib/tabbyx/utils/adb_devices.rb', line 132

def self.install_app(app_path)
  Shell.adb("install -r " << app_path).to_s
end

.is_install?(package_name) ⇒ Boolean

判断应用是否已经安装,已安装,返回true,否则返回false package_name 包名

Returns:

  • (Boolean)


165
166
167
168
169
170
171
172
173
# File 'lib/tabbyx/utils/adb_devices.rb', line 165

def self.is_install?(package_name)
  get_installed_apps_list.each do |app|
    return true if app == package_name
  end
  get_system_apps_list.each do |app|
    return true if app == package_name
  end
  false
end

.kill_process(pid) ⇒ Object

进程被杀死,返回true,否则返回出错提示信息



103
104
105
106
# File 'lib/tabbyx/utils/adb_devices.rb', line 103

def self.kill_process(pid)
  out = Shell.adb_shell("kill " << pid)
  (out == "") ? true : out
end

.make_call(phone_number) ⇒ Object

拨打号码



244
245
246
# File 'lib/tabbyx/utils/adb_devices.rb', line 244

def self.make_call(phone_number)
  Shell.adb_shell("am start -a android.intent.action.CALL -d tel:" << phone_number)
end

.pull_file(remote_file_path, local_path) ⇒ Object

复制设备上的文件至本地 remote_file_path : 手机上文件所存储的路径 local_path: 本地文件夹路径



205
206
207
# File 'lib/tabbyx/utils/adb_devices.rb', line 205

def self.pull_file(remote_file_path,local_path)
  Shell.adb("pull " << remote_file_path << " " << local_path)
end

.push_file(local_file_path, remote_path) ⇒ Object

推送本地文件至设备 local_file_path: 本地文件路径 remote_path: 手机上文件夹路径



212
213
214
# File 'lib/tabbyx/utils/adb_devices.rb', line 212

def self.push_file(local_file_path,remote_path)
  Shell.adb("push " << local_file_path << " " << remote_path)
end

.reboot_deviceObject

重启设备



176
177
178
# File 'lib/tabbyx/utils/adb_devices.rb', line 176

def self.reboot_device
  Shell.adb("reboot")
end

.remove_all_apksObject

删除手机上tmp文件下临时存储的apk安装包



217
218
219
# File 'lib/tabbyx/utils/adb_devices.rb', line 217

def self.remove_all_apks
  Shell.adb_shell("rm /data/local/tmp/*.apk")
end

.reset_current_appObject

重置当前应用,清除当前应用的数据且重启该应用



126
127
128
# File 'lib/tabbyx/utils/adb_devices.rb', line 126

def self.reset_current_app
  clear_app_data(ADBDevices.get_current_package_name)
end

.run_android_test(package_name) ⇒ Object

运行 UIautomator测试脚本



230
231
232
233
234
# File 'lib/tabbyx/utils/adb_devices.rb', line 230

def self.run_android_test(package_name)
  shell = "am instrument -w -r   -e debug false -e package " << package_name << " " << package_name  << ".test/android.support.test.runner.AndroidJUnitRunner"
  outputs = Shell.adb_shell(shell)
  puts outputs
end

.run_android_test_by_class(class_name, package_name) ⇒ Object

运行 指定UIautomator测试脚本



222
223
224
225
226
227
# File 'lib/tabbyx/utils/adb_devices.rb', line 222

def self.run_android_test_by_class(class_name, package_name)
  shell = "am instrument -w -r   -e debug false -e class " << class_name << " " << package_name  << ".test/android.support.test.runner.AndroidJUnitRunner"
  puts shell
  outputs = Shell.adb_shell(shell)
  puts outputs
end

.send_key(key_code) ⇒ Object

按键



249
250
251
# File 'lib/tabbyx/utils/adb_devices.rb', line 249

def self.send_key(key_code)
  Shell.adb_shell("input keyevent " << key_code)
end

.send_keyboard(key) ⇒ Object

adb-shell发送按键命令



38
39
40
# File 'lib/tabbyx/utils/adb_devices.rb', line 38

def self.send_keyboard(key)
  Shell.adb_shell("input keyevent" << " " << key)
end

.start_activity(component) ⇒ Object

启动一个应用 component: 应用包名加主类名,packageName/Activity



187
188
189
# File 'lib/tabbyx/utils/adb_devices.rb', line 187

def self.start_activity(component)
  Shell.adb_shell("am start -n " << component)
end

.stop_app(package_name) ⇒ Object

退出当前应用



109
110
111
# File 'lib/tabbyx/utils/adb_devices.rb', line 109

def self.stop_app(package_name)
  Shell.adb_shell("am force-stop " << package_name)
end

.stop_current_appObject

退出指定包名应用



114
115
116
# File 'lib/tabbyx/utils/adb_devices.rb', line 114

def self.stop_current_app
  Shell.adb_shell("am force-stop " << self.get_current_package_name)
end

.uninstall_app(package_name) ⇒ Object

卸载指定应用 package_name, 应用包名,非apk名



138
139
140
141
142
143
144
145
# File 'lib/tabbyx/utils/adb_devices.rb', line 138

def self.uninstall_app(package_name)
  outputs = Shell.adb("uninstall " << package_name).to_s
  if outputs.include?("Success")
    return true
  else
    return outputs
  end
end

.visit_web(url) ⇒ Object

使用默认浏览器打开一个网页



239
240
241
# File 'lib/tabbyx/utils/adb_devices.rb', line 239

def self.visit_web(url)
  Shell.adb_shell("am start -a android.intent.action.VIEW -d " << url)
end