2

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 ?

5
  • 1
    The By.textStartsWith("Allow") is finding the TextView since it has the Allow word in it - hence the click error (the error dump indicates it is a TextView) - the button has ALLOW - is the By.text.. case insensitive? Commented Sep 13, 2024 at 15:25
  • @Computable Good observation. If I replace Allow with ALLOW, then allowButton is null. So the search is case sensitive but counterintuitive because the text found isn't the text displayed. ¯_(ツ)_/¯ Commented Sep 14, 2024 at 18:25
  • 1
    This answer shows how to get with res id : the res id for the allow button is com.android.packageinstaller:id/permission_allow_button based on this. Also, using the layout inspector at the time of permission display may provide more insight into the elements involved. Not able to test so just a suggestion. Commented Sep 14, 2024 at 20:39
  • UiObject2 allowButton2 = device.findObject(By.res("com.android.packageinstaller:id/permission_allow_button")); did the trick and it works perfectly. Please post your comment as an answer and I will make it the accepted answer. Thank you Commented Sep 14, 2024 at 22:26
  • @Computable The above statement (that works) is simply a replacement for UiObject2 allowButton = dialogContainer.findObject(By.textStartsWith("Allow")); in the code snippet in my original posting. Commented Sep 15, 2024 at 6:11

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The beauty of your answer is that it provides a systematic approach to finding a resource id of such system level button/text, albeit Android Device Monitor application is deprecated now. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.