1. 程式人生 > >Android 自動化測試(3) 根據ID查詢物件&touch&type (python)

Android 自動化測試(3) 根據ID查詢物件&touch&type (python)

    我在之前的兩篇文章中用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來進行功能校驗。

4、後面會介紹些 Android monkeyrunner &Android UI Test &Android Code Coverage Test