国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android模擬用戶點(diǎn)擊的實(shí)現(xiàn)方法

2019-10-22 18:14:38
字體:
供稿:網(wǎng)友

前言

Android模擬用戶點(diǎn)擊。在自動(dòng)化測(cè)試中可使用的工具。

可以利用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/55384.html">android/211423.html">android對(duì)應(yīng)的keycode,比如home鍵的keycode=3,back鍵的keycode=4

tap是touch屏幕的事件,只需給出x、y坐標(biāo)即可

swipe模擬滑動(dòng)的事件,給出起點(diǎn)和終點(diǎn)的坐標(biāo)即可

編寫一個(gè)bat腳本,模擬用戶滑動(dòng)

@echo offecho --------- Mock start ----------:tag_startecho running...adb shell input swipe 650 250 200 666@ping 127.0.0.1 -n 8 >nulgoto tag_startecho --------- Mock finish ---------pause

死循環(huán)發(fā)送滑動(dòng)命令,延時(shí)語(yǔ)句@ping 127.0.0.1 -n 8 >nul

monkeyrunner

環(huán)境配置,配置好Java與Android SDK的環(huán)境變量。手機(jī)連接到電腦。
系統(tǒng)變量中加入ANDROID_SWT,此例中路徑為G:/SDK/tools/lib/x86_64

修改后的腳本rustmonkeyrunner.bat,Windows環(huán)境下需要在gitbash或CMD里運(yùn)行

來自u(píng)nable-to-access-jarfile-framework-monkeyrunner-25-3-2-jar

@echo offrem Copyright (C) 2010 The Android Open Source Projectremrem 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 atremrem  http://www.apache.org/licenses/LICENSE-2.0remrem Unless required by applicable law or agreed to in writing, softwarerem 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 andrem limitations under the License.rem don't modify the caller's environmentsetlocalrem 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=%~f0rem Change current directory and drive to where the script is, to avoidrem issues with directories containing whitespaces.cd /d %~dp0rem Check we have a valid Java.exe in the path.set java_exe=call ../lib/find_java.batif not defined java_exe goto :EOFfor /f %%a in ("%APP_HOME%/lib/monkeyrunner-25.3.2.jar") do set jarfile=%%~nxaset frameworkdir=.set libdir=if exist %frameworkdir%/%jarfile% goto JarFileOk set frameworkdir=../libif exist %frameworkdir%/%jarfile% goto JarFileOk set frameworkdir=../framework:JarFileOkset 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:SwtDoneif 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:SetPathcall "%java_exe%" -Xmx512m "-Djava.ext.dirs=%frameworkdir%;%swt_path%" -Dcom.android.monkeyrunner.bindir=../../platform-tools -jar %jarpath% %*

運(yùn)行腳本

Administrator@rust-PC ~$ /cygdrive/g/SDK/tools/bin/rustmonkeyrunner.batJython 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

首次運(yùn)行時(shí)import模塊遲遲沒有反應(yīng)

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

嘗試運(yùn)行腳本an_test2.py

import osprint("importing module...")from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImageprint("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 = 20drag_point_right_x = s_wid - 20drag_point_y = s_height / 2for 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 --------")

命令行直接執(zhí)行,可以看到執(zhí)行結(jié)果和相應(yīng)的報(bào)錯(cuò)信息

C:/Users/Administrator>G:/SDK/tools/bin/rustmonkeyrunner.bat H:/fisher_p/py_ws/an_test2.pyimporting module...waiting for connection...device found!build.version.sdk 23display.width  1440display.height 2560current loop is 0waiting...continuecurrent loop is 1# .....-------- finish --------

測(cè)試中發(fā)現(xiàn),腳本可以運(yùn)行在系統(tǒng)app。若當(dāng)前打開的是第三方app,會(huì)直接報(bào)錯(cuò),獲取不到相應(yīng)信息

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)VEVB武林網(wǎng)的支持。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 长顺县| 靖远县| 渑池县| 嘉荫县| 伊通| 定州市| 集安市| 昭苏县| 福安市| 甘谷县| 伊春市| 卓资县| 宁都县| 泾阳县| 肇东市| 滁州市| 宝丰县| 望奎县| 镇坪县| 岗巴县| 汶川县| 武鸣县| 成武县| 竹溪县| 噶尔县| 辽中县| 赤水市| 海林市| 许昌市| 岢岚县| 虹口区| 吴忠市| 东乌珠穆沁旗| 保山市| 万全县| 大英县| 绥化市| 托克托县| 昭通市| 汾西县| 达拉特旗|