Android Marshmallow: Test permissions with Espresso?

前端 未结 13 1363
庸人自扰
庸人自扰 2020-11-29 19:31

The new permissions scheme introduced by Android Marshmallow requires checking for specific permissions at runtime, which implies the need to provide different flows dependi

13条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 20:08

    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");
        }
    }
    

提交回复
热议问题