Skip to content Skip to sidebar Skip to footer

Auto Grant Permissions For Espresso Ui Testing

I'm trying to avoid having to manually grant permissions on every single UI test built with Espresso. I have tried: @Rule public GrantPermissionRule permissionRule = GrantPer

Solution 1:

What you're doing is almost correct except that you should "sleep" a bit after executing the shell command. That's what your method body could look like:

    ArrayList<String> permissions = new ArrayList<>();
    permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
    //add here your other permissions

    for (int i = 0; i < permissions.size(); i++) {
        String command = String.format("pm grant %s %s", getTargetContext().getPackageName(), permissions.get(i));
        getInstrumentation().getUiAutomation().executeShellCommand(command);
        // wait a bit until the command is finished
        SystemClock.sleep(1000);
    }

Post a Comment for "Auto Grant Permissions For Espresso Ui Testing"