Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:添加“自动授权应用权限”方法,指定包名后自动赋予被测应用运行时所需权限,不再出现运行时权限弹窗,避免对用例执行的干扰 #977

Merged
merged 4 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
"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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ref https://github.com/appium/appium-adb/blob/master/lib/helpers.js#L585

if (apiLevel < 23) {
      log.debug(`Skipping permissions grant option, since ` +
                `the current API level ${apiLevel} does not support applications ` +
                `permissions customization`);

does here need to check?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

尝试在增加授权之前获取了目标App的target API版本,做判断之后再进行授权操作。

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use group_pattern instead of groupPattern. the following code need follow the same rule.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已经修改

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])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here just use package_name if ok, no need to use f'{package_name}'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改

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
Loading