monkeyrunner实例太少了,自己研究了下,写了个简单的例子,或许对大家有所帮助,该代码在模拟器上没有问题,但是在真机上,有的代码命令不能通过。
首先:导入Notepad源码,然后运行该程序
然后:使用monkeyrunner工具进行测试:
#使用320*480分辨率android屏幕
import sys
from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage
#添加一个新的note
def insertnote(d):
d.startActivity(component="com.example.android.notepad/.NotesList")
print "insert a new note"
MonkeyRunner.sleep(2)
d.press("KEYCODE_MENU",'DOWN')
MonkeyRunner.sleep(2)
d.touch(58,430,“”)
MonkeyRunner.sleep(2)
d.type("hello")
d.press("KEYCODE_BACK",'DOWN')
d.press("KEYCODE_HOME",'DOWN')
print "insert Successfully"
MonkeyRunner.sleep(5)
#对之前添加的note做更改
def updatenote(d):
d.startActivity(component="com.example.android.notepad/.NotesList")
print "update the note"
MonkeyRunner.sleep(2)
result = d.takeSnapshot()
result.writeToFile('noteslist.png','png')
d.touch(58,100," ")
MonkeyRunner.sleep(2)
result = d.takeSnapshot()
result.writeToFile('opennote.png','png')
d.press("KEYCODE_MENU",'DOWN')
MonkeyRunner.sleep(2)
result = d.takeSnapshot()
result.writeToFile('pressmenu.png','png')
d.touch(300,430," ")
MonkeyRunner.sleep(3)
result = d.takeSnapshot()
result.writeToFile('edittitle.png','png')
y=250
x1=300
x2=50
duration=1
steps=10
start=(x1,y)
end=(x2,y)
d.drag(start,end,duration,steps)
result = d.takeSnapshot()
result.writeToFile('cut.png','png')
d.touch(100,265," ")
MonkeyRunner.sleep(2)
d.type("note1")
MonkeyRunner.sleep(2)
result = d.takeSnapshot()
result.writeToFile('printnewname.png','png')
d.touch(285,305," ")
MonkeyRunner.sleep(2)
d.type("world")
MonkeyRunner.sleep(1)
d.press('KEYCODE_BACK','DOWN')
d.press("KEYCODE_HOME",'DOWN')
MonkeyRunner.sleep(5)
#删除该条note
def deletenote(d):
d.startActivity(component="com.example.android.notepad/.NotesList")
print "delete the note"
MonkeyRunner.sleep(2)
d.touch(58,100," ")
MonkeyRunner.sleep(2)
d.press("KEYCODE_MENU",'DOWN_AND_UP"')
MonkeyRunner.sleep(2)
d.touch(160,430)
d.press("KEYCODE_HOME",'DOWN_AND_UP')
def main():
print "Start"
device = MonkeyRunner.waitForConnection()
if not device:
print "Couldn't get connection"
sys.exit()
print "Found device"
insertnote(device)
updatenote(device)
deletenote(device)
if __name__ == '__main__':
main()