就我而言,我试图在Emulator Pixel 2 API R上的 Espresso UI 测试中自动化位置权限。
用例 1:
当我在GrantPermissionRule中同时添加 ACCESS_FINE_LOCATION 和 ACCESS_COARSE_LOCATION 时,运行时权限弹出窗口不会自动消失。
不工作:
@Rule
@JvmField
val grantPermissionRule: GrantPermissionRule =
GrantPermissionRule.grant(android.Manifest.permission.ACCESS_FINE_LOCATION,
android.Manifest.permission.ACCESS_COARSE_LOCATION)
然后我从GrantPermissionRule 中删除了ACCESS_COARSE_LOCATION并且自动化开始工作。
在职的:
@Rule
@JvmField
val grantPermissionRule: GrantPermissionRule =
GrantPermissionRule.grant(android.Manifest.permission.ACCESS_FINE_LOCATION)
用例 2:
也低于上述(失败/成功)用例的实现。
不工作:
@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"
)
}
}
在职的:
@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"
)
}
}