说实在话,本人有点懒,不想每次都在cmd下切换到android sdk的tools目录下执行monkey runner脚本,也比较讨厌在tools 目录下创建monkey runner脚步,我想的还是能不能在eclipse下执行monkey runner脚本呢?
一个偶然的机会我发现python 的os 跟subprocess模块下可以执行一些shell命令,那能不能实现打开cmd然后切换到android sdk的tools目录下执行monkey runner脚本呢?
带着这个想法我进行了一次次的尝试,终于能实现了;
执行monkey runner脚本的脚本为:
import subprocess
import os
def command_line():
#连续执行两个命令,切换目录到android sdk 的tools目录下,然后用monkey runner执行测试脚本
cmd_path=' cd /d "E:\\xxxx\\Android\\androidSDKwindows\\tools" & monkeyrunner F:\\xxxx\\AndroidAutoTest\\notepad\\notepad_test.py'
os.system(cmd_path)
command_line()
notedpad_test.py脚本仅实现安装与卸载应用的功能
import sys
# import the MonkeyRunners modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
def installApp(device):
device.installPackage('F:/xxxx/AndroidAutoTest/apps/NotesList.apk')
def call(d):
d.startActivity(component="com.example.android.notepad/.NotesList")
print "Start Activity"
MonkeyRunner.sleep(1.0)
def removeApp(device):
device.removePackage ('com.example.android.notepad')
print "remove success!!!"
def main():
print "Start"
# Connect to the current device returning the MonkeyDevice object
device = MonkeyRunner.waitForConnection()
#MonkeyRunner.alert("Starting Activity", "monkeyrunner", "OK")
if not device:
print "Couldn't get connection"
sys.exit()
print "Found device"
installApp(device)
call(device)
MonkeyRunner.sleep(10.0)
removeApp(device)
if __name__ == '__main__':
main()
文章写于2014-10-18 21:30