寶安商城網(wǎng)站建設(shè)哪家便宜百度搜索引擎平臺(tái)
??首先把安卓SDK的 環(huán)境變量給配置好,這里就不再多解釋,自己google
然后將自己的安卓設(shè)備打開(kāi)調(diào)試模式,USB連接至電腦,運(yùn)行CMD,輸入命令adb devices
查看你的安卓設(shè)備的ID(ID后面寫(xiě)程序會(huì)調(diào)用),如下圖所示:
繼續(xù)在這個(gè)命令提示框中輸入 monkeyrunner,進(jìn)入monkeyrunner的shell命令交互模式。如圖:
進(jìn)入shell命令交互模式后,首要一件事就是導(dǎo)入monkeyrunner所要使用的模塊。直接在shell命令下輸入:
from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice?回車(chē)
這2步完成就可以就行monkeyrunner的測(cè)試了。
下面的一個(gè)例子是連接手機(jī)并且點(diǎn)擊菜單按鈕,之后截圖保存(注:以下命令全部在CMD monkeyrunner環(huán)境下運(yùn)行)
第一:
# 連接安卓設(shè)備,其中4d007e9a1b0050d1 是設(shè)備的ID 上面有提到通過(guò)abd devices 獲得
device = MonkeyRunner.waitForConnection(5,'4d007e9a1b0050d1')
第二:
#安裝apk文件到這個(gè)設(shè)備
device.installPackage('E:\Education - 136_8080.apk')
安裝成功以后會(huì)返回true
第三:
# sets a variable with the package's internal name(設(shè)置包名)
package = 'com.incito.android.education'
第四:
# sets a variable with the name of an Activity in the package(設(shè)置activity)
activity = 'com.incito.android.education.MainActivity'
第五:
# sets the name of the component to start(設(shè)置運(yùn)行組件)
runComponent = package + '/' + activity
第六:
# Runs the component(運(yùn)行此組件)
device.startActivity(component=runComponent)
第七:
# Presses the Menu button(按下菜單按鈕,此時(shí)真機(jī)的菜單按鈕會(huì)被按下)
device.press('KEYCODE_MENU', MonkeyDevice.DOWN_AND_UP)
第八:
# Takes a screenshot(觸發(fā)截圖)
result = device.takeSnapshot()
第九:
# Writes the screenshot to a file(保存截圖)
result.writeToFile('E:\Snapshot\edu.png','png')
截圖成功會(huì)返回true,如圖
注意保存截圖的文件夾要變成可以狀態(tài)才能截圖成功不然會(huì)報(bào)異常:只讀去掉?
截圖成果:
圖:
所有的語(yǔ)句可以寫(xiě)在一個(gè)python文件里面然后用命令monkeyrunner test.py 即可