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?
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"
)
}
}
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");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With