Android Marshmallow: Test permissions with Espresso?
Asked Answered
H

13

77

The new permissions scheme introduced by Android Marshmallow requires checking for specific permissions at runtime, which implies the need to provide different flows depending on whether the user denies or allows access.

As we use Espresso to run automated UI tests on our app, how can we mock or update the state of the permissions in order to test different scenarios?

Hixson answered 26/11, 2015 at 3:6 Comment(2)
Possible duplicate of How to manage Runtime permissions android marshmallow espresso testsObduce
Try this it may be help you:-https://mcmap.net/q/81425/-storage-permission-error-in-marshmallowSuperconductivity
B
117

With the new release of the Android Testing Support Library 1.0, there's a GrantPermissionRule that you can use in your tests to grant a permission before starting any tests.

@Rule public GrantPermissionRule permissionRule = GrantPermissionRule.grant(android.Manifest.permission.ACCESS_FINE_LOCATION);

Kotlin solution

@get:Rule var permissionRule = GrantPermissionRule.grant(android.Manifest.permission.ACCESS_FINE_LOCATION)

@get:Rule must be used in order to avoid java.lang.Exception: The @Rule 'permissionRule' must be public. More info here.

Board answered 26/7, 2017 at 8:1 Comment(8)
Works like a charm! Check this blog post for further info.Georgeannageorgeanne
I keep encountering the following error despite having declared the permission in the Manifest: 12-28 14:09:35.063 7193-7215/com.blaha.test E/GrantPermissionCallable: Permission: android.permission.SET_TIME cannot be granted!Birdiebirdlike
This should be accepted as correct answer because it uses appropriate framework and actually work comparing to other solutions.Homoeroticism
@Faux Pas the GrantPermissionRule only works for runtime permissions the user can grant from within the app.Lichfield
Do not forget to add permission on Manifest, you could also create a new AndroidManifest.xml file in path: /src/debugSodality
If you want to grant multiple permissions in Java : @Rule public GrantPermissionRule permissionRule = GrantPermissionRule.grant(android.Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CAMERA);Moxa
Is there any way to do this on per-Method/Test level? Do I really need to split my tests for app setup class in two test classes, one for pre-permission-granted and one for post-permission-granted??Minnow
Is there a way to do this where the permission is removed from the test package, since the permissions appear to be the same as the main module.Shirt
J
54

The accepted answer doesn't actually test the permissions dialog; it just bypasses it. So, if the permissions dialog fails for some reason, your test will give a false green. I encourage actually clicking the "give permissions" button to test the whole app behaviour.

Have a look at this solution:

public static void allowPermissionsIfNeeded(String permissionNeeded) {
    try { 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !hasNeededPermission(permissionNeeded)) {
        sleep(PERMISSIONS_DIALOG_DELAY);
        UiDevice device = UiDevice.getInstance(getInstrumentation());
        UiObject allowPermissions = device.findObject(new UiSelector()
          .clickable(true) 
          .checkable(false) 
          .index(GRANT_BUTTON_INDEX));
        if (allowPermissions.exists()) {
          allowPermissions.click();
        } 
      } 
    } catch (UiObjectNotFoundException e) {
      System.out.println("There is no permissions dialog to interact with");
    } 
  } 

Find the whole class here: https://gist.github.com/rocboronat/65b1187a9fca9eabfebb5121d818a3c4

By the way, as this answer has been a popular one, we added PermissionGranter to Barista, our tool above Espresso and UiAutomator to make instrumental tests green: https://github.com/SchibstedSpain/Barista check it out, because we will maintain it release by release.

Jaunitajaunt answered 7/6, 2016 at 8:10 Comment(15)
Works wonders! Also for testing with different locales, which is my case.Etherize
Awesome! In most answers people don't take in care device's language.. So, well done and thanks!!Narrative
You can duplicate the code but change index to 0 if you want to test denying permission as well. Index 2 is the checkbox for never ask me again.Dryasdust
@Dryasdust wep! Just updated the code to avoid the "never ask me again" checkbox. It is not useful on a test environment. BTW, thanks for the feedback!Jaunitajaunt
Awesome, how would you test this if you have multiple test methods and all of them need a permission. Can you handle this in @BeforeTest or do you have to check at the beginning of each method?Gardening
@Gardening you have to call this code everytime you know that the permission dialog may appear. So, @ BeforeTest is not a great place to put it, but inside your @ TestJaunitajaunt
What if my app requests two permissions at once? The OS will then show a dialog where you need to click allow twice. I'm guessing it would work if you call your allowPermissionsIfNeeded twice, but only if you call it in the right order. If you reverse the order and check the second permission first, it will click allow on the first permission's dialog, and then the first permission check would be like "I already have the permission, no need to click allow" and just stop at the dialog.Ensheathe
So I've just checked, and if using multiple permissions at the same time, the indexes get shifted - 0 is nothing (false), 1 is deny and 2 is allowEnsheathe
Thanks, @Ensheathe ! Just opening an issue at Barista to reproduce and fix it :·)Jaunitajaunt
I've just checked the Barista and I really recommend this library. In the latest version IDs are used instead of indices which are subject to change, also the code is in Kotlin now.Quanta
I spent some time with Barista, it will generate false positives for certain permission edge cases. GrantPermissionRule is the way to go unless you have a specific reason to need to check for the permission dialog.Towardly
Yey @BobLiberatore! May you open a issue on Barista's GitHub? Just to know that edge cases if you have them fresh. Thanks! github.com/AdevintaSpain/BaristaJaunitajaunt
@RocBoronat I've previously done that. It was not productive.Towardly
GRANT_BUTTON_INDEX is unresolved and I am getting nothing from Google.Shirt
thanks @RocBoronat . With Barista this was solved with just one line! PermissionGranter.allowPermissionsIfNeeded(PERMISSION_1_CONTACTS)Unequaled
C
30

Give a try with such static method when your phone is on English locale:

private static void allowPermissionsIfNeeded() {
    if (Build.VERSION.SDK_INT >= 23) {
        UiDevice device = UiDevice.getInstance(getInstrumentation());
        UiObject allowPermissions = device.findObject(new UiSelector().text("Allow"));
        if (allowPermissions.exists()) {
            try {
                allowPermissions.click();
            } catch (UiObjectNotFoundException e) {
                Timber.e(e, "There is no permissions dialog to interact with ");
            }
        }
    }
}

I found it here

Compulsory answered 22/1, 2016 at 13:25 Comment(5)
Note that this won't work with a non EN locale or if the button text changes in the future. It might even fail with some devices if the manufacturers customise the dialog.Subalpine
Strangely this not work on LG Nexus 5 with Android 6. the text "ALLOW" is not found. Using following it works: new UiSelector().clickable(true).checkable(false).index(1)Shovelhead
How do you do this for espresso tests?Pravit
@Bharga there is no way to do it with Espresso. But you can use both UIAutomation and Espresso together.Jeanettajeanette
If you want to deal with localization (or even possible string changes due to manufacturers), the string resource id they're probably using is android.R.string.allow, found here: raw.githubusercontent.com/android/platform_frameworks_base/…Benthamism
D
21

You can grant permissions before the test is run with something like:

@Before
public void grantPhonePermission() {
    // In M+, trying to call a number will trigger a runtime dialog. Make sure
    // the permission is granted before running this test.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        getInstrumentation().getUiAutomation().executeShellCommand(
                "pm grant " + getTargetContext().getPackageName()
                        + " android.permission.CALL_PHONE");
    }
}

But you can't revoke. If you try pm reset-permissions or pm revoke... the process is killed.

Dewhurst answered 3/8, 2016 at 13:51 Comment(5)
I found similar code in google test sample code. It works for the call phone sample app in that package, but it did not work if the permissions are asked as soon as the app starts, such as the camera permission. Anybody has any idea why?Odontograph
You could try @BeforeClass.Subalpine
@fangmobile.com perhaps because the activity is also started in an @ Before rule?Pavonine
You should be able to revoke permissions using the same method so long as you do it with the @org.junit.AfterClass or @org.junit.BeforeClass annotationsClaypan
Sadly this does not work very well. It works for permission checks itself, so WRITE_EXTERNAL_STORAGE is reported as granted afterwards. But permission is not use-able, /sdcard is still not writeable. It would require a app restart after pm grand to work, which is impossible within a test..Minnow
T
11

Actually there are 2 ways of doing this I know so far:

  1. Grant the permission using adb command before test starts (documentation):

adb shell pm grant "com.your.package" android.permission.your_permission

  1. You can click on permission dialog and set the permission using UIAutomator (documentation). If your tests are written with Espresso for android you can combine Espresso and UIAutomator steps in one test easily.
Tadtada answered 26/11, 2015 at 9:54 Comment(5)
I don't see how we could use the adb shell command along with our test suite. And about clicking the dialog, Espresso should be able to handle that itself. The problem is, after you run the test and the permission is enabled, the next time you run the test it will fail because the setting is persisted and the dialog won't show up again.Hixson
Espresso can't handle interactions with the permission dialog since dialog is an instance of other application - com.android.packageinstaller.Tadtada
Have you tried to accept the popup with UIAutomator? For me it doesn't seem to find the "ALLOW" button. Any ideas how to solve this?Pika
@Pika you have to look for "Allow" text, I guess, not "ALLOW". The system makes string upper case at runtime but actually it can be saved as "Allow".Tadtada
@denys, on Nexus 6 it works using ALLOW, but on LG Nexus 5 this won't work, this type of disparities are really anoying.Shovelhead
B
7

You can achieve this easily by granting permission before starting the test. For example if you are supposed to use camera during the test run, you can grant permission as follows

@Before
public void grantPermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        getInstrumentation().getUiAutomation().executeShellCommand(
                "pm grant " + getTargetContext().getPackageName()
                        + " android.permission.CAMERA");
    }
}
Bluebonnet answered 3/3, 2017 at 4:17 Comment(1)
This works for me! I had to look in the manifest to find out exactly which permissions to grant using this technique.Blinking
T
4

ESPRESSO UPDATE

This single line of code grants every permission listed as parameter in the grant method with immediate effect. In other words, the app will be treated like if the permissions were already granted - no more dialogs

@Rule @JvmField
val grantPermissionRule: GrantPermissionRule = GrantPermissionRule.grant(android.Manifest.permission.ACCESS_FINE_LOCATION)

and gradle

dependencies {
  ...
  testImplementation "junit:junit:4.12"
  androidTestImplementation "com.android.support.test:runner:1.0.0"
  androidTestImplementation "com.android.support.test.espresso:espresso-core:3.0.0"
  ...
}

reference: https://www.kotlindevelopment.com/runtime-permissions-espresso-done-right/

Tafia answered 9/1, 2018 at 14:12 Comment(2)
Is there a similar method to deny a permission in order to test that case during a test?Further
Not mentioning it at GrantPermissionRule.grant() makes thisBedplate
S
2

If you need to set a permission for a single test or during runtime rather than a rule you can use this:

PermissionRequester().apply {
    addPermissions(android.Manifest.permission.RECORD_AUDIO)
    requestPermissions()
}

e.g.

@Test
fun openWithGrantedPermission_NavigatesHome() {
    launchFragmentInContainer<PermissionsFragment>().onFragment {
        setViewNavController(it.requireView(), mockNavController)
        PermissionRequester().apply {
            addPermissions(android.Manifest.permission.RECORD_AUDIO)
            requestPermissions()
        }
    }

    verify {
        mockNavController.navigate(R.id.action_permissionsFragment_to_homeFragment)
    }
}
Salford answered 18/1, 2020 at 2:3 Comment(1)
Is this still working? At least for WRITE_EXTERNAL_STORAGE it seems not, since this usually need the app to be restarted to apply!?Minnow
C
1

There is GrantPermissionRule in Android Testing Support Library, that you can use in your tests to grant a permission before starting any tests.

@Rule public GrantPermissionRule permissionRule = GrantPermissionRule.grant(android.Manifest.permission.CAMERA, android.Manifest.permission.ACCESS_FINE_LOCATION);
Cage answered 25/5, 2018 at 6:5 Comment(0)
M
1

Thank you @niklas for the solution. In case anyone looking to grant multiple permissions in Java:

 @Rule
public GrantPermissionRule permissionRule = GrantPermissionRule.grant(android.Manifest.permission.ACCESS_FINE_LOCATION,
        Manifest.permission.CAMERA);
Moxa answered 17/4, 2019 at 8:22 Comment(0)
E
0

I know an answer has been accepted, however, instead of the if statement that has been suggested over and over again, another more elegant approach would be to do the following in the actual test you want for a specific version of OS:

@Test
fun yourTestFunction() {
    Assume.assumeTrue(Build.VERSION.SDK_INT >= 23)
    // the remaining assertions...
}

If the assumeTrue function is called with an expression evaluating to false, the test will halt and be ignored, which I am assuming is what you want in case the test is being executed on a device pre SDK 23.

Ensepulcher answered 2/9, 2017 at 10:51 Comment(0)
C
0

I've implemented a solution which leverages wrapper classes, overriding and build variant configuration. The solution is quite long to explain and is found over here: https://github.com/ahasbini/AndroidTestMockPermissionUtils.

It is not yet packed in an sdk but the main idea is to override the functionalities of ContextWrapper.checkSelfPermission and ActivityCompat.requestPermissions to be manipulated and return mocked results tricking the app into the different scenarios to be tested like: permission was denied hence the app requested it and ended with granted permission. This scenario will occur even if the app had the permission all along but the idea is that it was tricked by the mocked results from the overriding implementation.

Furthermore the implementation has a TestRule called PermissionRule class which can be used in the test classes to easily simulate all of the conditions to test the permissions seamlessly. Also assertions can be made like ensuring the app has called requestPermissions() for example.

Crimea answered 9/3, 2018 at 11:38 Comment(0)
Z
0

For allowing the permission, when necessary, I think the easiest way is to use Barista's PermissionGranter.allowPermissionsIfNeeded(Manifest.permission.GET_ACCOUNTS) directly in the test which requires this permission.

Zacarias answered 23/4, 2020 at 8:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.