monkeyrunner 模擬測試使用者動作

2021-06-22 11:08:29 字數 3088 閱讀 5271

在sdk 中monkeyrunner 可以執行python指令碼

如:monkeyrunner test.py

from com.android.monkeyrunner import monkeyrunner,monkeydevice,monkeyimage

device=monkeyrunner.waitforconnection()

if not device:

print "please connect a device to start!"

else:

print "start "

log=open("camera/log.txt","w")

for i in range(1,10000):

device.startactivity(component='com.android.gallery3d/com.android.camera.cameraactivity')

monkeyrunner.sleep(1)

device.press('keycode_camera','down_and_up')

monkeyrunner.sleep(3)

photo=device.takesnapshot()

photo=photo.getsubimage((0,127,1278,612))

photo.writetofile("././camera/camera["+str(i)+"].png","png")

device.press('keycode_back','down_and_up')

monkeyrunner.sleep(1)

log.write("["+str(i)+"]cycle.\n\n")

log.close()

語法

1. #匯入模組;

from com.android.monkeyrunner import monkeyrunner, monkeydevice, monkeyimage

2. #連線當前裝置,並返回乙個monkeydevice物件;

device = monkeyrunner.waitforconnection()

if not device:

print "please connect a device to start!"

else:

print "start "

3. #安裝android包,注意,此方法返回的返回值為boolean,由此可以判斷安裝過程是否正常 ;

device.removepackage ('com.example.android.notepad')

print ('解除安裝成功')

device.installpackage('apidemos.apk')

print ('安裝成功')

4. #啟動乙個activity;

device.startactivity

(component='com.android.htccontacts/com.android.htccontacts.contactstabactivity')

5. #截圖;

result = device.takesnapshot()

result.writetofile('c:\\users\\martin\\desktop\\test.png','png')

6. #時延(秒);

monkeyrunner.sleep(3)

7. #滑動螢幕;

for i in range(1,70):

device.drag((250,850),(250,110),0.1,10)

//開始,結束,持續時間,步驟   

for i in range(1,70):

device.drag((250,110),(250,850),0.1,10)

monkeyrunner.sleep(1)

8. #觸擊螢幕;

device.touch(507,72,"down_and_up")

9. #執行adb shell命令;

device.shell("input text goup01")

按下home鍵     device.press('keycode_home','down_and_up')

按下back鍵     device.press('keycode_back','down_and_up')

按下下導航鍵     device.press('keycode_dpad_down','down_and_up')

按下上導航鍵     device.press('keycode_dpad_up','down_and_up')

按下ok鍵     

device.press('keycode_dpad_center','down_and_up')

相應的按鍵對應的名稱如下:

home鍵:keycode_home

back鍵:keycode_back

send鍵:keycode_call

end鍵:keycode_endcall

上導航鍵:keycode_dpad_up

下導航鍵:keycode_dpad_down

左導航:keycode_dpad_left

右導航鍵:keycode_dpad_right

ok鍵:keycode_dpad_center

上音量鍵:keycode_volume_up

下音量鍵:keycode_volume_down

power鍵:keycode_power

camera鍵:keycode_camera

menu鍵:keycode_menu

如果出現

e/adb: unable to locate adb.

please use sdk manager and check if android sdk platform-tools are installed.

請確認sdk 目錄中是否包含platform-tools

沒有執行tools/android

安裝乙個android sdk platform-tools

並將platform-tools 放入到環境變數path 中

MonkeyRunner基本操作

1.匯入模組 from com.android.monkeyrunner import monkeyrunner,monkeydevice,monkeyimage 2.連線當前裝置,並返回乙個monkeydevice物件 device monkeyrunner.waitforconnection i...

monkey與monkeyrunner的使用

monkey是android中的乙個命令列工具,可以執行在模擬器裡或實際裝置中。它向系統傳送偽隨機的使用者事件流 如按鍵輸入 觸控螢幕輸入 手勢輸入等 實現對正在開發的應用程式進行壓力測試。monkey測試是一種為了測試軟體的穩定性 健壯性的快速有效的方法。1 測試的物件僅為應用程式包,有一定的侷限...

monkey和monkeyrunner的區別

簡單來說 1.monkey是在裝置或模擬器直接執行adb shell命令生成隨機事件來進行測試 2.monkeyrunner是通過api傳送特定的命令和事件來控制裝置 為了支援黑盒自動化測試的場景,android sdk提供了兩個測試工具 monkey和monkeyrunner,均可以向待測應用傳送...