随着智能手机的市场份额超过PC成为个人微机的大头,更多的应用的开发会更倾向于安卓。安卓应用以点按和拖动为主,输入也通过触摸屏实现,这样的应用的功能测试也是通过点按和拖动为主的。然而pc端和浏览器有优秀的自动化的功能测试工具,而手机端却没有比较合适的类似工具。实际上安卓提供了一个AndroidSDK,里面提供了与安卓平台交互的工具(比如adbshell),这样就实现了电脑与手机的交互。接下来小编就教你如何在adbshell介入的方式下使用python语言来控制电脑操作手机吧。
下载后的文件:platform-tools_r30.0.4-windows.zip(大约12M)
接着解压文件到指定目录
解压后的路径与文件,接着为工具目录添加系统环境变量
验证安装结果
手机连接电脑USB后执行adbdevices查看手机连接状态
查询已连接设备/模拟器:adbdevices
此处连接手机,需要手机在开发者模式开启USB调试功能。顺便也开启模拟按键功能,后面会用到。
该命令经常出现以下问题:
offline——表示设备未连接成功或无响应;
device——设备已连接;
nodevice——没有设备/模拟器连接;
Listofdevicesattached设备/模拟器未连接到adb或无响应
方法1、进入手机的开发者模式,打开在手机上实时显示坐标的功能,长按屏幕位置自动显示坐标
方法2、使用androidadbshell命令获取
#截屏到手机adbshellscreencap/sdcard/screen.png#将手机上刚才的截图上传到电脑adbpull/sdcard/screen.png/Users/Administrator/Desktop/screen.png从原始图片上,使用PS扣出自己想要的局部图片,然后使用下面的代码获取局部图片在原始图片上的坐标不会使用PS,可以直接使用截图在原始图片上截图,但是没PS精细。
1.模拟点击
adbshellinputtap1001002.滑动
adbshellinputswipex1y1x2y2adbinputtouchscreenswipex1y1x2y2100adbshellinputswipe100100400100300#左往右adbshellinputswipe400100100100300#右往左adbshellinputswipe100100100400300#上往下adbshellinputswipe100400100100300#下往上adbshellinputswipe100100400400300#上往下斜adbshellinputswipe400400100100300#下往上斜3.长按
adbshellinputswipe1001001001001000//在100100位置长按1000毫秒adbshellinputswipe3674693674698004.打印所有包名
adbshellpmlistpackages~adbshellpmlistpackagespackage:com.huawei.floatMmspackage:com.android.defcontainerpackage:com.tencent.mm5.打印制定包的apk路径
adbshellpmpathcom.android.phone~adbshellpmpathcom.huawei.android.launcherpackage:/system/app/HwLauncher6.apk6.删除制定包
adbshellpmclearcom.test.abc7.截图
adbshellscreencap/sdcard/screen.pngadbpull/sdcard/screen.png#下载到本地8.获取被点击的位置信息
adbshellgetevent>/dev/input/event03393e1/dev/input/event0114a1/dev/input/event011451/dev/input/event0335406//x坐标/dev/input/event03541083//y坐标/dev/input/event0000/dev/input/event0339ffffffff/dev/input/event0114a0/dev/input/event011450/dev/input/event000getevent-l-c16输出所有event设备的基本信息adddevice1:/dev/input/event2name:"hi6421_on"couldnotgetdriverversionfor/dev/input/mouse0,Notatypewriteradddevice2:/dev/input/event4name:"huawei,touchscreen"adddevice3:/dev/input/event0name:"mhl_rcp_dev"couldnotgetdriverversionfor/dev/input/mice,Notatypewriteradddevice4:/dev/input/event1name:"hisi_gpio_key.14"adddevice5:/dev/input/event3name:"hi3630_hi6401_CARDHeadsetJack"getevent-c10//输出10条信息后退出getevent-l//将type、code、value以对应的常量名称显示9.打开对应的activity
adbshellamstart-n{包(package)名}/{包名}.{活动(activity)名称}adbshellamstartcom.songheng.eastnews/com.oa.eastfirst.activity.WelcomeActivity10.获得当前活动窗口的信息,包名以及活动窗体
adbshelldumpsyswindowwindows|grepmCurrent11.包名管理命令,获得对应包名的对应apk路径
adbshellpmpathcom.migu.lobby12.使用dumpsys命令可以查看Android手机当前正在运行的Activity
adbshelldumpsysactivityactivities|findstr"Run"13.使用uiautomatordump获取app上的页面元素
adbshelluiautomatordump/data/local/tmp/uidump.xmladbshelluiautomatordump/sdcard/dump.xml14.下载文件
adbpull/sdcard/demo.mp415.上传文件
adbpushtest.apk/sdcard16.息屏
adbshellinputkeyevent2617.keyevent
adbshellinputkeyevent20#向下adbshellinputkeyevent4#返回adbshellinputkeyevent3#Homeadbshellinputkeyevent6#挂机adbshellinputkeyevent84#搜索adbshellinputkeyevent26#电源adbshellinputkeyevent24#音量+adbshellinputkeyevent25#音量-18.输入框输入
adbshellinputtext"ANDROID"19.利用无线来查看adbshell
>adbtcpip5555连接:>adbconnectIP:555520.查看所有已经连接上的设备
adbdevices21.安装卸载
adbinstall