测试:python调用cmd命令,让push包自动化

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了测试:python调用cmd命令,让push包自动化相关的知识,希望对你有一定的参考价值。

一、python与monkey script脚本相结合,达到修改.script脚本中的内容

 1 #coding:utf-8
 2 ‘‘‘
 3 push脚本进室内机,并运行
 4 
 5 ‘‘‘
 6 import os
 7 import re
 8 import subprocess
 9 
10 #执行脚本
11 def run_monkey(path):
12     filelist=os.listdir(path)
13     for filename in filelist:
14         filepath=os.path.join(path,filename)
15         cmd="adb remount"
16         p=subprocess.Popen(cmd,shell=True)
17         p.wait()
18         if p.returncode!=0:
19             return -1
20         cmd="adb push "+filepath+" sdcard/"
21         p=subprocess.Popen(cmd,shell=True)
22         p.wait()
23         if p.returncode!=0:
24             return -1
25         tarpath=os.path.join(r"sdcard/",filename)
26         cmd="adb shell monkey -f "+tarpath+" -v 1"
27         p=subprocess.Popen(cmd,shell=True)
28         p.wait()
29         if p.returncode!=0:
30             return -1
31 
32 #修改脚本的内容
33 def modify_monkey(path,repstr):
34     filelist=os.listdir(path)
35     for filename in filelist:
36         filepath=os.path.join(path,filename)
37         contect=""
38         with open(filepath,encoding=utf-8) as file:
39             text=file.read()
40             pattern=re.compile(r"captureDispatchString(.*)")
41             repstr="captureDispatchString({})".format(repstr)
42             contect=re.sub(pattern,repstr,text)
43         with open(filepath,mode=w,encoding=utf-8) as file:
44             file.write(contect)     
45 if __name__=="__main__":
46     path=r"C:\Users\Administrator\Desktop\pythoncmd\runscript"
47     times=500
48     for i in range(times):
49         modify_monkey(path,str(i))
50         run_monkey(path) 
51     print("执行完毕")   
52         

二、push .apk和.so文件自动化

 1 #coding:utf-8
 2 ‘‘‘
 3 push脚本进室内机,并运行
 4 
 5 ‘‘‘
 6 import os
 7 import re
 8 import subprocess
 9 
10 #执行脚本
11 def push_apk(path):
12     filelist=os.listdir(path)
13     cmd="adb remount"
14     p=subprocess.Popen(cmd,shell=True)
15     p.wait()
16     if p.returncode!=0:
17         return -1
18     for filename in filelist:
19         filepath=os.path.join(path,filename)
20         filename,extension=os.path.splitext(filename)
21         if extension==".apk":
22             #print(".apk")
23             cmd="adb push "+filepath+" /system/app"
24             p=subprocess.Popen(cmd,shell=True)
25             p.wait()
26             if p.returncode!=0:
27                 return -1
28         if extension==".so":
29             #print(".so")
30             cmd="adb push "+filepath+" /system/lib"
31             p=subprocess.Popen(cmd,shell=True)
32             p.wait()
33             if p.returncode!=0:
34                 return -1        
35     cmd="adb reboot"
36     p=subprocess.Popen(cmd,shell=True)
37     p.wait()
38     if p.returncode!=0:
39         return -1    
40 
41 if __name__=="__main__":
42     path=r"C:\Users\Administrator\Desktop\pythoncmd\pushapk"
43     push_apk(path)
44     print("执行完毕,正在重启")   
45         

 

以上是关于测试:python调用cmd命令,让push包自动化的主要内容,如果未能解决你的问题,请参考以下文章

测试:python调用cmd命令三种方法

appium+python自动化51-adb文件导入和导出(pull push)

解决python在cmd运行时导入包失败,出现错误信息 "ModuleNotFoundError: No module named ***"

搭建自动化测试环境

Go执行命令行操作

python3调用cmd执行命令报乱码问题