今天又重新看了一下以前写的monkey runner脚本,参考了一下monkey runner的官方文档,说可以在monkey runner中使用subprocess,不过我在monkey runner脚本中输入import subprocess会提示我出现错误,错误的原因是因为subprocess需要python的 interpreter,而不是monkey runner 的interpreter,于是我用python 执行monkey runner脚本。
1. python代码如下:
Ubuntu:
import subprocess
subprocess.Popen("/android-sdk-linux_x86/tools/monkeyrunner /AutoTesting/zhekou/startApp.py", shell=True)
2. startApp.py为monkeyrunner脚本,这样就可以通过python 执行monkey runner了,很不错吧。
Windows:
import subprocess
subprocess.Popen("E:/Androids/tools/monkeyrunner F:\AutoTesting\src\zhe\zhe_start_app.py",shell=True)
把monkeyruner加入path则执行为:
subprocess.Popen("monkeyrunner F:\AutoTesting\src\zhe\zhe_start_app.py",shell=True)
可能会出现如下错误:
Can't open specified script file
Usage: monkeyrunner [options] SCRIPT_FILE
-s MonkeyServer IP Address.
-p MonkeyServer TCP Port.
-v MonkeyServer Logging level (ALL, FINEST, FINER, FINE, CONFIG, INFO, WARNING, SEVERE, OFF)
解决方法为:
subprocess.Popen("monkeyrunner F:/AutoTesting/src/zhe/zhe_start_app.py",shell=True)
3. 实例代码:
import sys
from com.android.monkeyrunner import MonkeyRunner as mr,MonkeyDevice as md
def startApp(d):
pack="com.aa.bb"
act=".SplashActivity"
runCom=pack+"/"+act
d.startActivity(component=runCom)
mr.sleep(5)
def screenShot(d):
result=d.takeSnapshot()
result.writeToFile('\src\aa\aa_image\shot1.png','png') #设置图片存储的绝对路径
print 'Over'
def main():
device=mr.waitForConnection()
if not device:
print "Device is not found!"
sys.exit()
print "Device is connected!"
startApp(device)
screenShot(device)
if __name__=='__main__':
main()
4.启动新浪微博:
import sys
from com.android.monkeyrunner import MonkeyRunner as mr,MonkeyDevice as md
def startApp(d):
pack="com.sina.weibo"
act=".SplashActivity"
runCom=pack+"/"+act
d.startActivity(component=runCom)
mr.sleep(5)
def screenShot(d):
result=d.takeSnapshot()
result.writeToFile('F:\mywork\AutoTesting\src\sina_weibo\sina_weibo_image\shot1.png','png')
print 'Image is saved!'
#进行图片比较
def execCommand(d):
print "Start execute command"
command="python compare_image.py"
#command="ls -l"
os.system(command)
def main():
device=mr.waitForConnection()
if not device:
print "Device is not found!"
sys.exit()
print "Device is connected!"
startApp(device)
screenShot(device)
execCommand(device)
if __name__=='__main__':
main()