Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 自動化測試(3)(monkeyrunner) 根據ID查找對象&touch&type (python)

Android 自動化測試(3)(monkeyrunner) 根據ID查找對象&touch&type (python)

編輯:關於Android編程

我在之前的兩篇文章中用java來實現過 Android 自動化測試(1)如何安裝和卸載一個應用(java)、Android 自動化測試(2)根據ID查找對象(java)。 但是本質上都是用monkeyrunner對應的java lib 來實現的,但是相關的文檔非常少,如果真的要用monkeyrunner來做功能性的自動化測試,強烈還是推薦使用python語言



1、monkey runner

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 工具提供了一個從Android源碼外部寫程序控制一個Android設備或者模擬器的API。
你可以用monkeyrunner寫一個Python程序,來裝一個Android應用和測試包,運行它,發key事件,截屏,存在本地。
monkeyrunner工具的設計起源於在功能/框架層面來測試應用和設備,和跑單元測試的測試套件


2、樣例實現

2.1代碼實現功能: 進入robot dream的頁面mainActivity,找到id為center_image 的按鈕發生點擊事件,然後對彈出的對話框中再次找到id為button1的按鈕,進行二次確認。

from com.android.monkeyrunner.easy import EasyMonkeyDevice, By
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice

if __name__ == '__main__':

    import codecs
    codecs.register(lambda name: codecs.lookup('utf-8') if name == 'cp65001' else None)

    print ('test start')
    device = MonkeyRunner.waitForConnection()

    easyMonkey = EasyMonkeyDevice(device)
    print ('start robot dream mainActivity')

    device.shell('am start com.robot.dream/com.robot.dream.mainActivity')

    MonkeyRunner.sleep(3)
    #easyMonkey.touch(MonkeyDevice.DOWN_AND_UP, By.id("id/center_image"));

    by = By.id("id/center_image");
    print (by)
    easyMonkey.touch(by,MonkeyDevice.DOWN_AND_UP)

    MonkeyRunner.sleep(3)

    hierachy_view = device.getHierarchyViewer()
    print(hierachy_view)

    view_node = hierachy_view.findViewById('id/center_image')
    items_node = view_node.children
    print (len(view_node.children))

    print ('touch 2')
    easyMonkey.touch(by,MonkeyDevice.DOWN_AND_UP)

    MonkeyRunner.sleep(3)

    by2 = By.id('id/button1')
    print (by2)
    print (easyMonkey.visible(by2))

    vn2 = hierachy_view.findViewById('id/button1')
    print (dir(vn2))
    print (getattr(vn2, 'name')) 
    print (vn2.id)

    easyMonkey.touch(by2, MonkeyDevice.DOWN_AND_UP)

    MonkeyRunner.sleep(3)


2.2 代碼實現功能: 進入robot dream的登陸頁面LoginActivity,根據id為login_account找到登陸編輯框TextEditor輸入用戶名abc, 根據id為login_pwd找到密碼編輯框TextEditor輸入密碼abc123, 最後找到id為login_button的登陸按鈕,成功登陸

from com.android.monkeyrunner.easy import EasyMonkeyDevice, By
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice

if __name__ == '__main__':
    import codecs
    codecs.register(lambda name: codecs.lookup('utf-8') if name == 'cp65001' else None)

    print ('test start')
    device = MonkeyRunner.waitForConnection()
    easyMonkey = EasyMonkeyDevice(device)

    print ('start login')
    device.shell('am start com.robot.dream/com.robot.dream.LoginActivity')

    MonkeyRunner.sleep(3)

    by = By.id("id/login_account")
    print (easyMonkey.visible(by))
    easyMonkey.type(by,'abc')
    MonkeyRunner.sleep(1)
    device.press('KEYCODE_ENTER')
    MonkeyRunner.sleep(1)
    device.press('KEYCODE_BACK')
    MonkeyRunner.sleep(1)

    by2 = By.id('id/login_pwd')
    print (easyMonkey.visible(by2))
    easyMonkey.type(by2,"abc123")
    MonkeyRunner.sleep(1)
    device.press('KEYCODE_ENTER')
    MonkeyRunner.sleep(1)
    device.press('KEYCODE_BACK')
    MonkeyRunner.sleep(1)

    by3 = By.id('id/login_button')
    print (easyMonkey.visible(by3))
    easyMonkey.touch(by3,MonkeyDevice.DOWN_AND_UP)
    MonkeyRunner.sleep(1)

    device.press('KEYCODE_BACK')


3、總結:

使用monkeyrunner 使用id來查找控件,使用上EasyMonkeyDevice的Touch、Type等幾個基本操作,就可以完成很多基本的功能性測試了。

使用monkeyrunner 的好處,測試程序運行的環境是真實的場景下的環境。

使用monkeyrunner的局限性,無法像單元測試那樣通過Assert來進行功能校驗。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved