How to set Allow Mock location on Android device before running AndroidTest using uiautomator and espresso?

Basically, every time I have to run AndroidTest, which uses a location provider, I need to manually check the box on the device emulator: Settings → location layout. How to automate this task directly from the Android test? Is there a way to use espresso / uiautomator / something else?

+2
source share
1 answer

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

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. } } 

If you also need to run the task before launching the application through the Android studio, you need to add it, as before starting the edit configuration “run”.

+2
source

Source: https://habr.com/ru/post/1246177/


All Articles