Python_使用python解譯器執行monkey runner指令碼

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.