androidandroid-espressoandroid-testingandroid-uiautomator

androidTest (instrumented): How to click the ALLOW button on Android 8.0 (API 26)?


For an instrumented androidTest test, I need to click the ALLOW "button" as popped up by the system when the app starts on a device running Android 8 (API 26):

Allow app to record audio on Android 8

(Note: on Android 14 (API 34) I have no problem as the permission dialog box is entirely different and its "allow/grant" button can be accessed by its resource id)

The following code:

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");

results in:

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]

I am suspecting this is because ALLOW is not a real button. This is because dialogContainer isn't null and allowButton isn't null either.

However, I don't know how to click that ALLOW (text? link?) programmatically from my androidTest.

What is the proper way of clicking the ALLOW button on Android 8.0 (API 26) androidTest ?


Solution

  • Based on two SO answers it was speculated and confirmed by OP that finding the "permission allow button" can be done using its android id as in:

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

    The notion of searching on resource id was found here (although plenty of other possible sources).

    The actual identification of the system button resource id was found here.

    As to how one could find out that id using official android documentation is a mystery at the moment - other than using SO.


    Updating the original OP code:

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

    Note starting at API 30 (Android 11) the permission dialog has three buttons described here the additional button is the "Only this time" choice.