在使用uiautomator和espresso执行AndroidTest之前,如何在Android设备上设置“允许模拟位置"? [英] How to set Allow Mock Location on Android Device before executing AndroidTest with uiautomator and espresso?

查看:171
本文介绍了在使用uiautomator和espresso执行AndroidTest之前,如何在Android设备上设置“允许模拟位置"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,每次我必须执行一个利用模拟位置提供程序的AndroidTest时,我都需要手动选中设备模拟器上的框:设置->模拟位置.如何直接从android测试自动执行此任务?有什么办法可以使用espresso/uiautomator/其他东西吗?

Basically every time I have to execute an AndroidTest that makes use of mock location provider I need to manually check the box on device emulator: Settings--> mock locations. How to automate this task directly from the android test? Is there any way using espresso/uiautomator/something else?

推荐答案

我设法以自己想要的方式做到了这一点.感谢张贴在评论上的链接.我在gradle文件中添加了以下代码段:

I managed to do that in the way I wanted. Thanks to the links posted on comments. I added in my gradle file the following snippet:

task enableMockLocationForTestsOnDevice(type: Exec) {
    Properties properties = new Properties()
    properties.load(project.rootProject.file('local.properties').newDataInputStream())
    def sdkDir = properties.getProperty('sdk.dir')
    def adb = "$sdkDir/platform-tools/adb"
    description 'enable mock location on connected android device before executing any android test'
    commandLine "$adb", 'shell', 'appops', 'set', 'indian.fig.whatsaround', 'android:mock_location', 'allow'
}

afterEvaluate {
    // Note: the app must be already installed on device in order to this to run!
    connectedDebugAndroidTest.dependsOn enableMockLocationForTestsOnDevice
    connectedAndroidTest.dependsOn enableMockLocationForTestsOnDevice
}


// execute android tests before realising a new apk
tasks.whenTaskAdded { task ->
    if (task.name == 'assembleRelease') {
        task.dependsOn('enableMockLocationForTestsOnDevice')
        task.dependsOn('testReleaseUnitTest') // Run unit tests for the release build
        task.dependsOn('connectedAndroidTest') // Installs and runs instrumentation tests for all flavors on connected devices.

    }
}

如果您还需要在通过android studio启动应用程序之前运行任务,则需要像运行编辑运行"配置之前一样添加它.

If you also need to run the task before launching the app via android studio you need to add it as before run editing the "run" configuration.

这篇关于在使用uiautomator和espresso执行AndroidTest之前,如何在Android设备上设置“允许模拟位置"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆