Android模拟用户点击的实现方法
前言
Android模拟用户点击。在自动化测试中可使用的工具。
可以利用adb命令,也可以使用AndroidSDK中的monkeyrunner工具。
- win7-64
- gitbash
使用adb命令
主要使用input命令
usage:input... inputtextinputkeyevent inputtap inputswipe
keyevent指的是android对应的keycode,比如home键的keycode=3,back键的keycode=4
tap是touch屏幕的事件,只需给出x、y坐标即可
swipe模拟滑动的事件,给出起点和终点的坐标即可
编写一个bat脚本,模拟用户滑动
@echooff echo---------Mockstart---------- :tag_start echorunning... adbshellinputswipe650250200666 @ping127.0.0.1-n8>nul gototag_start echo---------Mockfinish--------- pause
死循环发送滑动命令,延时语句@ping127.0.0.1-n8>nul
monkeyrunner
环境配置,配置好Java与AndroidSDK的环境变量。手机连接到电脑。
系统变量中加入ANDROID_SWT,此例中路径为G:\SDK\tools\lib\x86_64
修改后的脚本rustmonkeyrunner.bat,Windows环境下需要在gitbash或CMD里运行
来自unable-to-access-jarfile-framework-monkeyrunner-25-3-2-jar
@echooff remCopyright(C)2010TheAndroidOpenSourceProject rem remLicensedundertheApacheLicense,Version2.0(the"License"); remyoumaynotusethisfileexceptincompliancewiththeLicense. remYoumayobtainacopyoftheLicenseat rem remhttp://www.apache.org/licenses/LICENSE-2.0 rem remUnlessrequiredbyapplicablelaworagreedtoinwriting,software remdistributedundertheLicenseisdistributedonan"ASIS"BASIS, remWITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied. remSeetheLicenseforthespecificlanguagegoverningpermissionsand remlimitationsundertheLicense. remdon'tmodifythecaller'senvironment setlocal remSetupprogtobethepathofthisscript,includingfollowingsymlinks, remandsetupprogdirtobethefully-qualifiedpathnameofitsdirectory. setprog=%~f0 remChangecurrentdirectoryanddrivetowherethescriptis,toavoid remissueswithdirectoriescontainingwhitespaces. cd/d%~dp0 remCheckwehaveavalidJava.exeinthepath. setjava_exe= call..\lib\find_java.bat ifnotdefinedjava_exegoto:EOF for/f%%ain("%APP_HOME%\lib\monkeyrunner-25.3.2.jar")dosetjarfile=%%~nxa setframeworkdir=. setlibdir= ifexist%frameworkdir%\%jarfile%gotoJarFileOk setframeworkdir=..\lib ifexist%frameworkdir%\%jarfile%gotoJarFileOk setframeworkdir=..\framework :JarFileOk setjarpath=%frameworkdir%\%jarfile% ifnotdefinedANDROID_SWTgotoQueryArch setswt_path=%ANDROID_SWT% gotoSwtDone :QueryArch for/f"delims="%%ain('%frameworkdir%\..\bin\archquery')dosetswt_path=%frameworkdir%\%%a :SwtDone ifexist"%swt_path%"gotoSetPath echoSWTfolder'%swt_path%'doesnotexist. echoPleasesetANDROID_SWTtopointtothefoldercontainingswt.jarforyourplatform. exit/B :SetPath call"%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.bat Jython2.5.3(2.5:c56500f08d34+,Aug132012,14:54:35) [JavaHotSpot(TM)64-BitServerVM(OracleCorporation)]onjava1.8.0_77
首次运行时import模块迟迟没有反应
>>>fromcom.android.monkeyrunnerimportMonkeyRunner,MonkeyDevice,MonkeyImage
尝试运行脚本an_test2.py
importos print("importingmodule...") fromcom.android.monkeyrunnerimportMonkeyRunner,MonkeyDevice,MonkeyImage print("waitingforconnection...") device=MonkeyRunner.waitForConnection() print("devicefound!") 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=20 drag_point_right_x=s_wid-20 drag_point_y=s_height/2 foriinrange(0,10): print("currentloopis"+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.batH:\fisher_p\py_ws\an_test2.py importingmodule... waitingforconnection... devicefound! build.version.sdk23 display.width1440 display.height2560 currentloopis0 waiting... continue currentloopis1 #..... --------finish--------
测试中发现,脚本可以运行在系统app。若当前打开的是第三方app,会直接报错,获取不到相应信息
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对毛票票的支持。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。