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

唤醒屏幕



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

def self.awake_screen
  send_key("26")
end

.bootload_deviceObject

重启设备



171
172
173
# File 'lib/tabbyx/utils/adb_devices.rb', line 171

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

.clear_app_data(package_name) ⇒ Object

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



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

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



183
184
185
186
187
188
189
190
# File 'lib/tabbyx/utils/adb_devices.rb', line 183

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

返回设备电池电量



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

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

.get_battery_statusObject

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/tabbyx/utils/adb_devices.rb', line 49

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

返回设备电池温度



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

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



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

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

.get_current_package_nameObject

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



81
82
83
# File 'lib/tabbyx/utils/adb_devices.rb', line 81

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



69
70
71
72
73
74
75
76
77
78
# File 'lib/tabbyx/utils/adb_devices.rb', line 69

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

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



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

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值



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

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

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



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

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的存放路径



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

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)


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

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,否则返回出错提示信息



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

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

.make_call(phone_number) ⇒ Object

拨打号码



234
235
236
# File 'lib/tabbyx/utils/adb_devices.rb', line 234

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: 本地文件夹路径



195
196
197
# File 'lib/tabbyx/utils/adb_devices.rb', line 195

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: 手机上文件夹路径



202
203
204
# File 'lib/tabbyx/utils/adb_devices.rb', line 202

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

.reboot_deviceObject

重启设备



166
167
168
# File 'lib/tabbyx/utils/adb_devices.rb', line 166

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

.remove_all_apksObject

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



207
208
209
# File 'lib/tabbyx/utils/adb_devices.rb', line 207

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

.reset_current_appObject

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



116
117
118
# File 'lib/tabbyx/utils/adb_devices.rb', line 116

def self.reset_current_app
  clear_app_data(ADBDevices.get_current_package_name)
end

.run_android_test(package_name) ⇒ Object

运行 UIautomator测试脚本



220
221
222
223
224
# File 'lib/tabbyx/utils/adb_devices.rb', line 220

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测试脚本



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

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

按键



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

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

.start_activity(component) ⇒ Object

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



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

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

.stop_current_appObject

退出当前应用



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

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名



128
129
130
131
132
133
134
135
# File 'lib/tabbyx/utils/adb_devices.rb', line 128

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

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



229
230
231
# File 'lib/tabbyx/utils/adb_devices.rb', line 229

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