Android测试--用Python批量给多设备安装app应用

今天要在多个设备上验证一个简单的问题(大概20多台设备),嫌弃一个一个安装太麻烦,简单的写了个python的脚本。
基础命令非常简单:需要指定设备名和apk的路径

adb -s devices install -r APK_PATH

思路是这样,先获取到adb devices列出的所有连接设备,至于其他异常情况直接忽略了。(因为只为快速偷懒。。。)然后为每台设备安装就好了。

    apk_path = "/Users/johnhao/Download/yourapp.apk"

    connectdeviceid = []
    p = os.popen('adb devices')
    outstr = p.read()
    print outstr
    connectdeviceid = re.findall(r'(\w+)\s+device\s', outstr)

    for device in connectdevice:
        cmd = "adb -s %s install -r %s" % (device,apk_path)
        subprocess.call(cmd, shell=True)
        print device, "Install finished"

试验发现这种方法慢的要死,设备一个接一个的安装,简直不能忍。所以自然而然地想到用threads处理。

    commands = []

    for device in connectdevice:
        cmd = "adb -s %s install -r %s" % (device,apk_path)
        commands.append(cmd)

    threads = []
    threads_count = len(commands)

    for i in range(threads_count):
        t = threading.Thread(target = excute, args = (commands[i],))

试验下,发现还可以,之前快了许多。。。效率高了不少
下面是完整的code,哪里不对或者应用错误还望指正

#!usr/bin/python
# -*- coding:utf-8 -*-
# Author:John Hao
# 把指定目录的apk安装到所有连接设备中
import os
import subprocess
import threading
import re
import time

apk_path = "/Users/johnhao/Download/yourapp.apk"

def excute(cmd):
    subprocess.Popen(cmd, shell=True)

def get_conn_dev():
    connectdeviceid = []
    p = os.popen('adb devices')
    outstr = p.read()
    print outstr
    connectdeviceid = re.findall(r'(\w+)\s+device\s', outstr)
    return connectdeviceid

def main():
    connectdevice = get_conn_dev()
    commands = []

    for device in connectdevice:
        cmd = "adb -s %s install -r %s" % (device,apk_path)
        commands.append(cmd)

    threads = []
    threads_count = len(commands)

    for i in range(threads_count):
        t = threading.Thread(target = excute, args = (commands[i],))
        threads.append(t)

    for i in range(threads_count):
        time.sleep(1)  # 防止adb连接出错
        threads[i].start()

    for i in range(threads_count):
        threads[i].join()

if __name__ == '__main__':
    main()
Android测试--用Python批量给多设备安装app应用_第1张图片
关注获取更多

你可能感兴趣的:(Android测试--用Python批量给多设备安装app应用)