【金陽光測試】Android自動化 -- 學習曆程:MonkeyRunner原理初步

來源:互聯網
上載者:User

標籤:des   android   style   blog   http   color   java   使用   

章節:自動化基礎篇——MonkeyRunner原理初步

網易雲課堂:

http://study.163.com/course/courseLearn.htm?courseId=712011#/learn/video?lessonId=877115&courseId=712011

 

主要講解內容及筆記:

一、理論知識和指令碼示範

最佳方式是上官網文檔去查看monkeyrunner的介紹,官網上不去,就找了一個本地的android 4.2 的查看,基本內容沒啥變化

First,什麼是MonkeyRunner?

英文文檔如下:

The monkeyrunner tool provides an API for writing programs that control an Android device or emulator from outside of Android code. With monkeyrunner, you can write a Python program that installs an Android application or test package, runs it, sends keystrokes to it, takes screenshots of its user interface, and stores screenshots on the workstation. The monkeyrunner tool is primarily designed to test applications and devices at the functional/framework level and for running unit test suites, but you are free to use it for other purposes.  

翻譯過來就是:

這個monkeyrunner的工具,提供了一個API來供大家編程啦,用以控制一個android裝置或者模擬器,而不用瞭解這個android的源碼。有了這個monkeyrunner,我們可以寫一個Python的程式來安裝android程式或者測試包,運行它,給它發送一些鍵盤控制事件,進行使用者介面的,並將儲存在工作站。monkeyrunner的工具的作用是在功能/架構層上測試應用和裝置,跑單元測試用例,當然你也可以用它做其他的,我們就管不著了。

並且monkeyrunner與monkey不同哦,monkey是用adb shell的這個命令列,發送一堆來自使用者或者系統的隨機類比事件,主要是用來做壓力測試的。

 

Second,MonkeyRunner能做什嗎?

英文文檔如下:

The monkeyrunner tool provides these unique features for Android testing:

  • Multiple device control: The monkeyrunner API can apply one or more test suites across multiple devices or emulators. You can physically attach all the devices or start up all the emulators (or both) at once, connect to each one in turn programmatically, and then run one or more tests. You can also start up an emulator configuration programmatically, run one or more tests, and then shut down the emulator.
  • Functional testing: monkeyrunner can run an automated start-to-finish test of an Android application. You provide input values with keystrokes or touch events, and view the results as screenshots.
  • Regression testing - monkeyrunner can test application stability by running an application and comparing its output screenshots to a set of screenshots that are known to be correct.
  • Extensible automation - Since monkeyrunner is an API toolkit, you can develop an entire system of Python-based modules and programs for controlling Android devices. Besides using the monkeyrunner API itself, you can use the standard Python os and subprocess modules to call Android tools such as Android Debug Bridge.

    You can also add your own classes to the monkeyrunner API. This is described in more detail in the section Extending monkeyrunner with plugins.

翻譯過來就是:

monkeyrunner提供了如下一些android測試的獨特功能:

1、多裝置控制:那就是說可以跨裝置。

2、功能測試:可在一個android的應用上自動跑一次從頭到尾的測試,需要提供鍵盤或者touch 事件的值,通過截屏方式查看結果。

3、迴歸測試:monkeyrunner可通過跑一個應用程式,並將它的輸出與標準的正確輸出集合相對比來測試應用程式的穩定性。一般在功能比較穩定的時候,下次迴歸的時候,做一個基本的功能迴歸是必須的,並且通過自動化會很節省資源。

4、可擴充的自動化測試:monkeyruner本身就是一個API的小工具,我們當然可以開發一個基於python的模組或者程式來控制android裝置。當然也可以利用標準Python的os或者subprocess模組來調用android工具,如adb。

 

Third,MonkeyRunner怎麼用?

官網提供的具體程式碼範例如下:

 1 # Imports the monkeyrunner modules used by this program 2 from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice 3  4 # Connects to the current device, returning a MonkeyDevice object 5 device = MonkeyRunner.waitForConnection() 6  7 # Installs the Android package. Notice that this method returns a boolean, so you can test 8 # to see if the installation worked. 9 device.installPackage(‘myproject/bin/MyApplication.apk‘)10 11 # sets a variable with the package‘s internal name12 package = ‘com.example.android.myapplication‘13 14 # sets a variable with the name of an Activity in the package15 activity = ‘com.example.android.myapplication.MainActivity‘16 17 # sets the name of the component to start18 runComponent = package + ‘/‘ + activity19 20 # Runs the component21 device.startActivity(component=runComponent)22 23 # Presses the Menu button24 device.press(‘KEYCODE_MENU‘, MonkeyDevice.DOWN_AND_UP)25 26 # Takes a screenshot27 result = device.takeSnapshot()28 29 # Writes the screenshot to a file30 result.writeToFile(‘myproject/shot1.png‘,‘png‘)

OK,具體步驟如下:

(1)先去import一下MonkeyRuner和MonkeyDevice類

(2)串連裝置,device = MonkeyRunner.waitForConnection(),等待裝置串連,並返回一個device的執行個體

(3)安裝一下安裝包(寫上絕對路徑就好啦),這裡會返回布爾型變數,可通過這個值,寫一個if語句,判斷是否安裝成功

(4)搞到安裝後的包名(在manifest.xml中配置的),搞到包內的Activity的名字,這個是如何擷取的?

這裡插進來一段話,用Python擷取包名和主Activity的名字:

http://blog.csdn.net/pugongying1988/article/details/7349068

這裡講解了如何擷取,但是需要在手機或者模擬器上配置android環境;這裡用到了python for android的內容,能夠通過一些線程的api介面擷取到;當然也可以用java程式來編寫擷取

(5)使用包名加Activity的名字運行,通過startActivity跑起來

(6)開始按鍵啦,或者touch事件啦

(7)將(6)中的結果截個圖

(8)寫到一個目錄下,儲存起來,之後就可以與正確的結果做對比了。但是結果如何對比目前還不知道。?????

 

自己搞一個apk程式,用python寫一段測試代碼,跑一下自動化: 

1 # coding=utf-82 3 from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice4 device = MonkeyRunner.waitForConnection()5 device.shell("monkey -p com.qihoo.linker -v 1000")

比如像上面這樣的,但是

(1)目前有一個問題就是頁面很多的話,各個介面之間會有跳轉,這種情況如何做monkey的壓力測試呢?

因為我在測試的過程中,隨機產生的一些事件是應該可以點擊home鍵或者是返回鍵的,這樣可能導致應用退出,如何處理?

(2)針對單個頁面,可以通過座標點方式對其進行DOWN_AND_UP,或者是其他一些操作。

 

二、封裝MonkeyRunner錄製

 

開始執行個體代碼:

java調用android自動化的monkeyrunner,具體程式結構為:

 

首先,因為我的是高版本的jar包,因此需要首先從sdk/tools/lib下匯入chimpchat.jar包(用來定義AdbBackend執行個體和AdbChimpDevice執行個體,建立adb橋接和產生裝置執行個體),另外需要匯入monkeyrunner.jar(用來定義MonkeyRecorederFrame執行個體,這個是用來錄製的),之後在下面直接輸入你需要定義的對象,就能夠import上對應的class類

源碼MyTest.java如下,測試過程如下:

 1 package com.android.test; 2  3 import com.android.chimpchat.adb.AdbBackend; 4 import com.android.chimpchat.adb.AdbChimpDevice; 5 import com.android.monkeyrunner.recorder.MonkeyRecorderFrame; 6  7  8 public class MyTest { 9 10     /**11      * @param args12      */13     14     private static AdbChimpDevice device;15     private static AdbBackend adb;16     17     public static void main(String[] args) {18         // TODO Auto-generated method stub19     20         if (adb==null){ 21             adb = new AdbBackend(); 22             // 參數分別為自己定義的等待連線時間和裝置id;當然也可為空白23             // 這裡需要注意一下adb的類型24             device = (AdbChimpDevice) adb.waitForConnection();25             26             if (device != null)27             {28                 MonkeyRecorderFrame frame = new MonkeyRecorderFrame(device);29                 frame.setVisible(true);30             }31         } 32 33     }34 35 }

接下來是測試過程,右鍵MyTest.java,Run as->Java Application即可,可能啟動起來會比較慢(需要串連上自己的手機,可使用cmd命令下adb devices查看是否串連成功了,如果出現unauthorized問題,可以找到開發人員選項,選擇“撤回USB除錯授權”即可,本人所有手機為三星Galaxy S3,其他手機也類似;之後重新插拔手機,串連後就會出現是否授權提示,點擊確定即可)

然後程式運行起來就是這個:

點擊螢幕或者是選擇Press a Button之後都會有響應,能夠在右邊看到點擊了哪些位置

之後點擊上面的Export Actions,輸入檔案名稱如a,不用要尾碼名,儲存在自己可以記得目錄下即可,之後用notepad++開啟,如下:

PRESS|{‘name‘:‘HOME‘,‘type‘:‘downAndUp‘,}
TOUCH|{‘x‘:571,‘y‘:370,‘type‘:‘downAndUp‘,}
TOUCH|{‘x‘:438,‘y‘:170,‘type‘:‘downAndUp‘,}
TOUCH|{‘x‘:162,‘y‘:312,‘type‘:‘downAndUp‘,}
TOUCH|{‘x‘:240,‘y‘:509,‘type‘:‘downAndUp‘,}
TOUCH|{‘x‘:227,‘y‘:730,‘type‘:‘downAndUp‘,}
TOUCH|{‘x‘:497,‘y‘:666,‘type‘:‘downAndUp‘,}
TOUCH|{‘x‘:351,‘y‘:925,‘type‘:‘downAndUp‘,}
TOUCH|{‘x‘:110,‘y‘:960,‘type‘:‘downAndUp‘,}
TOUCH|{‘x‘:535,‘y‘:968,‘type‘:‘downAndUp‘,}
TOUCH|{‘x‘:132,‘y‘:296,‘type‘:‘downAndUp‘,}
TOUCH|{‘x‘:630,‘y‘:224,‘type‘:‘downAndUp‘,}
PRESS|{‘name‘:‘HOME‘,‘type‘:‘downAndUp‘,}
TOUCH|{‘x‘:848,‘y‘:514,‘type‘:‘downAndUp‘,}
TOUCH|{‘x‘:130,‘y‘:389,‘type‘:‘downAndUp‘,}

 

三、A模式(座標點)的特點<註:還有基於控制項的B模式>

下面講解一下針對不同的解析度怎麼做處理:

如320*480的,怎麼移植到480*600的上面? 

device.touch(340,580,‘DOWN_AND_UP‘)

320/x = 480/newx, x = 340

480/y = 600/newy, y = 580

這樣就能求得一個比例關係了,用這個比例關係做一個類似宏替換就行,代碼基本不用動;甚至可以寫一個方法,擷取到像素後,與基礎的計算後,直接替換即可。

除以上方式,還可以運用hierarchyviewer擷取控制項,通過控制項找到座標。缺點:要開root,4.1.2以下要開root。所以俺的手機顯示不了了!之後用UIAutomator就可以了,不用開root也可以。

四、後續持續整合

這裡講了一個Robotium的Recorder工具,是用來進行指令碼錄製的,只有30天試用期。

 

1、官網:www.goldensunshine.cc
2、百度搜:金陽光測試,找到金陽光學習資料和官網、視頻。
3、官方新浪微博:金陽光woody
4、官方qq:212260449
6、官方YY教育頻道:74894998
7、官方CSDN技術資料:http://blog.csdn.net/haorenmin2008

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.