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):
(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 ?

By.textStartsWith("Allow")is finding theTextViewsince it has theAllowword in it - hence the click error (the error dump indicates it is aTextView) - the button hasALLOW- is theBy.text..case insensitive?AllowwithALLOW, then allowButton is null. So the search is case sensitive but counterintuitive because the text found isn't the text displayed. ¯_(ツ)_/¯com.android.packageinstaller:id/permission_allow_buttonbased 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.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 youUiObject2 allowButton = dialogContainer.findObject(By.textStartsWith("Allow"));in the code snippet in my original posting.