Django+vue自动化测试平台(28)-- ADB获取设备信息

概述

adb的全称为Android Debug Bridge,就是起到调试桥的作用。通过adb可以在Eclipse中通过DDMS来调试Android程序,说白了就是调试工具。

adb的工作方式比较特殊,采用监听Socket TCP 5554等端口的方式让IDE和Qemu通讯,默认情况下adb会daemon相关的网络端口,所以当我们运行Eclipse时adb进程就会自动运行。

adb是android sdk里的一个工具, 用这个工具可以直接操作管理android模拟器或者真实的android设备(如G1手机).  它的主要功能有:

  • 运行设备的shell(命令行)
  • 管理模拟器或设备的端口映射
  • 计算机和设备之间上传/下载文件
  • 将本地apk软件安装至模拟器或android设备
    ADB是一个 客户端-服务器端 程序, 其中客户端是你用来操作的电脑, 服务器端是android设备.

代码实现:

python 复制代码
def get_device_info():
    version = ""
    cmd = 'tidevice list --json'
    device_info_list = []
    ios_result = os.popen(cmd).read().replace("\\n", "").replace("\u001b[0m", "")
    if eval(ios_result):
        for i in eval(ios_result):
            if i["market_name"] == "-":
                name = i["name"]
            else:
                name = i["market_name"]
            ios = {}
            ios["deviceName"] = i["udid"]
            ios["device_name"] = name
            ios["os_type"] = "ios"
            ios["version"] = i["product_version"]   
            device_info_list.append(ios)
    else:
        device_info_list = []
    devices_output = subprocess.check_output("adb devices", shell=True).decode().strip()
    if devices_output != "List of devices attached":
        devices_list = devices_output.split('\n')[1:]
        for device in devices_list:
            device_id = device.split('\t')[0]
            device_info_output = subprocess.check_output(f"adb -s {device_id} shell getprop",
                                                         shell=True).decode().strip()
            brand = get_property(device_info_output, 'ro.product.brand')
            if brand == "OPPO":
                if get_property(device_info_output, 'ro.oppo.market.name') == "":
                    device_name = get_property(device_info_output, 'ro.vendor.oplus.market.name')
                else:
                    device_name = get_property(device_info_output, 'ro.oppo.market.name')
                version = str(get_property(device_info_output, "ro.product.build.version.release"))
            elif brand == "HUAWEI":
                # 鸿蒙系统
                version = "Harmony OS " + str(get_property(device_info_output, "hw_sc.build.platform.version"))
                device_name = get_property(device_info_output, 'ro.config.marketing_name')
            elif brand == "Redmi":
                device_name = get_property(device_info_output, 'ro.product.marketname')
                version = str(get_property(device_info_output, "ro.product.build.version.release"))
            elif brand == "SG":
                device_name = get_property(device_info_output, 'ro.sh.brand')
                version = str(get_property(device_info_output, "ro.system.build.version.release"))
            elif brand == "XIAOMI":
                device_name = get_property(device_info_output, 'ro.product.model')
                version = str(get_property(device_info_output, "ro.product.build.version.release"))
            elif brand == "OnePlus":
                device_name = get_property(device_info_output, 'ro.product.device')
                version = str(get_property(device_info_output, "ro.product.build.version.release"))
            elif brand == "google":
                device_name = get_property(device_info_output, 'ro.product.model')
                version = str(get_property(device_info_output, "ro.product.build.version.release"))
            elif brand == "HONOR":
                device_name = get_property(device_info_output, 'ro.config.marketing_name')
                version = str(get_property(device_info_output, "ro.product.build.version.release"))
            elif brand == "samsung":
                device_name = get_property(device_info_output, 'ro.product.model')
                version = str(get_property(device_info_output, "ro.product.build.version.release"))
            else:
                device_name = get_property(device_info_output, 'ro.product.brand')
                version = "未知操作系统版本"
            device_info_list.append({
                'deviceName': get_property(device_info_output, 'ro.serialno'),
                'device_name': device_name,
                "os_type": "android",
                "version": version,
            })
        else:
            return device_info_list
    return device_info_list

结果:

javascript 复制代码
[
  {
    "deviceName": "00008110-000C49623C99801E",
    "device_name": "iPhone 13 Pro",
    "os_type": "ios",
    "version": "15.1.1"
  },
  {
    "deviceName": "6faf7764",
    "device_name": "OPPO K9 5G",
    "os_type": "android",
    "version": "12"
  }
]
相关推荐
袁袁袁袁满9 分钟前
100天精通Python(爬虫篇)——第113天:‌爬虫基础模块之urllib详细教程大全
开发语言·爬虫·python·网络爬虫·爬虫实战·urllib·urllib模块教程
老大白菜32 分钟前
Python 爬虫技术指南
python
深圳安锐科技有限公司2 小时前
首次接触结构安全自动化监测系统,价格高吗?后期维护?
运维·自动化
古希腊掌管学习的神2 小时前
[搜广推]王树森推荐系统——矩阵补充&最近邻查找
python·算法·机器学习·矩阵
TGB-Earnest2 小时前
【py脚本+logstash+es实现自动化检测工具】
大数据·elasticsearch·自动化
LucianaiB3 小时前
探索CSDN博客数据:使用Python爬虫技术
开发语言·爬虫·python
一个处女座的程序猿O(∩_∩)O3 小时前
小型 Vue 项目,该不该用 Pinia 、Vuex呢?
前端·javascript·vue.js
PieroPc5 小时前
Python 写的 智慧记 进销存 辅助 程序 导入导出 excel 可打印
开发语言·python·excel
云和数据.ChenGuang6 小时前
Django 应用安装脚本 – 如何将应用添加到 INSTALLED_APPS 设置中 原创
数据库·django·sqlite
梧桐树04299 小时前
python常用内建模块:collections
python