目的:設計一個指令碼,自動執行monkey命令,並實現日誌自動儲存。
思路:readlines讀取adb devices所有裝置內容
index找到裝置名稱的分割線
唯讀取index之前的內容到一個新列表裡(可以用兩列表元素賦值,也可以用append)
通過選擇裝置號,列印相應的裝置名稱
引入os來實現互動
下面是核心代碼:
讀取裝置:
temp=os.popen('adb devices').readlines() n=len(temp)-2
for i in range(n): point=temp[i+1].index('\t') ds.append(temp[i+1][:point]) print str(i+1)+'-'+ds[i] print ''
迴圈執行monkey命令
fd.write(':loop\n') fd.write('adb -s ') fd.write(dev) fd.write(' shell monkey -p com.x.xx --monitor-native-crashes --pct-touch 80 --pct-motion 15 --pct-nav 5 -s %random% -v --throttle 500 100000>d:\\') fd.write(fn) fd.write('\\%random%.log\n') time.sleep(5) fd.write('@goto loop')
全部代碼如下:
#!/usr/bin/python# -*- coding: utf-8 -*-#coding=gbkimport osimport os.pathimport timeds=[]temp=os.popen('adb devices').readlines()n=len(temp)-2#print tempprint 'the number of devices is ' +str(n)for i in range(n):point=temp[i+1].index('\t')ds.append(temp[i+1][:point])print str(i+1)+'-'+ds[i]print ''nds=1if n!=1:nds=input('select device:')if nds<=n:dev=ds[nds-1]fn=raw_input('please input file name:')fd=open(fn+'.cmd','w')if not os.path.isdir('D:\\'+fn):os.mkdir('D:\\'+fn)fd.write(':loop\n')fd.write('adb -s ')fd.write(dev)fd.write(' shell monkey -p com.sky.jisuanji --monitor-native-crashes --pct-touch 80 --pct-motion 15 --pct-nav 5 -s %random% -v --throttle 500 10>d:\\')fd.write(fn)fd.write('\\%random%.log\n')time.sleep(5)fd.write('@goto loop')fd.close()