1. 截图
adb shell /system/bin/screencap -p /sdcard/screenshot.png
adb pull /sdcard/screenshot.png ./screenshot.png
2. 单击(根据坐标位置单击)
adb shell input tap x y
3.输入文字
adb shell input text hello
4. 获取正在运行的app包名
adb shell dumpsys window | grep mCurrentFocus
5.根据app包名获取该app对应的apk
adb shell pm path com.tencent.mobileqq
6.根据apk获取activity
aapt dump badging base.apk | grep activity
7. 获取adb shell 的输出内容(python版本)
def get_adbshell_content(cmd:str) ->str:
'''
cmd 为 adb shell 命令
'''
p = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE)
out,err = p.communicate()
return out.decode()
8.文件(手机)复制到本地(pc)
adb pull 手机文件 本地目录
9 根据包名和activity启动app
adb shell am start -n app包名/app的activity
9. 根据包名退出app
adb shell am forece-stop app的包名
最后可以封装一个工具类(python版本)
class AndroidADB(object):
def screenshot(self,img_name:str) ->None:
'''
截图
:param img_name:
:return:
'''
os.system("adb shell /system/bin/screencap -p /sdcard/screenshot.png")
os.system("adb pull /sdcard/screenshot.png {}/{}.png".format(Constant.IMG_PATH,img_name))
def click(self,x:int,y:int) ->None:
'''
单击
:param x:
:param y:
:return:None
'''
os.system("adb shell input tap {} {}".format(x,y))
def input_text(self,x:int,y:int,text:str) ->None:
'''
固定位置输入 文字
:param x:
:param y:
:return:
'''
self.click(x,y)
for i in text:
os.system("adb shell input text {}".format(i))
def __input_clear(self):
'''
文本框 清理
:return:
'''
pass
def get_runing_app_package(self) ->str:
'''
获取正在运行的 app 包名
:return:
'''
cmd = "adb shell dumpsys window | grep mCurrentFocus"
p = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE)
out,err = p.communicate()
out_str = out.decode()
print(out_str)
start = out_str.index("com")
end = out_str.index("/")
return out_str[start:end]
def get_apkpath_apkname(self,app_package:str) ->tuple:
'''
获取 apk 在手机中的路径 和 该包名对应的 apk名称
:param app_package: app 包名
:return: apk 路径 和 apk名称
'''
cmd = "adb shell pm path {}".format(app_package)
cmd_out_str = self.__get_cmd_stdout_content(cmd).strip("\n")
app_abspath = cmd_out_str.split(":")[-1]
apkpath = os.path.dirname(app_abspath)
apkname = os.path.basename(app_abspath)
return apkpath,apkname
def get_app_activity(self,apk_abspath:str) ->str:
'''
获取app 启动主窗口
:param app_package: app 包名
:return:
'''
cmd = "aapt dump badging {} | grep activity".format(apk_abspath)
cmd_out_str = self.__get_cmd_stdout_content(cmd).split("\n")[0].split(" ")[1].split("'")[1]
return cmd_out_str
def __get_cmd_stdout_content(self,cmd:str) ->str:
'''
获取 adb 命令 输出到终端的内容
:param cmd:
:return:
'''
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
out, err = p.communicate()
return out.decode()
def file2local(self,apkabspath:str,local_path:str) ->None:
'''
将文件从 安卓设备 复制 到 本地
:param apkabspath:
:param local_path:
:return:
'''
cmd = "adb pull {} {}".format(apkabspath,local_path)
os.system(cmd)
def start_app(self,app_package:str,app_activity:str) ->None:
'''
启动 app
:param app_package: app的包名
:param app_activity: app的启动主窗口
:return:
'''
os.system("adb shell am start -n {}/{}".format(app_package,app_activity))
def quit_app(self,app_package:str) ->None:
'''
退出app
:param app_package: app应用包名
:return: None
'''
cmd = "adb shell am force-stop {}".format(app_package)
os.system(cmd)