微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在运行连接Android测试时启用读/写联系人权限?

任务:

让连接的Android测试在Android M上运行良好.

题:

如何在运行连接Android测试时启用读/写联系人权限?

问题:

我知道pm命令可以启用apk的权限.

adb shell pm grant <PACKAGE_NAME> <PERMISSION_NAME>

我想运行可以在真正的apis和mock apis上运行的测试.如果我无法在gradle DSL中触发pm命令,则出于安全原因,测试代码无法触及真正的api.

我尝试将该步骤添加为connectedAndroidTest(connectedInstrumentTest)任务的第一步.它不适用于目标apk还没有安装.使用错误代码调用命令行.

android.testvariants.all { variant ->
    variant.connectedInstrumentTest.doFirst {
        def adb = android.getAdbExe().toString()
        exec {
            commandLine 'echo',"hello,world testvariants"
        }
        exec {
            commandLine adb,'shell','pm','grant',variant.testedVariant.applicationId,'android.permission.READ_ACCOUNTS'
         }
     }
 }

我尝试添加步骤作为安装任务的最后一步.当我启动connectedAndroidTest时不会调用它.

android.applicationVariants.all { variant ->
    if (variant.getBuildType().name == "debug") {
        variant.install.doLast {
            def adb = android.getAdbExe().toString()

            exec {
                commandLine 'echo',world applicationVariants"
            }
            exec {
                commandLine adb,variant.applicationId,'android.permission.READ_ACCOUNTS'
            }
        }
    }
}

我的计划是在启动测试之前启用权限.我不知道哪个任务是正确的.看起来connectvariantAndroidTest不依赖于installVariant,尽管它们都调用了adb install.

我尝试从测试用例运行pm grant.它按预期失败.

我会接受其他解决方案来运行Android测试.

解决方法

我认为您需要根据installDebug创建自己的任务,然后使connectedDebugAndroidTest依赖于您的任务.

人们这样做是为了禁用动画和工作,你强制安装应用程序并在执行android测试之前授予你特定的权限:

def adb = android.getAdbExe().toString()

task nameofyourtask(type: Exec,dependsOn: 'installDebug') { // or install{productFlavour}{buildType}
    group = 'nameofyourtaskgroup'
    description = 'Describe your task here.'
    def mypermission = 'android.permission.READ_ACCOUNTS'
    commandLine "$adb shell pm grant ${variant.applicationId} $mypermission".split(' ')
}

tasks.whenTaskAdded { task ->
    if (task.name.startsWith('connectedDebugAndroidTest')) { // or connected{productFlavour}{buildType}AndroidTest
        task.dependsOn nameofyourtask
    }
}

您可以将此代码添加到新的yourtask.gradle文件中,并在build.gradle文件底部添加下一行:

apply from: "yourtask.gradle"

并在适当的清单中声明您的许可

<uses-permission android:name="android.permission.READ_ACCOUNTS" />

更新:

修复了与your version for multiple variants一样的commandLine命令,谢谢.

android.applicationVariants.all { variant ->
    if (variant.getBuildType().name == "debug") {
        task "configDevice${variant.name.capitalize()}" (type: Exec){
            dependsOn variant.install

            group = 'nameofyourtaskgroup'
            description = 'Describe your task here.'

            def adb = android.getAdbExe().toString()
            def mypermission = 'android.permission.READ_ACCOUNTS'
            commandLine "$adb shell pm grant ${variant.applicationId} $mypermission".split(' ')
        }
        variant.testvariant.connectedInstrumentTest.dependsOn "configDevice${variant.name.capitalize()}"
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐