Android Marshmallow:使用 Espresso 测试权限? [英] Android Marshmallow: Test permissions with Espresso?
问题描述
Android Marshmallow 引入的新权限方案需要在运行时检查特定权限,这意味着需要根据用户是拒绝访问还是允许访问来提供不同的流程.
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.
当我们使用 Espresso 在我们的应用上运行自动化 UI 测试时,我们如何模拟或更新权限状态以测试不同的场景?
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?
推荐答案
随着 Android 测试支持库 1.0,有一个 GrantPermissionRule 您可以在测试中使用它来在开始任何测试之前授予权限.
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 解决方案
@get:Rule var permissionRule = GrantPermissionRule.grant(android.Manifest.permission.ACCESS_FINE_LOCATION)
必须使用
@get:Rule
以避免 java.lang.Exception:@Rule 'permissionRule' 必须是公开的.
更多信息 此处.
@get:Rule
must be used in order to avoid java.lang.Exception: The @Rule 'permissionRule' must be public.
More info here.
这篇关于Android Marshmallow:使用 Espresso 测试权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!