Skip to content

Commit

Permalink
add app_auto_grant_permissions methods
Browse files Browse the repository at this point in the history
  • Loading branch information
caofengbin committed May 20, 2024
1 parent 64c6f2b commit 2c5e3ac
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions mobile_tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

36 changes: 36 additions & 0 deletions uiautomator2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,42 @@ 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
"""
output, _ = self.shell(['dumpsys', 'package', f'{package_name}'])
groupPattern = re.compile(r'^(\s*' + 'runtime' + r' permissions:[\s\S]+)', re.MULTILINE)
groupMatcher = groupPattern.search(output)

Check warning on line 886 in uiautomator2/__init__.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/__init__.py#L884-L886

Added lines #L884 - L886 were not covered by tests
if not groupPattern:
return False
groupMatch = groupMatcher.group(1)
lines = groupMatch.split("\n")

Check warning on line 890 in uiautomator2/__init__.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/__init__.py#L888-L890

Added lines #L888 - L890 were not covered by tests
if len(lines) < 2:
return False
titleIndent = len(lines[0]) - len(lines[0].lstrip())

Check warning on line 893 in uiautomator2/__init__.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/__init__.py#L892-L893

Added lines #L892 - L893 were not covered by tests
for i in range(1, len(lines)):
line = lines[i]
currentIndent = len(line) - len(line.lstrip())

Check warning on line 896 in uiautomator2/__init__.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/__init__.py#L895-L896

Added lines #L895 - L896 were not covered by tests

if currentIndent <= titleIndent:
break

Check warning on line 899 in uiautomator2/__init__.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/__init__.py#L899

Added line #L899 was not covered by tests

permissionNamePattern = re.compile(r'android\.\w*\.?permission\.\w+')
permissionNameMatcher = permissionNamePattern.search(line)

Check warning on line 902 in uiautomator2/__init__.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/__init__.py#L901-L902

Added lines #L901 - L902 were not covered by tests

if not permissionNameMatcher:
continue

Check warning on line 905 in uiautomator2/__init__.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/__init__.py#L905

Added line #L905 was not covered by tests
else:
permissionName = permissionNameMatcher.group()
print(permissionName)
self.shell(['pm', 'grant', f'{package_name}', permissionName])
return True

Check warning on line 910 in uiautomator2/__init__.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/__init__.py#L907-L910

Added lines #L907 - L910 were not covered by tests

class _DeprecatedMixIn:
@property
def wait_timeout(self): # wait element timeout
Expand Down

0 comments on commit 2c5e3ac

Please sign in to comment.