今天又重新看了一下以前寫的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()