monkeyrunner的翻譯和解釋

monkeyrunner
monkeyrunner 提供了一些api給寫程序來控制一個android設備和一個模擬器從android的代碼的後面。使用monkerrunner ,你可以寫python程序安裝一個android應用或者測試包,運行它,發送按鍵給它,截圖用戶界面和存儲截圖到用戶的工作的地方。monkeyrunner工具是一個主要用來設計測試應用和設備在功能、框架水平和運行unit測試套,但是是免費使用的對於其他的目的。

monkeyrunner工具不關聯Monkey工具的,monkey運行在通過adb sehll直接在設備或者模擬器上並且產生(用戶和系統的時間)僞隨機流。
對比,monkerrunner工具控制設備和模擬器從一個工作區域通過發送指定的命令行和時間從一個api中。(也即是monkerrunner是Selenium2的方式,而monkey是Selenium1的方式)
Selenium1:是將腳本放到設備上運行
Selenium2:在pc上運行來控制設備。

monkerrunner提供的一些特色:
1、多設備控制:monkerrunner 的api能夠提供一個或者多個測試套通過多種設備或者模擬器。你可以通過物理點擊所有的設備或者啓動所有的模擬器同時,鏈接在依次的程序中,和運行一個或者多個測試。你能夠啓動一個模擬配置編程,運行一個或者多個測試和然後關閉模擬器。
2、功能測試:monkerrunner 能夠運行一個自動的開始到完成測試android應用的測試。你提供一個輸入值通過按鍵或者觸摸事件和查看截圖的結果。
3、迴歸測試:測試應用的穩定性通過運行一個應用和比較它的輸出截圖到一系列截圖,然後知道是對的。
4、可擴展自動化 :因爲monkerrunner是一個api工具包。你可以開發一個機遇python模塊和程序開發一個完成的系統控制android設備。在使用這些api,你可以使用標準的python操作系統和子進程模塊去調用android工具eg:adb。
同時,你也可以增加你的類庫到monkeyruuner的api中,

一個簡單的monkerrunner程序
這裏是一個monkeyrunner程序連接設備和創建monkeyDevice對象,使用功能monkeyDevice兌現個,程序安裝一個android應用程序包,運行一個活動和發送關鍵事件到活動。程序獲得一個截屏的結果,創建monkerImage對象。 同這個對象,程序寫出一個.png 文件截屏裏。

# Imports the monkeyrunner modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice

# Connects to the current device, returning a MonkeyDevice object
device = 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 name
package = 'com.example.android.myapplication'

# sets a variable with the name of an Activity in the package
activity = 'com.example.android.myapplication.MainActivity'

# sets the name of the component to start
runComponent = package + '/' + activity

# Runs the component
device.startActivity(component=runComponent)

# Presses the Menu button
device.press('KEYCODE_MENU', MonkeyDevice.DOWN_AND_UP)

# Takes a screenshot
result = device.takeSnapshot()

# Writes the screenshot to a file
result.writeToFile('myproject/shot1.png','png')

monkeyrunner的api
monkeyrunner包括三個模塊在包com.android.monkeyrunner 中
monkeyRunner:對於monkeyrunner編程是一個有效的類,這個類提供了連接誒monkeyrunner和設備或者模擬器。它同樣提供方式去創建UIs給monkeyrunner程序和給顯示創建的幫助。
monkeyDevice:表示一個設備和一個模擬器。這個類庫提供了去安裝或者下載包,開始一個活動和發送一個按鍵或者觸摸事件到一個應用程序中,你同樣可以使用功能這個類庫運行測試包。
monkeyImage;表示一個屏幕的截圖圖像。這個類提供方式去截屏和轉化bitmap圖像到多種格式,比較兩個monkeyImage對象,和一個圖像到文件中。

在一個python程序中,你接受每一個類想python模塊,monkeyrunner工具並沒有自動導入這些模塊。導入這個模塊,eg:通過“from”關鍵字。
from com.android.monkeyrunner import <module>
<module>是一個你想導入的類。你可以導入多於一個module在一個相同的from表述中通過區分模塊的名字通過“逗號”。

運行monkeyrunner
你可以 運行monkeyrunner程序從一個文件、或者寫入monkeyrunner表述在要給交互的回話中。你可以處理他們動過調用monkeyrunner命令,可以查找到在SDK目錄的tools/子目錄下。如果提供一個文件名爲參數, monkeyrunner命令運行文件的內容(如:python程序);其他的,它啓動一個交互的回話。

monkeyrunner 命令的格式:
monkeyrunner -plugin <plugin_jar> <program_filename> <program_options>

monkeyrunner Built-in Help(創建的幫助)
產昇一個api藉口給monkeyrunner通過運行:
monkeyrunner help.py <format> <outfile>

參數:
<format> text、html文件輸出選擇
<outfile> 輸出的路徑

Extending monkeyrunner with Plugins
(擴展monkeyrunner通過插件)
你可以擴展monkeyrunner api 通過你寫的java程序和創建一個或者多個jar文件,你能夠使用功能這些特色來擴展monkeyrunner的api 使用你自己的類或者擴展已經存在的類,你同樣能夠使用這些特色來初始化monkeyrunner的環境。
提供插件給monkeyrunner,需要調用monkeyrunenr 命令通過 -plugin <plugin_jar> 在table 1.
在對monkeyrunner提供插件的時候,你能夠導入和擴展主要的monkeyrunenr類庫MonkeyDevice, MonkeyImage, and MonkeyRunner 在 com.android.monkeyrunner .

說明:插件沒有提供訪問android SDK ,你不能狗導入包像com.android.app. 這個以爲你monkeyrunner 和設備、模擬器交互在框架的APIs的水平上。

The plugin startup class(插件的啓動類)
.jar 文件對於插件能夠指定一個類實例化之前解腳本的執行的開始。去指定一個類,增加關鍵的MonkeyRunnerStartupRunner 到.jar文件的manifest裏。值必須是類的名字去運行在開始。接下來的片段顯示了你怎麼做在一個ant並且創建腳本。
<jar jarfile="myplugin" basedir="${build.dir}">
<manifest>
<attribute name="MonkeyRunnerStartupRunner" value="com.myapp.myplugin"/>
</manifest>
</jar>

訪問monkeyrunner運行時的環境,啓動類能夠實現com.google.common.base.Predicate<PythonInterpreter>
eg:這些類建立一些有用的變量在默認的命名空間。
package com.android.example;
import com.google.common.base.Predicate;
import org.python.util.PythonInterpreter;

public class Main implements Predicate<PythonInterpreter> {
    @Override
    public boolean apply(PythonInterpreter anInterpreter) {

        /*
        * Examples of creating and initializing variables in the monkeyrunner environment's
        * namespace. During execution, the monkeyrunner program can refer to the variables "newtest"
        * and "use_emulator"
        *
        */
        anInterpreter.set("newtest", "enabled");
        anInterpreter.set("use_emulator", 1);

        return true;
    }
}

總結:

monkeyrunner:就是一個Selenium2的一個app的測試框架,他是一個在pc機上通過連接來控制手機的app原生的版本。

發佈了144 篇原創文章 · 獲贊 11 · 訪問量 24萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章