51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1768|回复: 0

python3 通过线程解决安装 apk 时的权限弹窗问题

[复制链接]

该用户从未签到

发表于 2019-4-10 16:19:53 | 显示全部楼层 |阅读模式
在我们做UI自动化之前,需要自动安装apk,然而很多时候会遇到很多系统弹窗,那么我们可以用python3的线程去监控点掉系统弹窗,直到我们的目标apk安装成功,再退出这个线程。
  1. # -*- coding: utf-8 -*-
  2. # @author: xiaoxiao
  3. # @date  : 2019/4/6


  4. import threading
  5. import os
  6. import uiautomator2 as u2

  7. driver = u2.connect("882QADT9UWT")

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

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

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

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

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

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

  40.     # 点掉系统弹窗
  41.     def usb_install(self):
  42.         print("Begin to install apk...\n")
  43.         while True:
  44.             # 如果apk安装成功,则退出
  45.             if self.is_apk_has_installed('com.xxx'):
  46.                 # print("The apk has been installed~")
  47.                 break
  48.             # 点掉弹窗"允许"、"确认"、"继续安装"等
  49.             try:
  50.                 driver(text="允许").click()
  51.             except:
  52.                 pass
  53.             try:
  54.                 driver(text="确认").click()
  55.             except:
  56.                 pass
  57.             try:
  58.                 driver(text="继续安装").click()
  59.             except:
  60.                 pass
  61.            try:
  62.                 driver(text="安装").click()
  63.             except:
  64.                 pass

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

  69.     # 安装apk
  70.     def app_install(self, app_file_path, device_id = ''):
  71.         if device_id == '':
  72.             cmd = 'adb install -r ' + str(app_file_path)
  73.         else:
  74.             cmd = 'adb -s ' + str(device_id) +'install -r ' + str(app_file_path)
  75.         self.shell(cmd)


  76. thread1 = usb_install_thread()
  77. thread1.start()
  78. print("The thread is alive: " + str(thread1.is_alive()))
  79. thread1.app_install('/Users/xiaoxiao/Downloads/xxx.apk', '882QADT9UWT')
  80. print("After install the apk, the thread is alive: " + str(thread1.is_alive()))
复制代码


回复

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-4-19 06:49 , Processed in 0.064219 second(s), 24 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表