标签:and 方式 订阅 rip tps 硬件配置 初始化过程 大小 except
Android系统测试过程中,
比如接口测试,需要用到设备的iccid, 或者uuid,
车载测试需要用到VIN号(车辆唯一标识号),
4G测试等需要设计IMEI号等设备配置字参数等,
我们还可以读取到设备的分辨率,emmc磁盘容量,
cpu型号,内存大小等,这些硬件配置信息,
可以辅助我们做测试,如何读取到这些硬件信息呢?
getprop命令是android自带的动态获取系统属性参数,
包括了软件,硬件属性参数等,其有好几种执行方式,包括:
用法一: getprop 获取全部系统属性参数,
比如魅族Note5的getprop我们保存为txt文件(可点击下载)
用法二: getprop 属性名,可获取某个指定属性的具体参数信息,比如:
相对应的,也有setprop命令是设置某个属性的具体参数的,
测试一般不建议随意设置属性参数。
# 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"Hardware\s+:(.*)", 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”,
有利于提高程序的健壮性!
本次进行改进:
# 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"Hardware\s+:(.*)", 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")
# 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"Hardware\s+:(.*)", 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
也可关注“武散人”微信订阅号,随时接受文章推送。
标签:and 方式 订阅 rip tps 硬件配置 初始化过程 大小 except
原文地址:https://www.cnblogs.com/zipython/p/12485723.html