Michael Osofsky
Michael Osofsky

Reputation: 13135

GrantPermissionRule stopped working

The GrantPermissionRule that I successfully implemented in my Android app on 1/3/2018 no longer works. When I run my Espresso tests through Android Studio, the emulator blocks waiting for permissions. However, when I run the tests from the command line with ./gradlew dist; ./gradlew connectedDebugAndroidTest --stacktrace it doesn't ask for the permissions. Note I do wipe data from the emulator manually before each run to ensure it's a proper test of GrantPermissionRule.

Here are the original references I used to implement GrantPermissionRule: https://www.kotlindevelopment.com/runtime-permissions-espresso-done-right/ https://developer.android.com/reference/android/support/test/rule/GrantPermissionRule.html

Versions in app/build.gradle:

Has GrantPermissionRule stopped working as-advertised for anyone else?

Upvotes: 10

Views: 4087

Answers (3)

Muhammad Maqsood
Muhammad Maqsood

Reputation: 1662

In my case, I was trying to automate Location permissions in Espresso UI tests on Emulator Pixel 2 API R.

UseCase 1:

When I was adding both ACCESS_FINE_LOCATION & ACCESS_COARSE_LOCATION in GrantPermissionRule then RunTime permission popup was not auto disappearing.

Not Working:

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

Then I removed ACCESS_COARSE_LOCATION from GrantPermissionRule and the automation starts working.

Working:

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

UseCase 2:

Also below implementation working as above mentioned (failure/success)use-case.

Not Working:

@Before
fun grantPhonePermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        getInstrumentation().uiAutomation.executeShellCommand(
            "pm grant " + getApplicationContext<Context>()
                .packageName
                    + " android.permission.ACCESS_FINE_LOCATION"
        )
        getInstrumentation().uiAutomation.executeShellCommand(
            "pm grant " + getApplicationContext<Context>()
                .packageName
                    + " android.permission.ACCESS_COARSE_LOCATION"
        )
    }
}

Working:

@Before
fun grantPhonePermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        getInstrumentation().uiAutomation.executeShellCommand(
            "pm grant " + getApplicationContext<Context>()
                .packageName
                    + " android.permission.ACCESS_FINE_LOCATION"
        )
    }
}

Upvotes: 1

Michael Osofsky
Michael Osofsky

Reputation: 13135

The problem seems to have disappeared, perhaps because I had to downgrade the Android Emulator to work around another issue (Here's How to downgrade Android Emulator).

Upvotes: 0

Zeek Aran
Zeek Aran

Reputation: 606

I tried using

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

And this did not work. The permission alerts still show up when I run my espresso tests. This has been working for me though:

@Before
public void grantPhonePermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        getInstrumentation().getUiAutomation().executeShellCommand(
                "pm grant " + getTargetContext().getPackageName()
                        + " android.permission.ACCESS_FINE_LOCATION");
    }
}

Upvotes: 6

Related Questions