Android模拟用户点击的实现方法

 更新时间:2018年02月25日 11:27:17   作者:Rust Fisher  
这篇文章主要给大家介绍了关于Android模拟用户点击的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学学习学习吧。

前言

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

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

  • win7-64
  • gitbash

使用adb命令

主要使用input命令

usage: input ...
  input text <string>
  input keyevent <key code number or name>
  input tap <x> <y>
  input swipe <x1> <y1> <x2> <y2>

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

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

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

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

@echo off
echo --------- Mock start ----------

:tag_start
echo running...
adb shell input swipe 650 250 200 666
@ping 127.0.0.1 -n 8 >nul
goto tag_start

echo --------- 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 off
rem Copyright (C) 2010 The Android Open Source Project
rem
rem 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 at
rem
rem  http://www.apache.org/licenses/LICENSE-2.0
rem
rem Unless required by applicable law or agreed to in writing, software
rem 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 and
rem limitations under the License.

rem don't modify the caller's environment
setlocal

rem 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=%~f0

rem Change current directory and drive to where the script is, to avoid
rem issues with directories containing whitespaces.
cd /d %~dp0

rem Check we have a valid Java.exe in the path.
set java_exe=
call ..\lib\find_java.bat
if not defined java_exe goto :EOF
for /f %%a in ("%APP_HOME%\lib\monkeyrunner-25.3.2.jar") do set jarfile=%%~nxa
set frameworkdir=.
set libdir=

if exist %frameworkdir%\%jarfile% goto JarFileOk
 set frameworkdir=..\lib

if exist %frameworkdir%\%jarfile% goto JarFileOk
 set frameworkdir=..\framework

:JarFileOk

set 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

:SwtDone

if 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

: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
Jython 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 os

print("importing module...")
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage

print("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 = 20
drag_point_right_x = s_wid - 20
drag_point_y = s_height / 2

for 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.py
importing module...
waiting for connection...
device found!
build.version.sdk 23
display.width  1440
display.height 2560
current loop is 0
waiting...
continue
current loop is 1
# .....
-------- finish --------

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

总结

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

相关文章

  • Android修改字体样式的示例代码

    Android修改字体样式的示例代码

    本篇文章主要介绍了Android修改字体样式的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-10-10
  • Android中LayoutAnimal的使用方法详解

    Android中LayoutAnimal的使用方法详解

    这篇文章给大家讲讲酷炫的动画成员LayoutAnimal,文章通过一个实例给大家详细介绍了Android中LayoutAnimal的使用方法,感兴趣的小伙伴可以自己动手试一试
    2023-09-09
  • Android自定义水平进度条的圆角进度

    Android自定义水平进度条的圆角进度

    这篇文章主要为大家详细介绍了Android自定义水平进度条的圆角进度,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-08-08
  • Android使用Volley实现上传文件功能

    Android使用Volley实现上传文件功能

    这篇文章主要介绍了Android使用Volley实现上传文件功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-12-12
  • 深入理解Android Bitmap

    深入理解Android Bitmap

    Bitmap是Android系统中的图像处理的最重要类之一。这篇文章主要介绍了理解Android Bitmap,需要的朋友可以参考下
    2017-11-11
  • Android编程之文件读写操作与技巧总结【经典收藏】

    Android编程之文件读写操作与技巧总结【经典收藏】

    这篇文章主要介绍了Android编程之文件读写操作与技巧,结合实例形式总结分析了Android常见的文件与目录的读写操作,及相关函数的使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2016-06-06
  • Android蓝牙服务启动流程分析探索

    Android蓝牙服务启动流程分析探索

    这篇文章主要介绍了Android蓝牙服务启动流程,了解内部原理是为了帮助我们做扩展,同时也是验证了一个人的学习能力,如果你想让自己的职业道路更上一层楼,这些底层的东西你是必须要会的
    2023-01-01
  • Android如何实现URL转换成二维码

    Android如何实现URL转换成二维码

    这篇文章主要为大家详细介绍了Android实现URL转换成二维码的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-04-04
  • Android实现文字滚动效果

    Android实现文字滚动效果

    这篇文章主要为大家详细介绍了Android实现文字滚动效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • Android启动画面的实现方法

    Android启动画面的实现方法

    这篇文章主要介绍了Android启动画面的实现方法,分析了布局文件及加载启动文件的实现方法,非常具有实用价值,需要的朋友可以参考下
    2015-01-01

最新评论