appium自动化测试框架——在python脚本中执行dos命令

一般我们运行dos命令,会有两种需求,一种是需要收集执行结果,如ip、device等;一种是不需要收集结果,如杀死或开启某个服务。

对应的在python中就要封装两种方法,来分别实现这两种需求。

1、引入包

import os

2、只执行不收集结果

os.system(command)

3、执行并手机结果

os.popen(command).readlines()

4、代码实现

 1 #coding=utf-8
 2 import os
 3 
 4 class DosCmd:
 5     '''
 6     用来封装windows执行dos命令,分两种,一种是收集执行结果,一种是不需要收集执行结果
 7     '''
 8     def excute_cmd_result(self,command):
 9         '''
10         执行command命令,并返回执行结果
11         :param command: 传入要执行的命令,字符串格式
12         :return:返回执行结果,列表格式
13         '''
14         result_list = []
15         result = os.popen(command).readlines()
16         for i in result:
17             if i == '\n':
18                 continue
19             result_list.append(i.strip('\n'))#strip() 方法用于移除字符串头尾指定的字符
20         return result_list
21 
22 
23     def excute_cmd(self,command):
24         '''
25         仅执行command命令,不收集执行结果
26         :param command: 传入要执行的命令,字符串格式
27         '''
28         os.system(command)
29 
30 if __name__=="__main__":
31     dos = DosCmd()
32     print dos.excute_cmd_result('adb devices')
33     dos.excute_cmd('adb devices')

猜你喜欢

转载自www.cnblogs.com/loveapple/p/9141519.html