您的位置:首页 > 其它

MonkeyRunner_Examples(1)

2014-03-24 16:32 218 查看
# Imports the monkeyrunner modules used by this programfrom com.android.monkeyrunner import MonkeyRunner, MonkeyDevice

# Connects to the current device, returning a MonkeyDevice object
device = MonkeyRunner.waitForConnection()

# Installs the Android package. Notice that this method returns a boolean, so you can test
# to see if the installation worked.
device.installPackage('myproject/bin/MyApplication.apk')

# sets a variable with the package's internal name
package = 'com.example.android.myapplication'

# sets a variable with the name of an Activity in the package
activity = 'com.example.android.myapplication.MainActivity'

# sets the name of the component to start
runComponent = package + '/' + activity

# Runs the component
device.startActivity(component=runComponent)

# Presses the Menu button
device.press('KEYCODE_MENU', MonkeyDevice.DOWN_AND_UP)

# Takes a screenshot
result = device.takeSnapshot()

# Writes the screenshot to a file
result.writeToFile('myproject/shot1.png','png')////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
# 代码文件:contacts.py# 导入要用到的代码库from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
from com.android.monkeyrunner import MonkeyImage
from com.android.monkeyrunner.easy import EasyMonkeyDevice
from com.android.monkeyrunner.easy import HierarchyViewer
from com.android.monkeyrunner.easy import By
from com.android.hierarchyviewerlib.device import ViewNode#初始化 MonkeyDevice,device类型是MonkeyDevicedevice = MonkeyRunner.waitForConnection()#初始化EasyMonkeyDevice,easy_device的类型是EasyMonkeyDevice#在android 2.3.3中,加入了新的API,即EasyMonkeyDevice
easy_device = EasyMonkeyDevice(device)print 'start Contacts'# 启动Contacts 应用# 启动方法使用了adb shell中的am命令,关于am命令的使用,可以在启动adb shell后,键入am回车,里面有# 该命令的帮助文档
device.shell('am start -a android.intent.action.MAIN -n com.android.contacts/.DialtactsContactsEntryActivity')print 'press menu'
print 'check have any'# shell 命令返回后,休眠两秒,等待contacts应用启动起来MonkeyRunner.sleep(2)# 如果联系人为空,就会出现"you don't have any..."的界面,其中显示文字的控件(TextView)的id为"emptyText"# 这里我们检查一下,这个TextView控件是否可见,这个函数是EasyMonkeyDevice类提供的功能
text = easy_device.visible(By.id('id/emptyText')) #这步操作耗时较长,因为按id查找控件需要时间较长
print text #如果可见,将打印出True# 通过MonkeyDevice获取HierarchyViewer对象实例hierarchy_viewer = device.getHierarchyViewer() #通过HierarchyViewer获取ViewNode实例(一个viewnode实例代表一个控件)view_node = hierarchy_viewer.findView(By.id('id/emptyText'))#获取控件内的文本
text = view_node.namedProperties.get('mText').toString()
if text.find('have any') < 0:
print 'not find have any!'
else:
print 'find have any!'print text# 获取当前屏幕的截图new_img = device.takeSnapshot()# 将截图保存到文件
new_img.writeToFile('/home/lstest/test1.png', 'png')# 下面代码实现按菜单进入新建联系人界面print "menu"
device.press('KEYCODE_MENU', 'DOWN_AND_UP')
device.press('KEYCODE_DPAD_UP', 'DOWN_AND_UP')
device.press('KEYCODE_DPAD_UP', 'DOWN_AND_UP')
device.press('KEYCODE_DPAD_RIGHT', 'DOWN_AND_UP')
device.press('KEYCODE_DPAD_RIGHT', 'DOWN_AND_UP')
device.press('KEYCODE_DPAD_CENTER', 'DOWN_AND_UP')
# 下面代码将光标移入First Name文本输入框device.press('KEYCODE_DPAD_UP', 'DOWN_AND_UP')
device.press('KEYCODE_DPAD_UP', 'DOWN_AND_UP')
device.press('KEYCODE_DPAD_UP', 'DOWN_AND_UP')
device.press('KEYCODE_DPAD_UP', 'DOWN_AND_UP')
device.press('KEYCODE_DPAD_DOWN', 'DOWN_AND_UP')
device.type('ls_01') # 输入联系人姓名 "ls_01"MonkeyRunner.sleep(3)#下面代码将光标移入Phone文本输入框
device.press('KEYCODE_DPAD_DOWN', 'DOWN_AND_UP')
device.press('KEYCODE_DPAD_DOWN', 'DOWN_AND_UP')
device.press('KEYCODE_DPAD_RIGHT', 'DOWN_AND_UP')device.type('12345678901') #输入电话号码
easy_device.touch(By.id('id/btn_done'), 'DOWN_AND_UP') #实现点击“Done”按钮功能
MonkeyRunner.sleep(5)#获取截图并保存 (截图如下:)new_img = device.takeSnapshot()
new_img.writeToFile('/home/lstest/test2.png', 'png')#返回Idle界面device.press('KEYCODE_BACK', 'DOWN_AND_UP')
device.press('KEYCODE_BACK', 'DOWN_AND_UP')
#文件结束
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  monkeyrunner