androidTest(已检测):如何在 Android 8.0 (API 26) 上单击“允许”按钮?

问题描述 投票:0回答:1

对于仪器化的

androidTest
测试,当应用程序在运行Android 8(API 26)的设备上启动时,我需要单击系统弹出的
ALLOW
“按钮”:

Allow app to record audio on Android 8

(注意:在 Android 14 (API 34) 上我没有问题,因为权限对话框完全不同,并且可以通过其资源 id 访问其“允许/授予”按钮)

以下代码:

UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
            dialogContainer = device.findObject(By.textEndsWith("to record audio?")); // Android 8.x
            if (dialogContainer != null) {
                UiObject2 allowButton = dialogContainer.findObject(By.textStartsWith("Allow"));
                if (allowButton != null)
                    allowButton.click();
                 else
                    logE("PermissionTest", "Error! Could not find 'Allow' dialog button");
            }
            else
                logE("PermissionTest", "Error! Permission dialog did not appear");

结果:

09-13 10:52:09.831 30108 30133 W UiObject2: Clicking on non-clickable object:
 android.view.accessibility.AccessibilityNodeInfo@64c49;
 boundsInParent: Rect(0, 0 - 756, 175);
 boundsInScreen: Rect(412, 1083 - 1168, 1258);
 packageName: com.google.android.packageinstaller;
 className: android.widget.TextView;
 text: Allow My App to record audio?; error: null;
 maxTextLength: -1; contentDescription: null;
 viewIdResName: com.android.packageinstaller:id/permission_message;
 checkable: false; checked: false; focusable: false; focused: false; selected: false; clickable: false;
 longClickable: false; contextClickable: false; enabled: true; password: false;
 scrollable: false; importantForAccessibility: true;
 actions: [AccessibilityAction: ACTION_SELECT - null, AccessibilityAction: ACTION_CLEAR_SELECTION - null, AccessibilityAction: ACTION_ACCESSIBILITY_FOCUS - null, AccessibilityAction: ACTION_NEXT_AT_MOVEMENT_GRANULARITY - null, AccessibilityAction: ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY - null, AccessibilityAction: ACTION_SET_SELECTION - null, AccessibilityAction: ACTION_SHOW_ON_SCREEN - null]

我怀疑这是因为

ALLOW
不是一个真正的按钮。这是因为
dialogContainer
不为空,并且
allowButton
也不为空。

但是,我不知道如何从我的

ALLOW
以编程方式单击该
androidTest
(文本?链接?)。

在 Android 8.0 (API 26) 上单击“允许”按钮的正确方法是什么

androidTest

android android-espresso android-testing android-uiautomator
1个回答
0
投票

根据两个SO答案,OP推测并确认可以使用其android id找到“权限允许按钮”,如下所示:

UiObject2 allowButton2 = device.findObject(By.res("com.android.packageinstaller:id/permission_allow_button"));

在此处找到了搜索资源 id 的概念(尽管还有很多其他可能的来源)。

这里找到了按钮资源id的实际标识。 至于如何使用官方 Android 文档找到 id 目前是一个谜 - 除了使用 SO 之外。

© www.soinside.com 2019 - 2024. All rights reserved.