Snake's Home

初学monkeyrunner

现在的工作的重心渐渐从Web转到Mobile上来了。根据网上的例子,在公司产品上体验了一把MokeyRunner

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/pyton
#encoding=utf-8
import time
import sys

from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage

now=time.strftime("%Y-%m-%d-%H-%M-%S")
logpath="D:\\log\\"
name=sys.argv[0].split("\\")
filename=name[len(name)-1]

log=open(logpath+filename[0:-3]+"-log"+now+".txt",'w')

device = MonkeyRunner.waitForConnection()
print('connected')
MonkeyRunner.sleep(3)
log.write("连接成功...\n")

device.installPackage('../platform-tools/weather.apk')
print('install')
MonkeyRunner.sleep(3)
log.write("安装成功...\n")

#device.shell('am force-stop com.eztech.kylinlauncher')
device.shell('monkey -p com.XXX.app -v 1')

#device.startActivity(component='com.eztech.kylinlauncher/com.eztech.kylinlauncher.MainActivity')
device.startActivity(component ="com.XXX.app/com.XXX.app.activity.WelcomeActivity")

MonkeyRunner.sleep(10)

device.touch(137,205,'DOWN_AND_UP')
MonkeyRunner.sleep(2)
device.touch(346,200,'DOWN_AND_UP')
MonkeyRunner.sleep(2)
device.touch(186,304,'DOWN_AND_UP')
MonkeyRunner.sleep(2)
device.touch(234,202,'DOWN_AND_UP')
MonkeyRunner.sleep(2)
device.touch(562,205,'DOWN_AND_UP')
MonkeyRunner.sleep(2)
device.touch(27,90,'DOWN_AND_UP')
MonkeyRunner.sleep(2)
device.touch(609,197,'DOWN_AND_UP')
MonkeyRunner.sleep(2)

device.press('KEYCODE_DPAD_UP','DOWN_AND_UP')
MonkeyRunner.sleep(2)
device.press('KEYCODE_DPAD_UP','DOWN_AND_UP')
MonkeyRunner.sleep(2)
device.press('KEYCODE_DPAD_RIGHT','DOWN_AND_UP')
MonkeyRunner.sleep(2)
device.press('KEYCODE_DPAD_RIGHT','DOWN_AND_UP')
MonkeyRunner.sleep(2)
device.press('KEYCODE_DPAD_RIGHT','DOWN_AND_UP')
MonkeyRunner.sleep(2)
device.press('KEYCODE_DPAD_LEFT','DOWN_AND_UP')
MonkeyRunner.sleep(2)
device.press('KEYCODE_DPAD_RIGHT','DOWN_AND_UP')
MonkeyRunner.sleep(5)
device.press("KEYCODE_DPAD_CENTER","DOWN_AND_UP")
MonkeyRunner.sleep(10)
device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')
MonkeyRunner.sleep(3)
device.press('KEYCODE_BACK ','DOWN_AND_UP')
MonkeyRunner.sleep(3)
#点击发送的位置
#device.touch(324,73,'DOWN_AND_UP')
device.touch(388,298,'DOWN_AND_UP')
MonkeyRunner.sleep(10)
print('touch')
log.write("我点!...\n")
#移动第一个便签的位置到最后面去,参数是:一个起始点坐标,一个终点坐标,移动的时间,移动的步骤
#device.drag((232,235),(216,472),3,2)
device.drag((314,248),(314,360),4,3)
MonkeyRunner.sleep(3)
print('drag')
log.write("我拽!...\n")
#截图
result = device.takeSnapshot()
#保存截图
result.writeToFile('./pic0012.png','png')
#取电脑中的需要对比的截图
resultTrue=MonkeyRunner.loadImageFromFile('C:\\SDK\\adt-bundle-windows-x86_64-20140321\\sdk\\tools\\picture\\pic001.png')
log.write("对比截图...\n")
#compare pictures with the same is 90%
#对比截图,看相似度能否达到9成
if(result.sameAs(resultTrue,0.9)):
#print the message
print("compare success")
#write message to log
log.write("对比成功...\n")
else:
#print the failed message
print("compare failed")
log.write("对比失败,图片相似度没有达到90%...\n")

device.removePackage('com.moji.mjweather')
print ('uninstall Success!')
log.write("卸载成功...\n")
#回到主界面
device.press('KEYCODE_HOME ','DOWN_AND_UP')
MonkeyRunner.sleep(3)
print('back to home ')
log.write("结束!...\n")

跑完了以后,感觉这工具不太好用。
1: 取坐标这个是硬伤。兼容性难做到。
2: 靠图片相似度对比,这个太不准了。