From 5d20e56ebef91b459c52bcf6b92fad5be1e32ea5 Mon Sep 17 00:00:00 2001 From: fengbincao <1050430934@qq.com> Date: Mon, 27 May 2024 14:25:15 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=B7=BB=E5=8A=A0=E2=80=9C=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E6=8E=88=E6=9D=83=E5=BA=94=E7=94=A8=E6=9D=83=E9=99=90?= =?UTF-8?q?=E2=80=9D=E6=96=B9=E6=B3=95=EF=BC=8C=E6=8C=87=E5=AE=9A=E5=8C=85?= =?UTF-8?q?=E5=90=8D=E5=90=8E=E8=87=AA=E5=8A=A8=E8=B5=8B=E4=BA=88=E8=A2=AB?= =?UTF-8?q?=E6=B5=8B=E5=BA=94=E7=94=A8=E8=BF=90=E8=A1=8C=E6=97=B6=E6=89=80?= =?UTF-8?q?=E9=9C=80=E6=9D=83=E9=99=90=EF=BC=8C=E4=B8=8D=E5=86=8D=E5=87=BA?= =?UTF-8?q?=E7=8E=B0=E8=BF=90=E8=A1=8C=E6=97=B6=E6=9D=83=E9=99=90=E5=BC=B9?= =?UTF-8?q?=E7=AA=97=EF=BC=8C=E9=81=BF=E5=85=8D=E5=AF=B9=E7=94=A8=E4=BE=8B?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E7=9A=84=E5=B9=B2=E6=89=B0=20(#977)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add app_auto_grant_permissions methods * fix some code specifications * add target API version check when auto grant permission * 修改判断是否执行动态权限的授予,改为使用手机系统版本 --- mobile_tests/test_session.py | 4 ++++ uiautomator2/__init__.py | 44 ++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/mobile_tests/test_session.py b/mobile_tests/test_session.py index cfea114..c642227 100644 --- a/mobile_tests/test_session.py +++ b/mobile_tests/test_session.py @@ -54,3 +54,7 @@ def test_session_app(dev: u2.Device, package_name): def test_session_window_size(dev: u2.Device): assert isinstance(dev.window_size(), tuple) + +def test_auto_grant_permissions(dev: u2.Device): + dev.app_auto_grant_permissions('com.tencent.mm') + diff --git a/uiautomator2/__init__.py b/uiautomator2/__init__.py index 870f190..fc6e3be 100644 --- a/uiautomator2/__init__.py +++ b/uiautomator2/__init__.py @@ -9,6 +9,7 @@ import logging import os import re +import string import time import warnings from functools import cached_property @@ -895,6 +896,49 @@ def app_info(self, package_name: str) -> Dict[str, Any]: "versionCode": info.version_code, } + def app_auto_grant_permissions(self, package_name: str) -> bool: + """ auto grant runtime permissions to target app,prevent dynamic permission pop-up window to pop up + Args: + package_name (str): package name + + Returns: + bool of operate + """ + device_api_version = self.shell(['getprop', 'ro.build.version.sdk']).output + device_api_version = device_api_version.rstrip("\n") + output, _ = self.shell(['dumpsys', 'package', f'{package_name}']) + + group_pattern = re.compile(r'^(\s*' + 'runtime' + r' permissions:[\s\S]+)', re.MULTILINE) + group_matcher = group_pattern.search(output) + if not group_pattern: + return False + group_match = group_matcher.group(1) + lines = group_match.split("\n") + if len(lines) < 2: + return False + title_indent = len(lines[0]) - len(lines[0].lstrip()) + if device_api_version is None or int(device_api_version) < 23: + print('Skipping permissions grant option,only target api greater or equal to 23 support') + return True + for i in range(1, len(lines)): + line = lines[i] + current_indent = len(line) - len(line.lstrip()) + + if current_indent <= title_indent: + break + + permission_name_pattern = re.compile(r'android\.\w*\.?permission\.\w+') + permission_name_matcher = permission_name_pattern.search(line) + + if not permission_name_matcher: + continue + else: + permission_name = permission_name_matcher.group() + print(f'auto grant permission {permission_name}') + self.shell(['pm', 'grant', package_name, permission_name]) + return True + + class _DeprecatedMixIn: @property def wait_timeout(self): # wait element timeout