android - 在使用 uiautomator 和 espresso 执行 AndroidTest 之前,如何在 Android 设备上设置允许模拟位置?

标签 android testing location android-espresso

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

最佳答案

我设法以我想要的方式做到了。感谢评论中发布的链接。我在我的 gradle 文件中添加了以下 fragment :

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 启动应用程序之前运行任务,您需要像运行前一样添加它,编辑“运行”配置。

关于android - 在使用 uiautomator 和 espresso 执行 AndroidTest 之前,如何在 Android 设备上设置允许模拟位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39753097/

相关文章:

Android 单元测试 - startActivity() 导致应用程序/测试崩溃

ios - iOS 上的 GPS,不同手机之间的准确度如何?

javascript - 如果使用 loadUrl() 而不是 evaluateJavascript() 在 webView 中启动,则 JS 代码有效

android - 播放音频时,Android SeekBar不会消失

使用 Dagger 模拟注入(inject)构造函数的 Android 测试

unit-testing - 没有方法的类应该被测试?

testing - 如何使用 groovy 在 geb 中选择组合框

ios - 谷歌地图位置更新

iOS 位置管理器比每秒更新一次更频繁

android - 将粘性标题添加到视差滚动 - android