zoukankan      html  css  js  c++  java
  • 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 == '
    ':
    18                 continue
    19             result_list.append(i.strip('
    '))#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')
  • 相关阅读:
    编译原理学习导论-作者四川大学唐良(转)初学者必看
    Ajax在chrome浏览器中测试调用失败解决办法
    sublime中输入法输入框只能在一个位置
    第三次作业
    第二次作业
    第一次作业
    2018年 大一下学期第零次作业
    14,15周作业
    第七周作业
    第六周作业
  • 原文地址:https://www.cnblogs.com/loveapple/p/9141519.html
Copyright © 2011-2022 走看看