9

I am unable to tap on Deny or Allow buttons on the permissions dialog in Android using Appium+Java. Do I need to add any capabilities before going to tap on those buttons? Below is the code:

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("deviceName", "ASUS_Z00LD");
capabilities.setCapability("platformVersion", "6.0");
capabilities.setCapability("app","<AppPath>");
capabilities.setCapability("browserName", "");
AndroidDriver<MobileElement> driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
        Thread.sleep(10000);
driver.findElement(MobileBy.id("permission_allow_button")).click();

Below is error in Eclipse console:

org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information)

Below is the Appium log:

info: [debug] Responding to client with success: {"status":0,"value":{"platform":"LINUX","browserName":"","platformVersion":"6.0.1","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,"warnings":{},"desired":{"app":"/Users/Shiva/Documents/workspace/AndroidPractice/APK/****_Android.apk","browserName":"","platformName":"Android","deviceName":"ASUS_Z00LD","platformVersion":"6.0"},"app":"/Users/Shiva/Documents/workspace/AndroidPractice/APK/****_Android.apk","platformName":"Android","deviceName":"FAAZCY127084"},"sessionId":"b64fd5af-3de5-4299-a2d4-1948fc8e883e"}
info: <-- GET /wd/hub/session/b64fd5af-3de5-4299-a2d4-1948fc8e883e 200 1.534 ms - 625 {"status":0,"value":{"platform":"LINUX","browserName":"","platformVersion":"6.0.1","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,"warnings":{},"desired":{"app":"/Users/Shiva/Documents/workspace/AndroidPractice/APK/****_Android.apk","browserName":"","platformName":"Android","deviceName":"ASUS_Z00LD","platformVersion":"6.0"},"app":"/Users/Shiva/Documents/workspace/AndroidPractice/APK/****_Android.apk","platformName":"Android","deviceName":"FAAZCY127084"},"sessionId":"b64fd5af-3de5-4299-a2d4-1948fc8e883e"}

info: --> POST /wd/hub/session/b64fd5af-3de5-4299-a2d4-1948fc8e883e/element {"using":"id","value":"permission_allow_button"}

info: [debug] Waiting up to 0ms for condition

info: [debug] Pushing command to appium work queue: ["find",{"strategy":"id","selector":"permission_allow_button","context":"","multiple":false}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"permission_allow_button","context":"","multiple":false}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: find
info: [debug] [BOOTSTRAP] [debug] Finding permission_allow_button using ID with the contextId:  multiple: false
info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.****.****:id/permission_allow_button]

info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=android:id/permission_allow_button]

info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[DESCRIPTION=permission_allow_button, INSTANCE=0]

info: [debug] [BOOTSTRAP] [debug] Failed to locate element. Clearing Accessibility cache and retrying.
info: [debug] [BOOTSTRAP] [debug] Finding permission_allow_button using ID with the contextId:  multiple: false
info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.****.****:id/permission_allow_button]

info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=android:id/permission_allow_button]

info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[DESCRIPTION=permission_allow_button, INSTANCE=0]

info: [debug] [BOOTSTRAP] [debug] Returning result: {"status":7,"value":"No element found"}

info: [debug] Condition unmet after 178ms. Timing out.

info: [debug] Responding to client with error: {"status":7,"value":{"message":"An element could not be located on the page using the given search parameters.","origValue":"No element found"},"sessionId":"b64fd5af-3de5-4299-a2d4-1948fc8e883e"}
info: <-- POST /wd/hub/session/b64fd5af-3de5-4299-a2d4-1948fc8e883e/element 500 181.769 ms - 195 

Anyone's help to overcome this would be appreciated?

5 Answers5

9

Use full resource ID ...It worked for me....

below line worked for me.... driver.findElement(MobileBy.id("com.android.packageinstaller:id/permission_allow_button")).click();

Akhilesh Sinha
  • 861
  • 12
  • 20
5

With the below snippet I am able to click on all the allow buttons to get the permissions.

while (driver.findElements(MobileBy.xpath("//*[@class='android.widget.Button'][2]")).size()>0) {
    driver.findElement(MobileBy.xpath("//*[@class='android.widget.Button'][2]")).click();
}
5

Starting from appium 1.6.3 you can just add:

capabilities.setCapability("autoGrantPermissions", "true");

And you'll always allow all permissions your app wants.

Alex
  • 108
  • 1
  • 6
1

Appium gives you an API that detect the activity. Depending upon your device, you could get two activities - the package name may get stripped off or not:

'com.android.packageinstaller.permission.ui.GrantPermissionsActivity',
'.permission.ui.GrantPermissionsActivity'

After detecting this activity, you need to find an element by locator(id/xpath):

'com.android.packageinstaller:id/permission_message'

Then you can obtain the text of that message if you are interested in it. If you care which permission it is, you can match it against expected strings or regular expressions. If not, you can blindly accept by finding and clicking the element by id:

'com.android.packageinstaller:id/permission_allow_button'

If you'd rather not click 'allow' on all those windows, you can use adb to add all the permissions at once before you start testing (but after Appium has installed your app). If you know all the perms your app will need, you can add them with one command:

pm grant $app_name $space_delimited_set_of_perms

Or you can add all permissions one at a time, which takes 1.5-2 seconds per attempt.

Reference : https://discuss.appium.io/t/android-m-and-permissions/5760/13

Keshav
  • 415
  • 2
  • 12
  • Thanks for the quick response. I have tried that before but I didn't get success. That is the reason to post question in SO. But I didn't try 'pm grant $app_name $space_delimited_set_of_perms' . Could you tell what I have to at '$space_delimited_set_of_perms'. Thanks – Shiva Krishna Chippa Oct 19 '16 at 05:20
  • You can grant permission to your application using adb's simple command(in Quotation marks-ignore Quotation marks), "adb shell pm grant com.name.app android.permission.READ_PROFILE", app name you can enter the package id of your application under test – Keshav Oct 19 '16 at 06:23
  • I have appended below lines to my existing code. But no success. Could you look at this once and let me know if am doing in a wrong way. ProcessBuilder builder = new ProcessBuilder("/bin/bash", "-c", "/usr/local/bin/adb shell pm grant android.permission.READ_PROFILE"); builder.redirectErrorStream(true); builder.start(); – Shiva Krishna Chippa Oct 19 '16 at 09:26
  • 1. This code should work after installation of application. 2. Permission activity i had mentioned was a example, you should use a permission which fits your purpose, you can check the list of permission activities on this page https://developer.android.com/guide/topics/security/permissions.html – Keshav Oct 19 '16 at 10:26
0

I tried lot many solutions before I found what is the fix. I had enabled Touch ball / quick ball feature and icon used stay always on the screen to help me navigate.

This app was the culprit which had overlay feature enabled and wasn't easy to figure it out by browsing the active apps under the installed apps.

Note: Mine was MIUI 11 with Android 7 for Redmi Note 4.

Sailendra
  • 1,318
  • 14
  • 29