adb测试脚本

获取App包名(然后点击目标图标)
adb logcat | grep START

给指定的包打压力
adb shell monkey -p package 1000

获取链接设备
adb devices

安装apk
adb install apk全路径名

monkey压力测试
adb shell monkey 1000

Monkey高级参数的应用
throttle 参数

指定事件之间的间隔(注意throttle前面两个中横线)
adb shell monkey —throttle
adb shell monkey -p package —-throttle 1000 10
adb shell monkey -p com.xlt.wisdomrailway —throttle 1000 10

seed参数重现(seed值为100,50个事件)com.xlt.wisdomrailway
adb shell monkey -p package -s 100 50
adb shell monkey -p com.xlt.wisdomrailway -s 100 50

设置触摸事件百分比(注意pct前面两个中横线)百分之100的事件,80个事件,-v是列出所有的touch事件
adb shell monkey —p package —pct-touch
adb shell monkey -v -p package —pct-touch 100 80
adb shell monkey -v -p com.xlt.wisdomrailway –pct-touch 100 100

设置动作事件百分比(注意pct前面两个中横线)
adb shell monkey -v -p package —pct-touch 50 —pct-motion 10 —pct-appswitch 40 100
adb shell monkey —pct-motion

忽略崩溃和异常(注意pct前面两个中横线)
adb shell monkey -p package —ignore-crashes

忽略超时事件
adb shell monkey -p package —ignore-timeouts

          CARSH 结果分析

step1:安装引起crash的app
step2:执行压力命令
step3:分析crash的exception信息

查看手机中anr相关信息
adb shell
cd /data/anr
ll traces.txt

——————————————————————
Monkey Script
执行Monkey脚本的命令:
adb shell monkey -f

DispatchTrackball命令
轨迹球事件
DispatchTrackball(long downtime,long eventide,int action,float x,float y,float pressure,float size,int metastate,float xprecision,float yprecision,int device,int edgeflags)
(action 0代表按下,1代表弹起,x和y代表坐标)

点击事件
DispatchPointer(long downtime,long eventide,int action,float x,float y,float pressure,float size,int metastate,float xprecision,float yprecision,int device,int edgeflags)
(action 0代表按下,1代表弹起,x和y代表坐标)

输入字符串的命令
DispatchString
DispatchString(String text)

LaunchActivity命令
启动应用
LaunchActivity(String packageName,String activityName)

UserWait命令
等待事件
UserWait(long times)

DispatchPress命令
按下键值
DispatchPress(int keycode)//keycode 66 回车

uiautomatorviewer获取当前activity界面的控件信息

操作步骤

启动App
点击输入框
输入查询词
点击键盘上的回车
点击搜索按钮
等待结果的输出
点击Clear

脚本开始

typ=user
count=10
speed=1.0
start data >>
LaunchActivity(com.xlt.wisdomrailway,com.xlt.wisdomrailway.activity.AboutXltActivity)
UserWait(2000)
DispatchPointer(10,10,0,100,100,1,1,-1,1,1,0,0)
DispatchPointer(10,10,1,100,100,1,1,-1,1,1,0,0)
DispatchString(test)
UserWait(1000)
DispatchPress(66)
UserWait(1000)
DispatchPointer(10,10,0,400,100,1,1,-1,1,1,0,0)
DispatchPointer(10,10,1,400,100,1,1,-1,1,1,0,0)
UserWait(6000)

脚本结束

adb push mock.script /data/local/tmp/
monkey -f look.script 2

脚本执行
adb shell monkey -f /data/local/tmp/mook.script 2

—————————————————————————————————————————————————————
MonkeyRunner 实现截屏
MonkeyRunner API -alert

1、警告框
void alert(String message,String title,String okTitle)

脚本
from com.android.monkeyrunner import MonkeyRunner
MonkeyRunner.alert(‘Hello look friends’,’This is title ‘,’OK’)

执行
monkeyrunner demo.py

monkeyrunner路径:sdk/tools/monkeyrunner

2、waitForConnection
等待设备连接,有多个device id,需要指明具体哪个设备。
waitForConnection(float timeout,String deviceid)

3、-drag

拖动
drag(tuple start,tuple end,float duration,integer steps)
(start 起点位置,end终点位置,duration手势持续的时间,steps插值点的步数,默认10)

4、press
按键
press(String keyCode,dictionary type)
(keyCode名,Down,up,down_and_up)

5、startActivity
启动应用

startActiivty(activity全路径名)

6、touch点击

touch(integer x,integer y,integer type)
x坐标值,y坐标值,type=Down,up,down_and_up

7、type
type(String msg)

8、takeSnapshot
截屏
MonkeyImage takeSnapShot()

9、sameAs
图像对比
boolean sameAs(MonkeyImage other,float precent)

10、writetoFile
保存图像文件
void writeToFile(String path,String format)

-------------------------------------------
MonkeyRunner完整的脚本mook.py
引入包
from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage
连接设备
device=MonkeyRunner.waitForConnection(3,”192.168.56.101:5555”)
启动app
device.startActivity(“com.xlt.waildomway.MainActivity”)
MonkeyRunner.sleep(2)
点击搜索框
device.touch(100,100,”DOWN_AND_UP”)
MonkeyRunner.sleep(1)
输入查询关键词
device.type(“test”)
MonkeyRunner.sleep(1)
点击回车键
device.press(“KEYCODE_ENTER”,”DOWN_AND_UP”)
MonkeyRunner.sleep(1)
点击搜索按钮
device.touch(400,100,”DOWN_AND_UP”)
MonkeyRunner.sleep(6)
截图
image=device.takeSnapshot()
image.writeToFile(“./Test.png”,”png”)
点击清除按钮
device.touch(300,100,”DOWN_AND_UP”)
MonkeyRunner.sleep(3)

----------脚本end

执行脚本mook.py
monkeyrunner mook.py

你可能感兴趣的:(android,adb,shell脚本学习,命令,安卓测试)