MonkeyRunner實戰-自動化操做計算器計算

前面幾張詳細的講了MonkeyRunner的模塊,包括MonkeyRunner、MonkeyDevice、MonkeyImage,也包括easyMonkeyDevice,還有getHierarchyViewer這樣的一些方法。python

這一章具體的使用python來編寫自動化測試的腳本,利用MonkeyRunner完成自動化測試。android

 Case1git

用press、touch方法分別觸發計算器按鍵,並用MonkeyImage比較兩次計算結果是否一致測試

python腳本的編寫以下:ui

#第一次運算press命令行

from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage   #導入模塊code

from com.android.monkeyrunner.easy import EasyMonkeyDevice,By     #導入方法對象

device = MonkeyRunner.waitForConnection()     #創建和設備的鏈接blog

print '******Case1:Use MonkeyDevice and MonkeyImage to check calculator result******' #輸出get

print '---- start calculator App'

device.startActivity('com.android.calculator2/.Calculator') #啓動應用

print '---- calculator 3*8 with press method'

device.press('KEYCODE_3',MonkeyDevice.DOWN_AND_UP)

device.press('KEYCODE_NUMPAD_MULTIPLY',MonkeyDevice.DOWN_AND_UP)

device.press('KEYCODE_8',MonkeyDevice.DOWN_AND_UP)

device.press('KEYCODE_EQUALS',MonkeyDevice.DOWN_AND_UP)

easy = EasyMonkeyDevice(device)

image = device.takeSnapshot() #把當前的界面保存到MonkeyImage對象裏

subimage = image.getSubImage(easy.locate(By.id('id/display')))

#打開uiautomatorviewer查看保存結果文本框的座標範圍[0,75][912,426],因爲結果只在區域有半部分,因此座標能夠變爲[300,75][912,426]

subimage = image.getSubImage((300,75,612,351)) #這樣子就保存了第一次運算的結果

#第二次運算touch

print '---- calculator 4*6 with touch method'

easy.touch(By.id('id/digit4'),MonkeyDevice.DOWN_AND_UP)

easy.touch(By.id('id/mul'),MonkeyDevice.DOWN_AND_UP)

easy.touch(By.id('id/digit6'),MonkeyDevice.DOWN_AND_UP)

easy.touch(By.id('id/equal'),MonkeyDevice.DOWN_AND_UP)

image2 = device.takeSnapshot()

subimage2 = image2.getSubImage(easy.locate(By.id('id/display')))

if (subimage2.sameAs(subimage,0.8)):

    print '[Pass] the resault of 3*8 and 4*6 is equal !'

else:

    print '[Fail] the resault of 3*8 and 4*6 is not equal !'

把以上的腳本編寫好命名爲calculator_mr.py保存到C:\Users\hou-00下面,而後把腳本拖到命令行執行

Case2

用EasyMonkeyDevice來獲取按鍵並觸發,再用HierarchyViewer獲取對象屬性校驗結果正確性

 

from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage  

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

device = MonkeyRunner.waitForConnection()     

print '******Case2: Use EasyMonkeyDevice to check claculator result******' 

print '---- calculator 5*7 with EasyMonkeyDevice touch'

easy = EasyMonkeyDevice(device)

easy.touch(By.id('id/digit5'),MonkeyDevice.DOWN_AND_UP)

easy.touch(By.id('id/mul'),MonkeyDevice.DOWN_AND_UP)

easy.touch(By.id('id/digit7'),MonkeyDevice.DOWN_AND_UP)

easy.touch(By.id('id/equal'),MonkeyDevice.DOWN_AND_UP)

hv=device.getHierarchyViewer()

view = hv.findViewById('id/display')

str =view.children[0].namedProperties.get('text:mText').toString().split('=')[1].encode('utf8')

if (str == '35'):

    print '[PASS] the result of 5*7 is correct!'

else:

    print '[Fail] the result of 5*7 is correct! the result is -- ' +str

 

easy.touch(By.id('id/clear'),MonkeyDevice.DOWN_AND_UP)

device.press('KEYCODE_BACK',MonkeyDevice.DOWN_AND_UP)

相關文章
相關標籤/搜索