Android模拟用户点击的实现方法
作者:Rust Fisher 发布时间:2022-05-09 09:02:31
标签: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,会直接报错,获取不到相应信息
来源:http://rustfisher.github.io/2018/02/05/Android_note/Android-mock_user_touch/


猜你喜欢
- 前言上一篇:微服务网关Zuul上文中,我们介绍了微服务网关Zuul,Zuul 是 Netflix 公司开源的产品,被称为第一代网关,也是 S
- 简述在仿写豆瓣的时候,发现了根据评分不同,星级数也不同的星级条。百度一搜,发现Android有自带控件UIRatingBar,而iOS得要自
- 最近在机顶盒上做一个gridview,其焦点需要在item的子控件上,但gridview的焦点默认在item上,通过android:desc
- 结构化查询语言(SQL)是一种标准化的语言,它允许你在数据库上执行操作,如创建项目,读取内容,内容更新和删除条目。SQL是所有可能会使用几乎
- 前言:sleep 方法和 wait 方法都是用来将线程进入休眠状态的,并且 sleep 和 wait 方法都可以响应 interrupt 中
- 今天突发奇想,想做一个智能拼图游戏来给哄女友。需要实现这些功能第一图片自定义第二宫格自定义,当然我一开始就想的是3*3 4*4 5*5,没有
- 开始学习WebSocket,准备用它来实现一个在页面实时输出log4j的日志以及控制台的日志。首先知道一些基础信息:1.java7 开始支持
- 前言: SQLite简介:是一款轻型的数据库,是遵守AC
- NDK部分1、下载ndk这里就一笔带过了。2、解压ndk不要解压,文件权限会出错。执行之,会自动解压,然后mv到想放的地方。我放到了”/us
- Excel表格中的迷你图表能够直观地向我们展示出数据的变化趋势。本文将介绍C#如何实现为表格数据生成迷你图表,以及修改和删除迷你图表的方法。
- 众所周知在android7.0,修改了对私有存储的限制,导致在获取资源的时候,不能通过Uri.fromFile来获取uri了我们需要适配7.
- XML假如有这样一个XML格式的数据:<?xml version="1.0" encoding="utf
- 1、将 Jmeter 下 extras 目录中 ant-jmeter-1.1.1.jar 包拷贝至 ant 安装目录下的lib目录中,否则会
- 文件移动是计算机资源管理常用的一个操作,这在操作系统中可以通过文件的剪切与复制或鼠标拖动来实现。但是在Java文件的编程实现中,大多是通过复
- synchronized 和 Reentrantlock多线程编程中,当代码需要同步时我们会用到锁。Java为我们提供了内置锁(synchr
- 前言DataGridView是开发Winform的一个列表展示,类似于表格。学会下面的基本特征用法,再辅以经验,基本功能开发没问题。基本的数
- 之前一直在Android应用层上做工作,最近开始研究Android平台上的东东了,主要是在Android Frameworks层和系统库层进
- APT(Annotation Processing Tool 的简称),可以在代码编译期解析注解,并且生成新的 Java 文件,减少手动的代
- 本文实例讲述了C#实现用于操作wav声音文件的类。分享给大家供大家参考。具体如下:有了这个C#类,我们可以很轻易的调用本地wav文件进行同步
- 本文实例为大家分享了java实现小超市程序的具体代码,供大家参考,具体内容如下一.人物包1.顾客类package person;public