Android MonkeyRunner测试NotePad例子(SDK中的源码实例)

本文提供了一个MonkeyRunner自动化测试的简单示例,演示如何在Android应用中创建、更新及删除便签。代码适用于模拟器,但部分命令在真实设备上可能无法执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值