Android App 專項測試--壓力測試篇(三)

第三部分(腳本)

MonkeyScript

  1. 執行monkey 腳本的命令:

      adb shell monkey  -f  < scriptfile > < event -count >

      (1)軌跡球事件

       DispatchTrackball 命令: DispatchTrackball(long downtime,long eventide,int action, float x,float y, float pressure, float size, int metastate, float xprecision, float yprecision, int device, int edgeflags)

    action  0 代表 按下, 1 代表 彈起, x 和 y 帶表的座標點

   (2)點擊事件

    DispatchPointer(long downtime,long eventide,int action, float x,float y, float pressure, float size, int metastate, float xprecision, float yprecision, int device, int edgeflags)

     action 0 代表按下, 1 代表彈起, x 和 y 代表的座標點

   (3)輸入字符串的事件

    DispatchString (String text)

   (4)啓動應用

    LauchActivity(package,Activity)

   (5)等待事件

    UserWait(millonseconds)

    (6) 按下鍵值

    DispatchPress(int keycode)   #keycode 66 回車鍵

   事例:在搜索框中輸入查詢詞  重複2次

           adb shell monkey  -f  monkey.script  

#實現在搜索框 搜索關鍵詞
#1. 啓動app
#2. 點擊輸入框
#3. 輸入查詢詞
#4. 點擊鍵盤上的回車
#5. 點擊搜索按鈕
#6. 等待結果的出現

type = user
count = 10
speed = 1.0
start data >>

LaunchActivity(com.example.zhangjian.minibrowser2,
com.example.zhangjian.minibrowsers.MainActivity) UserWait(
2000) # 點擊座標(100,100) 0 代表按下 DispatchPointer(10,10,0,100,100,1,1,-1,1,1,0,0) # 點擊座標(100,100) 1 代表彈起 DispatchPointer(10,10,1,100,100,1,1,-1,1,1,0,0) #輸入框 輸入 test DispatchString(test) UserWait(1000) # 點擊回車的keycode 值爲66 DispatchPress(66) UserWait(1000) # 點擊搜索按鈕 DispatchPointer(10,10,0,400,100,1,1,-1,1,1,0,0) DispatchPointer(10,10,1,400,100,1,1,-1,1,1,0,0) # 等待6秒 UserWait(6000)

     注意:若app 不能被launch起來,檢查 manifest.xml這句 : < activity android:name = ".MainActivity"   android:exported= "true" >   允許被外部調起

   

Monkey Runner

  1. MonkeyRunner API - alert

   void alert (string message,striing title, striing okTitle)

1 #!python
2 #_*_UTF-8_*_
3 from com.android.monkeyrunner import MonkeyRunner
4 MonkeyRunner.alert("Hello mook frends", "This is title","OK")
5 
6 # 執行方式
7 #monkeyrunner demo.py

  2. MonkeyRunner API - waitForConnection

   waitForConnection(float timeout, string deviceid)

  3. MonkeyDevice API - drag(拖動)

   drag(tuple start, tuple end, float duration, integer steps)

   start 起點位置

   end 終點位置

   duration 手勢持續的時間

   steps 插值點的步數,默認 10 

  4. MonkeyDevice API - press(按鍵)

   press(string keycode,dictionary type)

   keycode 名,Down、UP、DOWN_AND_UP

  5. MonkeyDevice API - startActivity(啓動應用)

   startActivity(package/ activity)  

  6. MokeyDevice API - touch(點擊)

   touch(integer x,integer y, integer type)

   x 座標值, y座標值

    type: DOWN,UP,DOWN_AND_UP

  7. MonkeyDevice API- takeSnapshot(截屏)

   MonkeyDevice.takeSnapshot()

  8.MonkeyImage API - sameAs

   圖像對比

   boolean sameAs (MonkeyImage other, float percent )

  9. MonkeyImage API - writeToFile(保存圖像)

   void writeToFIle(string path, string format)

  實例:monkeyrunner  mook.py     

 1 #實例 搜索 建軍90年閱兵
 2 #!python
 3 #_*_UTF-8_*_
 4 from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage
 5 #連接設備
 6 device = MonkeyRunner.waitForConnection(3,"192.168.26.101:5555")
 7 #啓動app
 8 device.startActivity("com.example.zhangjian.minibrowser2/com.example.zhangjian.minibrowser2.MainActivity")
 9 MonkeyRunner.sleep(2)
10 #點擊搜索框
11 device.touch(100,100,"DOWN_AND_UP")
12 MonkeyRunner.sleep(1)
13 #輸入查詢詞
14 device.type("建軍90年閱兵")
15 MonkeyRunner.sleep(1)
16 #點擊回車鍵
17 device.press("KEYCODE_ENTER","DOWN_AND_UP")
18 MonkeyRunner.sleep(1)
19 #點擊搜索框
20 device.touch(400,100,"DOWN_AND_UP")
21 MonkeyRunner.sleep(6)
22 #截屏
23 image = device.takeSnapshot()
24 image.writeToFile('./test.png','png')
25 #點擊清除按鈕
26 device.touch (300,100,"DOWN_AND_UP")
27 MonkeyRunner.sleep(3)
mook.py

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章