qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請(qǐng)?jiān)L問(wèn) http://qaseven.github.io/

          Android Monkeyrunner Test

            關(guān)于Android自動(dòng)化測(cè)試,研究了Monkey,Robotium 這次來(lái)看下 Monkeyrunner.
            具體實(shí)踐最靠譜的當(dāng)然還是官網(wǎng)資料了。
            http://developer.android.com/tools/help/monkeyrunner_concepts.html
            這里簡(jiǎn)單記錄下實(shí)踐過(guò)程,Monkeyrunner 需要用Python來(lái)編寫(xiě),對(duì)于曾未學(xué)過(guò)Python的童鞋來(lái)說(shuō)也沒(méi)關(guān)系,因?yàn)镻ython屬于比較好學(xué)的一門腳本語(yǔ)言.筆者也未曾學(xué)過(guò)Python,但有其他編程基礎(chǔ)如:PHP,Java,Peal,還是能夠很好理解Python的。
            一、monkeyrunner 介紹
            monkeyrunner 提供了一個(gè)API,使用此API寫(xiě)出的程序可以在Android代碼之外控制Android設(shè)備和模擬器.
            二、monkeyrunner 測(cè)試類型
            多設(shè)備控制、功能測(cè)試、回歸測(cè)試
            三、實(shí)例(測(cè)試MyAndroid.apk)
            1. 新建一個(gè) monkeyrunnerTest.py
          # Import the monkey runner modules used by this program
          from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
          # Connects to current device, returning a MonkeyDevice object
          device = MonkeyRunner.waitForConnection()
          # Installs the Android package
          device.installPackage("./MyAndroid.apk")
          # Runs the component
          device.startActivity(component = 'com.luwenjie.android/.MyAndroidActivity')
          #Presses the Menu button
          device.press('KEYCODE_MENU','DOWN_AND_UP')
          #Takes a screenshot
          result = device.takeSnapshot()
          # Writes the screenshot to a file
          result.writeToFile('./shot1.png','png')
            2. 運(yùn)行在 %Android_HOME%\tools 目錄下運(yùn)行一下命令
            monkeyrunner  monkeyrunnerTest.py
            四、API參考
            MonkeyRunner:http://developer.android.com/tools/help/MonkeyRunner.html
            MonkeyDevice:http://developer.android.com/tools/help/MonkeyDevice.html
            MonkeyImage:http://developer.android.com/tools/help/MonkeyImage.html  五、錄制模式
          #Usage: monkeyrunner recorder.py
          #recorder.py  http://mirror.yongbok.net/linux/android/repository/platform/sdk/monkeyrunner/scripts/monkey_recorder.py
          from com.android.monkeyrunner import MonkeyRunner as mr
          from com.android.monkeyrunner.recorder import MonkeyRecorder as recorder
          device = mr.waitForConnection()
          recorder.start(device)
          #END recorder.py
          #Press ExportAction to save recorded scrip to a file
          #Example of result:
          #PRESS|{'name':'MENU','type':'downAndUp',}
          #TOUCH|{'x':190,'y':195,'type':'downAndUp',}
          #TYPE|{'message':'',}
          #Usage: monkeyrunner playback.py "myscript"
          #playback.py   http://mirror.yongbok.net/linux/android/repository/platform/sdk/monkeyrunner/scripts/monkey_playback.py
          import sys
          from com.android.monkeyrunner import MonkeyRunner
          # The format of the file we are parsing is very carfeully constructed.
          # Each line corresponds to a single command.  The line is split into 2
          # parts with a | character.  Text to the left of the pipe denotes
          # which command to run.  The text to the right of the pipe is a python
          # dictionary (it can be evaled into existence) that specifies the
          # arguments for the command.  In most cases, this directly maps to the
          # keyword argument dictionary that could be passed to the underlying
          # command.
          # Lookup table to map command strings to functions that implement that
          # command.
          CMD_MAP = {
          'TOUCH': lambda dev, arg: dev.touch(**arg),
          'DRAG': lambda dev, arg: dev.drag(**arg),
          'PRESS': lambda dev, arg: dev.press(**arg),
          'TYPE': lambda dev, arg: dev.type(**arg),
          'WAIT': lambda dev, arg: MonkeyRunner.sleep(**arg)
          }
          # Process a single file for the specified device.
          def process_file(fp, device):
          for line in fp:
          (cmd, rest) = line.split('|')
          try:
          # Parse the pydict
          rest = eval(rest)
          except:
          print 'unable to parse options'
          continue
          if cmd not in CMD_MAP:
          print 'unknown command: ' + cmd
          continue
          CMD_MAP[cmd](device, rest)
          def main():
          file = sys.argv[1]
          fp = open(file, 'r')
          device = MonkeyRunner.waitForConnection()
          process_file(fp, device)
          fp.close();
          if __name__ == '__main__':
          main()

          posted on 2014-01-02 09:36 順其自然EVO 閱讀(250) 評(píng)論(0)  編輯  收藏 所屬分類: android

          <2014年1月>
          2930311234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 商南县| 建德市| 宝清县| 黄大仙区| 东台市| 开封市| 上犹县| 托里县| 公安县| 萍乡市| 都江堰市| 临漳县| 屏山县| 曲沃县| 都昌县| 兴仁县| 民乐县| 湖州市| 晋江市| 连城县| 宜春市| 金堂县| 绥江县| 沐川县| 吉首市| 日照市| 连州市| 内江市| 惠州市| 铜陵市| 固阳县| 扎囊县| 富源县| 抚州市| 梅河口市| 二手房| 富顺县| 三河市| 涟源市| 银川市| 衡水市|