首先文件所在目录:

安卓自动化测试案例(跑在MonkeyRunner上)_安卓

MonkeyRunner所在目录:

安卓自动化测试案例(跑在MonkeyRunner上)_安卓_02

 

运行命令(通过cd 命令  进入Tools目录下):

安卓自动化测试案例(跑在MonkeyRunner上)_安卓_03

运行脚本:monkeyrunner.bat ..\honeywell\jsq.py

 

源文件代码为:

# coding=utf-8

import sys
from com.android.monkeyrunner import MonkeyRunner as mr,MonkeyDevice
from com.android.monkeyrunner.easy import EasyMonkeyDevice,By
from com.android.chimpchat.hierarchyviewer import HierarchyViewer

#连接设备
device = mr.waitForConnection(30,"emulator-5554")
if not device:
     print >> sys.stderr,"fail"
     sys.exit(1)
#连接设备完成
mr.sleep(2.0)
#打开主屏幕-根据ResourceId
#

#
upArrow="id/all_apps_handle"
easy_device = EasyMonkeyDevice(device)
easy_device.touch(By.id(upArrow), MonkeyDevice.DOWN_AND_UP)
mr.sleep(2.0)
#启动Activity 打开拨号键
activityName="com.android.dialer/.DialtactsActivity"
device.startActivity(component = activityName)
#等待时间,防止操作太快,设备反应不及时
mr.sleep(2.0)

#点击添加拨号动作
action_button="id/floating_action_button"

easy_device.touch(By.id(action_button), MonkeyDevice.DOWN_AND_UP)
mr.sleep(2.0)
#开始拨号
button_1="id/one"
button_2="id/two"
button_3="id/three"
button_4="id/four"
button_5="id/five"
button_6="id/six"
button_7="id/seven"
button_8="id/eight"
button_9="id/nine"
button_0="id/zero"
easy_device.touch(By.id(button_1), MonkeyDevice.DOWN_AND_UP)

easy_device.touch(By.id(button_8), MonkeyDevice.DOWN_AND_UP)

easy_device.touch(By.id(button_9), MonkeyDevice.DOWN_AND_UP)

easy_device.touch(By.id(button_1), MonkeyDevice.DOWN_AND_UP)

easy_device.touch(By.id(button_1), MonkeyDevice.DOWN_AND_UP)

easy_device.touch(By.id(button_6), MonkeyDevice.DOWN_AND_UP)

easy_device.touch(By.id(button_9), MonkeyDevice.DOWN_AND_UP)

easy_device.touch(By.id(button_5), MonkeyDevice.DOWN_AND_UP)

easy_device.touch(By.id(button_0), MonkeyDevice.DOWN_AND_UP)

easy_device.touch(By.id(button_8), MonkeyDevice.DOWN_AND_UP)

easy_device.touch(By.id(button_7), MonkeyDevice.DOWN_AND_UP)
mr.sleep(2.0)
#添加联系人
createNewId="id/cliv_name_textview"
easy_device.touch(By.id(createNewId), MonkeyDevice.DOWN_AND_UP)
mr.sleep(2.0)
#姓名输入  获取焦点
device.touch(300,1060,'DOWN_AND_UP')
mr.sleep(1.0)
#输入
device.type('JackChen')
mr.sleep(2.0)
#关闭键盘
device.press('KEYCODE_BACK',MonkeyDevice.DOWN_AND_UP)
#保存联系人
mr.sleep(2.0)
menu_save="id/menu_save"
easy_device.touch(By.id(menu_save), MonkeyDevice.DOWN_AND_UP)
mr.sleep(2.0)
device.press('KEYCODE_HOME',MonkeyDevice.DOWN_AND_UP)
mr.sleep(2.0)
componentName='com.google.android.apps.messaging/.ui.ConversationListActivity'
#启动特定的Activity
device.startActivity(component = componentName)
mr.sleep(2.0)
#新建信息
messageAdd="id/start_new_conversation_button"
easy_device.touch(By.id(messageAdd), MonkeyDevice.DOWN_AND_UP)
mr.sleep(2.0)
numText="id/recipient_text_view"
easy_device.touch(By.id(numText), MonkeyDevice.DOWN_AND_UP)
mr.sleep(1.0)

device.type('18911695087')
mr.sleep(3.0)
#点击10086
device.touch(301,300,'DOWN_AND_UP')
mr.sleep(2.0)
device.press('KEYCODE_BACK',MonkeyDevice.DOWN_AND_UP)
mr.sleep(1.0)
messageTxt="id/compose_message_text"
easy_device.touch(By.id(messageTxt), MonkeyDevice.DOWN_AND_UP)
mr.sleep(1.0)
device.type('Hello,jack')

#等待时间,防止操作太快,设备反应不及时
mr.sleep(1.0)
btn_send="id/self_send_icon"
easy_device.touch(By.id(btn_send), MonkeyDevice.DOWN_AND_UP)

mr.sleep(3.0)
conversationImg = device.takeSnapshot()
conversationImg.writeToFile('d:\\SendMessage.png','png');