Implementation method of Android simulating user click

  • 2021-08-21 21:17:11
  • OfStack

Preface

Android simulates user clicks. Tools available in automated testing.

You can use the adb command or the monkeyrunner tool in Android SDK.

win7-64 gitbash

Use the adb command

Mainly use input command


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

keyevent refers to keycode corresponding to android, such as keycode=3 of home bond and keycode=4 of back bond

tap is an event on the touch screen. You only need to give the coordinates of x and y

swipe simulates sliding events and gives the coordinates of the starting point and the ending point

Write an bat script to simulate user sliding


@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

Send sliding command in an infinite loop, delay statement @ ping 127.0. 0.1-n 8 > nul

monkeyrunner

Environment configuration, configure the environment variables of Java and Android and SDK. The mobile phone is connected to the computer.
Add ANDROID_SWT to the system variable, in this case the path is G:\ SDK\ tools\ lib\ x86_64

Modified script rustmonkeyrunner. bat, Windows environment needs to run in gitbash or CMD

From 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% %*

Run the script


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

The import module did not respond when it was run for the first time


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

Try to run the script 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"))  #  Get screen width pixels 
s_height = int(device.getProperty("display.height")) #  Get screen height pixels 

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 --------")

Command line direct execution, you can see the execution results and the corresponding error message


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 --------

The test found that the script can run on the system app. If the third party app is currently opened, an error will be reported directly, and the corresponding information cannot be obtained

Summarize


Related articles: