在我们做 UI 自动化之前,需要自动安装 apk,然而很多时候会遇到很多系统弹窗,那么我们可以用 python3 的线程去监控点掉系统弹窗,直到我们的目标 apk 安装成功,再退出这个线程。

# -*- coding: utf-8 -*-
# @author: xiaoxiao
# @date  : 2019/4/6


import threading
import os
import uiautomator2 as u2

driver = u2.connect("882QADT9UWT")

class usb_install_thread(threading.Thread): # 安装确认

    def __init__(self):
        threading.Thread.__init__(self)

    def run(self): # 把要执行的代码写到run函数里面 线程在创建后会直接运行run函数
        self.usb_install()

    # 判断apk是否已经安装
    def is_apk_has_installed(self, package_name, device_id = ''):
        if device_id != '':
            cmd = "adb -s " + str(device_id) + " shell pm list package | grep '" + str(package_name) +"'"
        else:
            cmd = "adb shell pm list package | grep '" + str(package_name) + "'"
        result = os.popen(cmd).read()
        return result

    # 判断app应用是否启动到前台
    def is_activity_started(self, package_name, device_id=''):
        if device_id != '':
            cmd_current_activity = "adb -s %s shell dumpsys activity activities | sed -En -e '/Running activities/,/Run #0/p' | grep 'ActivityRecord'" % device_id
        else:
            cmd_current_activity = "adb shell dumpsys activity activities | sed -En -e '/Running activities/,/Run #0/p' | grep 'ActivityRecord'"
        cmd_result = str(self.shell(cmd_current_activity))
        # 如果当前应用处于前台或resume后台状态,返回True
        if package_name in cmd_result:
            # 启动app后,如果有系统权限弹窗,也需要点掉它
            if 'GrantPermissionsActivity' in cmd_result:
                return False
            return True
        else:
            return False

    # 等待元素出现,默认等待10s
    def wait_for_element_visible(self, text, timeout=10):
        # return bool
        return driver(text = text).wait(timeout=timeout)

    # 点掉系统弹窗
    def usb_install(self):
        print("Begin to install apk...\n")
        while True:
            # 如果apk安装成功,则退出
            if self.is_apk_has_installed('com.xxx'):
                # print("The apk has been installed~")
                break
            # 点掉弹窗"允许"、"确认"、"继续安装"等
            try:
                driver(text="允许").click()
            except:
                pass
            try:
                driver(text="确认").click()
            except:
                pass
            try:
                driver(text="继续安装").click()
            except:
                pass
           try:
                driver(text="安装").click()
            except:
                pass

    # 执行adb shell
    def shell(self, cmd):
        p = os.popen(cmd)
        return p.read()

    # 安装apk
    def app_install(self, app_file_path, device_id = ''):
        if device_id == '':
            cmd = 'adb install -r ' + str(app_file_path)
        else:
            cmd = 'adb -s ' + str(device_id) +'install -r ' + str(app_file_path)
        self.shell(cmd)


thread1 = usb_install_thread()
thread1.start()
print("The thread is alive: " + str(thread1.is_alive()))
thread1.app_install('/Users/xiaoxiao/Downloads/xxx.apk', '882QADT9UWT')
print("After install the apk, the thread is alive: " + str(thread1.is_alive()))

注意:如果是 oppo、vivo 等手机可能会遇到权限报错(如下图):java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission

问题原因:因为 oppo、vivo 手机的开发者选项中没有开启 “USB 模拟点击”,无法通过自动化进行屏幕点击操作
解决步骤:
1.到开发者选项中开启 “USB 模拟点击”
2.如果遇到开启失败,需要返回到手机设置一级目录,退出你的 vivo 账号,再重新登录账号
3.重复步骤 1 即可

更多内容,请看下篇:https://testerhome.com/topics/19579


↙↙↙阅读原文可查看相关链接,并与作者交流