基本上每次我必须执行一个AndroidTest,利用模拟位置提供程序,我需要手动选中设备模拟器上的框:设置->模拟位置。如何从android测试直接自动化这个任务?有什么办法使用espresso/uiautomator/其他的吗?
我设法以我想要的方式做到了。感谢评论里的链接。我在gradle文件中添加了以下代码片段:
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启动应用程序之前运行任务,你需要在运行之前添加它,编辑"run"配置。