最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2018年02月25日 11:27:17   作者:Rust Fisher  
這篇文章主要給大家介紹了關(guān)于Android模擬用戶點擊的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)學(xué)習(xí)學(xué)習(xí)吧。

前言

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對應(yīng)的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

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

monkeyrunner

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

修改后的腳本rustmonkeyrunner.bat,Windows環(huán)境下需要在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模塊遲遲沒有反應(yīng)

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

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

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

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

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • Android修改字體樣式的示例代碼

    Android修改字體樣式的示例代碼

    本篇文章主要介紹了Android修改字體樣式的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • Android中LayoutAnimal的使用方法詳解

    Android中LayoutAnimal的使用方法詳解

    這篇文章給大家講講酷炫的動畫成員LayoutAnimal,文章通過一個實例給大家詳細介紹了Android中LayoutAnimal的使用方法,感興趣的小伙伴可以自己動手試一試
    2023-09-09
  • Android自定義水平進度條的圓角進度

    Android自定義水平進度條的圓角進度

    這篇文章主要為大家詳細介紹了Android自定義水平進度條的圓角進度,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Android使用Volley實現(xiàn)上傳文件功能

    Android使用Volley實現(xiàn)上傳文件功能

    這篇文章主要介紹了Android使用Volley實現(xiàn)上傳文件功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • 深入理解Android Bitmap

    深入理解Android Bitmap

    Bitmap是Android系統(tǒng)中的圖像處理的最重要類之一。這篇文章主要介紹了理解Android Bitmap,需要的朋友可以參考下
    2017-11-11
  • Android編程之文件讀寫操作與技巧總結(jié)【經(jīng)典收藏】

    Android編程之文件讀寫操作與技巧總結(jié)【經(jīng)典收藏】

    這篇文章主要介紹了Android編程之文件讀寫操作與技巧,結(jié)合實例形式總結(jié)分析了Android常見的文件與目錄的讀寫操作,及相關(guān)函數(shù)的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2016-06-06
  • Android藍牙服務(wù)啟動流程分析探索

    Android藍牙服務(wù)啟動流程分析探索

    這篇文章主要介紹了Android藍牙服務(wù)啟動流程,了解內(nèi)部原理是為了幫助我們做擴展,同時也是驗證了一個人的學(xué)習(xí)能力,如果你想讓自己的職業(yè)道路更上一層樓,這些底層的東西你是必須要會的
    2023-01-01
  • Android如何實現(xiàn)URL轉(zhuǎn)換成二維碼

    Android如何實現(xiàn)URL轉(zhuǎn)換成二維碼

    這篇文章主要為大家詳細介紹了Android實現(xiàn)URL轉(zhuǎn)換成二維碼的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • Android實現(xiàn)文字滾動效果

    Android實現(xiàn)文字滾動效果

    這篇文章主要為大家詳細介紹了Android實現(xiàn)文字滾動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android啟動畫面的實現(xiàn)方法

    Android啟動畫面的實現(xiàn)方法

    這篇文章主要介紹了Android啟動畫面的實現(xiàn)方法,分析了布局文件及加載啟動文件的實現(xiàn)方法,非常具有實用價值,需要的朋友可以參考下
    2015-01-01

最新評論

东台市| 巴中市| 木兰县| 莱西市| 江山市| 海淀区| 理塘县| 农安县| 监利县| 五台县| 明溪县| 洪湖市| 伊通| 横峰县| 眉山市| 贵州省| 台北市| 肇东市| 遂昌县| 交城县| 宝清县| 东平县| 广昌县| 巴林左旗| 罗定市| 凯里市| 自贡市| 丰城市| 威海市| 长治县| 大同县| 乐山市| 卓资县| 阆中市| 宝丰县| 千阳县| 龙游县| 昭通市| 通江县| 平凉市| 天长市|