前言

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 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里运行

@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,会直接报错,获取不到相应信息

总结

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

安卓java 模拟点击类_Android模拟用户点击的实现方法相关推荐

  1. 【Java】错误: 在类 Test1.Test2 中找不到 main 方法, 请将 main 方法定义为:    public static void main(String[] args)的解决方案

    问题:运行Java程序的时候碰到如下问题 错误: 在类 Test1.Test2 中找不到 main 方法, 请将 main 方法定义为:    public static void main(Stri ...

  2. java struts2 session mysql 内存不足_Java - 用户在线的数据库实现方法和内存实现方法...

    一,数据库实现方法:(实现思路:基于Struts2.0的拦截器) 0,流程:struts.xml -> ApplicationContext.xml -> LoginInterceptor ...

  3. java http请求 工具类_java模拟http请求调用远程接口工具类

    package ln; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamRea ...

  4. java 安卓 html文件怎么打开方式_android 浏览器 打开本地html文件的方法

    android 浏览器 打开本地html文件的方法有些html文件放在本地磁盘和sdcard,如何用打开这个网页呢? 这种应用在测试时非常有用. 有2个方法: 1. 使用文件管理器 如ES等,需要幸运 ...

  5. android多击事件_Android中的多击事件

    在android系统中.google提供了一个click事件.只负责相应单次点击. 如果想实现双机或者多击事件的话就需要借鉴下google原生应用的源码了. 在android中,三次点击android ...

  6. Java实验——定义一个类,该类中包含以下几个方法(静态):实现两个字符串数组的逆序排序,输出结果为字符串数组;求两个整形数组的交集;求两个浮点型数组的并集;

    目录 一.实现两个字符串数组的逆序排序,输出结果为字符串数组 解题思路: 具体代码: 运行截图: 二.求两个整形数组的交集 具体代码: 运行截图: 三.求两个浮点型数组的并集 解题思路: 具体代码: ...

  7. java 定义一个学生类,利用无参和带参方法调用

    包名展示 主函数 package Sudent;import Sudent.student;public class studentdemo {public static void main(Stri ...

  8. Java黑皮书课后题第10章:**10.7(游戏:ATM机)使用编程练习题9.7中创建的Account类来模拟一台ATM级

    **10.7(游戏:ATM机)使用编程练习题9.7中创建的Account类来模拟一台ATM级 题目 程序 代码 Test7.java Test7_Account.java 运行示例 题目 程序 Tes ...

  9. java测试模拟网页点击,httpunit爬虫模拟搜索详解与实战

    一.WEB测试工具介绍 httpunit是一个可以模拟浏览器的集成工具,它可以让你在不需要浏览器的情况下模拟浏览器的浏览行为,该工具是junit测试工具下面的一个子框架,主要用来做web端测试使用,它 ...

最新文章

  1. matlab kill
  2. 岗位推荐 | 腾讯AI Lab多媒体算法组招聘正式员工和实习生
  3. cmake编译mysql常用参数
  4. [html] html中如何使用svg?
  5. redis实践的一点思路,关于支付回调
  6. 安卓学习笔记:使用PopupWindow创建简单菜单
  7. 各浏览器对focusin/focusout事件的支持差异
  8. iviewtable表格数据 录音播放下载
  9. 常用的限流框架,你都会用吗?
  10. 使用Delphi创建,解析,操纵XML文档
  11. 服务器和交换机物理连接_连接网络的枢纽,网络交换机详解
  12. 线性回归分析步骤总结
  13. matlab曼德勃罗集,YaK与您一起欣赏BBC纪录片:''''神秘的混沌理论''''
  14. 第十一章 性能与可伸缩性(待续)
  15. Allegro学习笔记:“如何建自己的Subclass”
  16. matlab图片修复原理,图像修复 Region filling and object removal by exemplar-based image inpainting matlab实现...
  17. 指针的形式与动态存储分配
  18. 第三方接口转换工具类
  19. 华为云618年中钜惠,服务器免费领三个月
  20. 为什么黑客不攻击支付宝?

热门文章

  1. 王者荣耀服务器维修多久,王者荣耀服务器正在维护中,具体维护时间及更新内容可留意官网更新公告_游戏吧...
  2. 树莓派3B+ 编译Qt源码
  3. java用正则表达式判断字符串中是否仅包含英文字母、数字和汉字_灵思致远Leansmall的博客-CSDN博客_java判断字符串只包含数字字母
  4. iPhone手机史上最全的扫盲帖【解锁,激活,Cydia,基带,裸机】
  5. java 离线语音识别
  6. 【虚幻引擎】UE4/UE5 后期处理盒子(PostProcessVolume)
  7. ISAM error: no free disk space故障分析处理
  8. 基于社交模型的权证交易概念产品-理想中的目录
  9. 汽车零部件ELV测试标准
  10. 一条线 理解SSH登录前因后果