Espresso UI测试自动授权权限

5

我试图避免在每个使用Espresso构建的UI测试中手动授予权限。

我已经尝试过:

 @Rule
    public GrantPermissionRule permissionRule = GrantPermissionRule.grant(
            android.Manifest.permission.INTERNET,
            android.Manifest.permission.READ_PHONE_STATE,
            android.Manifest.permission.ACCESS_NETWORK_STATE,
            android.Manifest.permission.ACCESS_FINE_LOCATION.....)

并且

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

这里是我的大部分 Gradle 文件:

apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion '26.0.2'
defaultConfig {
    minSdkVersion 14
    targetSdkVersion 23
    testInstrumentationRunner 
'android.support.test.runner.AndroidJUnitRunner'
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 
'proguard-rules.txt'
        debuggable false
        jniDebuggable false
        buildConfigField "boolean", "UseProduction", "true"
    }
    qa {
        initWith debug
        debuggable true
        applicationIdSuffix ".qa" 
        buildConfigField "boolean", "UseProduction", "false"
    }
    debug {
        debuggable true
        testCoverageEnabled = false
        buildConfigField "boolean", "UseProduction", "true"
    }

}

flavorDimensions "standard"

productFlavors {
    standard
            {
                buildConfigField "boolean", "ChinaBuild", "false"
                buildConfigField "boolean", "YTOBuild", "false"
            }
   ....
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}
sourceSets {
    main {
        java.srcDirs = ['src/main/java']
    }
}


dependencies {
implementation 'com.android.support:support-v4:24.2.1'
implementation 'com.android.support:appcompat-v7:24.2.1'
implementation 'com.android.support:design:24.2.1'
implementation 'com.android.support:recyclerview-v7:24.2.1'
implementation 'com.android.support:percent:24.2.1'
implementation 'com.google.firebase:firebase-messaging:10.0.1'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso- 
core:3.0.2'
androidTestImplementation 'junit:junit:4.12'
androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'
androidTestImplementation 'org.mockito:mockito-core:2.+'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
}

所有的都不起作用。

如您所见,我正在针对API 23进行操作,因此GrantPermissionRule应该有效。有什么想法吗?我已经卡在这里3天了。 :(

看起来我的帖子主要是代码,我应该添加更多细节...但不确定还需要说什么。

2个回答

6

你所做的几乎正确,只是在执行 shell 命令后应该 "sleep" 一下。下面是你的方法体:

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

1
为什么不使用 getInstrumentation().getUiAutomation().grantRuntimePermission - Thibault

0

Kotlin的方式可以这样实现

@Before
    fun setup() {
        val permission = Manifest.permission.POST_NOTIFICATIONS
         InstrumentationRegistry.getInstrumentation().uiAutomation.grantRuntimePermission(BuildConfig.APPLICATION_ID, permission)
}

如果你将所有权限添加到列表中(就像第一个答案一样),你仍然可以为每个权限添加一个forEach。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接