• 《自拍教程43》Python adb一键读取硬件配置


    Android系统测试过程中,
    比如接口测试,需要用到设备的iccid, 或者uuid,
    车载测试需要用到VIN号(车辆唯一标识号),
    4G测试等需要设计IMEI号等设备配置字参数等,
    我们还可以读取到设备的分辨率,emmc磁盘容量,
    cpu型号,内存大小等,这些硬件配置信息,
    可以辅助我们做测试,如何读取到这些硬件信息呢?


    准备阶段
    1. adb shell getprop命令,可以获取到Android设备iccid, imei,emmc磁盘容量等
    2. adb shell wm size 可以读取到分辨率
    3. adb shell cat /proc/cpuinfo 可以读取到cpu相关的信息
    4. adb shell cat /proc/meminfo 或adb shell free 可以读取到内存相关的信息
    5. os.popen()函数可以快速调用以上命令并获取输出字符串。
    6. python的re正则表达式可以轻易地进行数据匹配,查找等强大的字符串处理

    getprop命令介绍

    getprop命令是android自带的动态获取系统属性参数,
    包括了软件,硬件属性参数等,其有好几种执行方式,包括:
    用法一: getprop 获取全部系统属性参数,
    比如魅族Note5的getprop我们保存为txt文件(可点击下载)

    用法二: getprop 属性名,可获取某个指定属性的具体参数信息,比如:

    相对应的,也有setprop命令是设置某个属性的具体参数的,
    测试一般不建议随意设置属性参数。

    Python批处理脚本形式
    # coding=utf-8
    
    import os
    import re
    
    # 获取iccid
    try:
        iccid_1 = os.popen("adb shell getprop persist.radio.cfu.iccid.0").read().strip()
        iccid_2 = os.popen("adb shell getprop persist.radio.cfu.iccid.1").read().strip()
    except:
        iccid_1 = "Unknown"
        iccid_2 = "Unknown"
    print("iccid_1: %s" % iccid_1)
    print("iccid_2: %s" % iccid_2)
    
    # 获取imei
    try:
        imei = os.popen("adb shell getprop ril.gsm.imei").read().strip().split(",")
    except:
        imei = ["Unknown", "Unknown"]
    print("IMEI_1: %s" % imei[0])
    print("IMEI_2: %s" % imei[1])
    
    # 获取EMMC磁盘容量
    try:
        emmc_size = os.popen("adb shell getprop vold.emmc_size").read().strip()
        emmc_size = str(int(int(emmc_size) / 1024 / 1024 / 1024))
    except Exception as e:
        print(e)
        emmc_size = "Unknown"
    print("EMMC_Size: %s GB" % emmc)
    
    # 获取Android设备的内存容量大小
    ram_size = os.popen("adb shell cat /proc/meminfo").read()
    try:
        ram_size = re.findall(r"MemTotal:(.*)", ram_size)[0].strip()
        ram_size = re.sub("kB", "", ram_size)
        ram_size = str(round(int(ram_size) / 1024 / 1024))
    except Exception as e:
        print(e)
        ram_size = "Unknown"
    print("RAM Size: %s GB" % ram_size)
    
    # 获取Android设备的CPU型号
    cpu_profile = os.popen("adb shell cat /proc/cpuinfo").read()
    try:
        cpu_profile = re.findall(r"Hardwares+:(.*)", cpu_profile)[0].strip()
    except:
        cpu_profile = "Unknown"
    print("CPU Profile: %s" % cpu_profile)
    
    # 获取Android设备的分辨率
    resolution = os.popen("adb shell wm size").read()
    resolution = resolution.split(":")[1].strip()
    print("Resolution: %s" % resolution)
    
    os.system("pause")
    

    try ...
    except Exception as e 是用来做调试用的,
    即当出现常规异常的时候,print(e)显示报错内容,
    且当出现异常时,相应的硬件信息将赋值为“Unknown”,
    有利于提高程序的健壮性!

    Python面向过程函数形式

    本次进行改进:

    1. os.popen("命令").read()用的太多了,我们需要考虑封装成一个函数。
    2. 获取各类硬件设备配置的语句块,分别独立做成一个个的函数。
    # coding=utf-8
    
    import os
    import re
    
    
    # 独立一个get_popen执行命令并获取其输出字符串
    def get_popen(command):
        p_str = os.popen("%s" % command).read()
        return p_str.strip()
    
    
    def get_iccid():  # 获取iccid
        try:
            iccid_1 = get_popen("adb shell getprop persist.radio.cfu.iccid.0")
            iccid_2 = get_popen("adb shell getprop persist.radio.cfu.iccid.1")
        except:
            iccid_1 = "Unknown"
            iccid_2 = "Unknown"
        return iccid_1, iccid_2
    
    
    def get_imei():  # 获取imei
        try:
            imei = get_popen("adb shell getprop ril.gsm.imei").split(",")
        except:
            imei = ["Unknown", "Unknown"]
        return imei
    
    
    def get_emmc_size():  # 获取EMMC磁盘容量
        try:
            emmc_size =get_popen("adb shell getprop vold.emmc_size")
            emmc_size = str(int(int(emmc_size) / 1024 / 1024 / 1024))
        except:
            emmc_size = "Unknown"
        return emmc_size
    
    
    def get_ram_size():  # 获取Android设备的内存容量大小
        ram_size = get_popen("adb shell cat /proc/meminfo")
        try:
            ram_size = re.findall(r"MemTotal:(.*)", ram_size)[0].strip()
            ram_size = re.sub("kB", "", ram_size)
            ram_size = str(round(int(ram_size) / 1024 / 1024))
        except:
            ram_size = "Unknown"
        return ram_size
    
    
    def get_cpu_profile():  # 获取Android设备的CPU型号
        cpu_profile = get_popen("adb shell cat /proc/cpuinfo")
        try:
            cpu_profile = re.findall(r"Hardwares+:(.*)", cpu_profile)[0].strip()
        except:
            cpu_profile = "Unknown"
        return cpu_profile
    
    
    def get_resolution():  # 获取Android设备的分辨率
        resolution = get_popen("adb shell wm size")
        resolution = resolution.split(":")[1].strip()
        return resolution
    
    
    iccid_1, iccid_2 = get_iccid()
    imei = get_imei()
    emmc_size = get_emmc_size()
    ram_size = get_ram_size()
    cpu_profile = get_cpu_profile()
    resolution = get_resolution()
    print("iccid_1: %s" % iccid_1)
    print("iccid_2: %s" % iccid_2)
    print("IMEI_1: %s" % imei[0])
    print("IMEI_2: %s" % imei[1])
    print("EMMC_Size: %s GB" % emmc_size)
    print("RAM Size: %s GB" % ram_size)
    print("CPU Profile: %s" % cpu_profile)
    print("Resolution: %s" % resolution)
    
    os.system("pause")
    

    面向对象类的形式
    1. 以"万物皆可归类"的思想, 先抽象化出一个类来,
      类名一般建议用"名词", 所以我们命名为"HWInfoGetter",
      代表硬件信息获取器, 且一般要驼峰式(首字母大写)来规范类的命名。
    2. 养成良好的类的初始化(__init__)的习惯,
      初始化过程中, 可以初始化7个属性(变量),类范围内可随意调用
    3. 细化各不同硬件信息获取的函数
    4. 类是一个抽象的事物, 必须实例化成具体的对象后,
      才能进行调用, 所以我们实例化并命名成了h_obj, 表明是一个对象。
    5. 实例化成具体对象后, 对象就可以调用这个类里边的各个函数了
    # coding=utf-8
    
    import os
    import re
    
    
    # 独立一个get_popen执行命令并获取其输出字符串
    def get_popen(command):
        p_str = os.popen("%s" % command).read()
        return p_str.strip()
    
    
    class HWInfoGetter():
        def __init__(self):
            self.iccid_1 = None
            self.iccid_2 = None
            self.imei = None
            self.emmc_size = None
            self.ram_size = None
            self.cpu_profile = None
            self.resolution = None
    
        def get_iccid(self):  # 获取iccid
            try:
                self.iccid_1 = get_popen("adb shell getprop persist.radio.cfu.iccid.0")
                self.iccid_2 = get_popen("adb shell getprop persist.radio.cfu.iccid.1")
            except:
                self.iccid_1 = "Unknown"
                self.iccid_2 = "Unknown"
            return self.iccid_1, self.iccid_2
    
        def get_imei(self):  # 获取imei
            try:
                self.imei = get_popen("adb shell getprop ril.gsm.imei").split(",")
            except:
                self.imei = ["Unknown", "Unknown"]
            return self.imei
    
        def get_emmc_size(self):  # 获取EMMC磁盘容量
            try:
                self.emmc_size = get_popen("adb shell getprop vold.emmc_size")
                self.emmc_size = str(int(int(emmc_size) / 1024 / 1024 / 1024))
            except:
                self.emmc_size = "Unknown"
            return self.emmc_size
    
        def get_ram_size(self):  # 获取Android设备的内存容量大小
            self.ram_size = get_popen("adb shell cat /proc/meminfo")
            try:
                self.ram_size = re.findall(r"MemTotal:(.*)", self.ram_size)[0].strip()
                self.ram_size = re.sub("kB", "", self.ram_size)
                self.ram_size = str(round(int(self.ram_size) / 1024 / 1024))
            except:
                self.ram_size = "Unknown"
            return self.ram_size
    
        def get_cpu_profile(self):  # 获取Android设备的CPU型号
            cpu_profile = get_popen("adb shell cat /proc/cpuinfo")
            try:
                self.cpu_profile = re.findall(r"Hardwares+:(.*)", cpu_profile)[0].strip()
            except:
                self.cpu_profile = "Unknown"
            return self.cpu_profile
    
        def get_resolution(self):  # 获取Android设备的分辨率
            self.resolution = get_popen("adb shell wm size")
            self.resolution = self.resolution.split(":")[1].strip()
            return self.resolution
    
    
    if __name__ == '__main__':
        h_obj = HWInfoGetter()
        iccid_1, iccid_2 = h_obj.get_iccid()
        imei = h_obj.get_imei()
        emmc_size = h_obj.get_emmc_size()
        ram_size = h_obj.get_ram_size()
        cpu_profile = h_obj.get_cpu_profile()
        resolution = h_obj.get_resolution()
        print("iccid_1: %s" % iccid_1)
        print("iccid_2: %s" % iccid_2)
        print("IMEI_1: %s" % imei[0])
        print("IMEI_2: %s" % imei[1])
        print("EMMC_Size: %s GB" % emmc_size)
        print("RAM Size: %s GB" % ram_size)
        print("CPU Profile: %s" % cpu_profile)
        print("Resolution: %s" % resolution)
        os.system("pause")
    

    代码运行方式

    确保Android车机设备通过USB线与电脑连接了,adb设备有效连接,
    以上代码的3种实现形式都可以直接运行,比如保存为get_hw_info.py并放在桌面,
    每次需要获取系统版本信息的时候,双击运行get_hw_info.py,
    运行效果如下(以魅族Note5为例),

    对比如下:


    更多更好的原创文章,请访问官方网站:www.zipython.com
    自拍教程(自动化测试Python教程,武散人编著)
    原文链接:https://www.zipython.com/#/detail?id=fdbe5cbccbb4400b959716226643367d
    也可关注“武散人”微信订阅号,随时接受文章推送。

  • 相关阅读:
    希腊字母写法
    The ASP.NET MVC request processing line
    lambda aggregation
    UVA 10763 Foreign Exchange
    UVA 10624 Super Number
    UVA 10041 Vito's Family
    UVA 10340 All in All
    UVA 10026 Shoemaker's Problem
    HDU 3683 Gomoku
    UVA 11210 Chinese Mahjong
  • 原文地址:https://www.cnblogs.com/zipython/p/12485723.html
Copyright © 2020-2023  润新知