前言

Android模拟用户点击。在自动化测试中可使用的工具。

可以利用adb命令,也可以使用Android SDK中的monkeyrunner工具。

  • win7-64
  • gitbash

使用adb命令

主要使用input命令

usage: input ...  input text   input keyevent   input tap    input swipe    

keyevent指的是android对应的keycode,比如home键的keycode=3,back键的keycode=4

tap是touch屏幕的事件,只需给出x、y坐标即可

swipe模拟滑动的事件,给出起点和终点的坐标即可

编写一个bat脚本,模拟用户滑动

@echo offecho --------- Mock start ----------:tag_startecho running...adb shell input swipe 650 250 200 666@ping 127.0.0.1 -n 8 >nulgoto tag_startecho --------- Mock finish ---------pause

死循环发送滑动命令,延时语句@ping 127.0.0.1 -n 8 >nul

monkeyrunner

环境配置,配置好Java与Android SDK的环境变量。手机连接到电脑。
系统变量中加入ANDROID_SWT,此例中路径为G:\SDK\tools\lib\x86_64

修改后的脚本rustmonkeyrunner.bat,Windows环境下需要在gitbash或CMD里运行

来自unable-to-access-jarfile-framework-monkeyrunner-25-3-2-jar

@echo offrem Copyright (C) 2010 The Android Open Source Projectremrem Licensed under the Apache License, Version 2.0 (the "License");rem you may not use this file except in compliance with the License.rem You may obtain a copy of the License atremrem  http://www.apache.org/licenses/LICENSE-2.0remrem Unless required by applicable law or agreed to in writing, softwarerem distributed under the License is distributed on an "AS IS" BASIS,rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.rem See the License for the specific language governing permissions andrem limitations under the License.rem don't modify the caller's environmentsetlocalrem Set up prog to be the path of this script, including following symlinks,rem and set up progdir to be the fully-qualified pathname of its directory.set prog=%~f0rem Change current directory and drive to where the script is, to avoidrem issues with directories containing whitespaces.cd /d %~dp0rem Check we have a valid Java.exe in the path.set java_exe=call ..\lib\find_java.batif not defined java_exe goto :EOFfor /f %%a in ("%APP_HOME%\lib\monkeyrunner-25.3.2.jar") do set jarfile=%%~nxaset frameworkdir=.set libdir=if exist %frameworkdir%\%jarfile% goto JarFileOk set frameworkdir=..\libif exist %frameworkdir%\%jarfile% goto JarFileOk set frameworkdir=..\framework:JarFileOkset jarpath=%frameworkdir%\%jarfile%if not defined ANDROID_SWT goto QueryArch set swt_path=%ANDROID_SWT% goto SwtDone:QueryArch for /f "delims=" %%a in ('%frameworkdir%\..\bin\archquery') do set swt_path=%frameworkdir%\%%a:SwtDoneif exist "%swt_path%" goto SetPath echo SWT folder '%swt_path%' does not exist. echo Please set ANDROID_SWT to point to the folder containing swt.jar for your platform. exit /B:SetPathcall "%java_exe%" -Xmx512m "-Djava.ext.dirs=%frameworkdir%;%swt_path%" -Dcom.android.monkeyrunner.bindir=..\..\platform-tools -jar %jarpath% %*

运行脚本

Administrator@rust-PC ~$ /cygdrive/g/SDK/tools/bin/rustmonkeyrunner.batJython 2.5.3 (2.5:c56500f08d34+, Aug 13 2012, 14:54:35)[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0_77

首次运行时import模块迟迟没有反应

>>> from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage

尝试运行脚本an_test2.py

import osprint("importing module...")from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImageprint("waiting for connection...")device = MonkeyRunner.waitForConnection()print("device found!")s_wid = int(device.getProperty("display.width"))  # 获取屏幕宽度像素s_height = int(device.getProperty("display.height")) # 获取屏幕高度像素print("build.version.sdk " + str(device.getProperty("build.version.sdk")))print("display.width  " + str(s_wid))print("display.height " + str(s_height))drag_point_left_x = 20drag_point_right_x = s_wid - 20drag_point_y = s_height / 2for i in range(0, 10): print("current loop is " + str(i)) device.drag((drag_point_right_x, drag_point_y), (drag_point_left_x, drag_point_y), 1.0, 50) print("waiting...") MonkeyRunner.sleep(1) print("continue") device.drag((drag_point_left_x, drag_point_y), (drag_point_right_x, drag_point_y), 0.5, 3) MonkeyRunner.sleep(3)print("-------- finish --------")

命令行直接执行,可以看到执行结果和相应的报错信息

C:\Users\Administrator>G:\SDK\tools\bin\rustmonkeyrunner.bat H:\fisher_p\py_ws\an_test2.pyimporting module...waiting for connection...device found!build.version.sdk 23display.width  1440display.height 2560current loop is 0waiting...continuecurrent loop is 1# .....-------- finish --------

测试中发现,脚本可以运行在系统app。若当前打开的是第三方app,会直接报错,获取不到相应信息

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

更多相关文章

  1. 【Android(安卓)Native Code开发系列】 五 运行、调试编译的Nati
  2. ERROR: No installed build tools found. Install the Android(
  3. android中无限循环滑动的gallery实例
  4. Android(安卓)HorizontalScrollView 水平滑动 在listview上面动
  5. Android(安卓)不同API版本 向后兼容
  6. listview 总结
  7. webrtc 针对 android 平台的编译和运行
  8. Android使用ViewPager+Fragment实现定制TabHost选项卡效果
  9. android代码混淆

随机推荐

  1. Android之TabHost
  2. android vm heap 虚拟机堆大小 每个应用
  3. 利用Handler来更新android的UI
  4. [黎活明-传智播客]Android视频教程(保存慢
  5. Android获取手机系统版本等信息的方法
  6. Android开发工具配置大全
  7. Ubuntu申请APIkey
  8. Android UI(8)Building Apps with User I
  9. android 9.0 SD卡权限问题 文件管理器没
  10. Android DEX安全攻防战