也许你有这样的困扰,想要使一个app自动运行在手机上,这个时候你第一个要做的就是开启root,然后执行下面的代码。monkey会为你搞定一切。
1.新建一个monkey.txt文件在电脑上。比如说放在E://monkey.txt
type = raw events
count = 1
speed = 1.0
start data >>
RunCmd(am instrument -w -r -e debug false -e class zhepan.com.mytestcase.ExampleInstrumentedTest#startUp zhepan.com.mytestcase.test/android.support.test.runner.AndroidJUnitRunner)
# 我在夜神模拟器中打开目录,并且在/storage/emulated/0/data/下新建了文件夹local/tmp,于是先把monkey.txt脚本传到模拟器中
>adb push E:/monkey.txt /storage/emulated/0/data/local/tmp
# 准备在手机中执行这个monkey.txt脚本
monkey命令:adb shell monkey -f /data/local/tmp/demo.txt -v 2 --ignore carshed
(ps:Monkey命令中,-v后面跟的参数表示外循环次数)
>adb shell monkey -f /storage/emulated/0/data/local/tmp/monkey.txt -v 1 --ignore carshed
最终的结果是运行成功了。
需要说明一点,现在在uiautomator2里,执行adb shell命令是通过Runtime.getRuntime().exec("su");来运行的。也就意味着手机必须得打开root权限。否则无响应。
adb shell monkey -f /data/local/tmp/demo.txt -v 1 --ignore carshed
更多阅读资讯: