monkeyrunner on eclipse

來源:互聯網
上載者:User

標籤:android測試   jython   monkeyrunner   

monkeyrunner是Android提供的自動化測試載入器,而且它還提供了Python介面,使我們能夠使用程式控制應用的行為,例如:安裝應用,根據包名啟動應用,點擊、滑動事件等。在eclipse中使用monkeyrunner需要一些配置:

1.下載並安裝python;

2.eclipse中安裝python外掛程式;

3.下載並安裝jython.jar;

4.配置eclipse;

由於前3步都比較簡單,這裡就不過多描述了,直接第4步。

a.配置python解譯器,點擊Window->Preferences->PyDev->Interpreters,如所示:

b.配置jython,點擊Window->Preferences->PyDev->Interpreters,另外,還要把monkeyrunner.jar添加到Libraries中,如所示:


c.接下來就可以建立jython項目,

d.最後就是建立python代碼,一段簡單的代碼,從Android官網上拷貝的,這段程式碼封裝括行為有:安裝MyApplication.apk,開啟MainActivity,點擊Menu

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice# Connects to the current device, returning a MonkeyDevice objectdevice = MonkeyRunner.waitForConnection()# Installs the Android package. Notice that this method returns a boolean, so you can test# to see if the installation worked.device.installPackage('myproject/bin/MyApplication.apk')# sets a variable with the package's internal namepackage = 'com.example.android.myapplication'# sets a variable with the name of an Activity in the packageactivity = 'com.example.android.myapplication.MainActivity'# sets the name of the component to startrunComponent = package + '/' + activity# Runs the componentdevice.startActivity(component=runComponent)# Presses the Menu buttondevice.press('KEYCODE_MENU', MonkeyDevice.DOWN_AND_UP)# Takes a screenshotresult = device.takeSnapshot()# Writes the screenshot to a fileresult.writeToFile('myproject/shot1.png','png')
e.運行代碼,實際上我執行這段代碼,要靠Android提供的monkeyrunner.bat的,所以不能像平常運行程式一樣,右鍵->Run->選擇執行程式,這時候要用到eclipse的External Tools,點擊Run->External Tools->External Tools Configurations,然後添加相關配置資訊,如:


以後,每次執行的時候,就不用重新添加了。

參考資料:http://forum.xda-developers.com/showthread.php?t=2566234


monkeyrunner on eclipse

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.