Search code examples
androidgradleandroid-uiautomator

Run UI automation tests with gradle without uninstalling


When I run instrumentation tests from within Android Studio, I see that the app remains on the device afterwards. But I can't figure out to do this from the command line with gradlew. My intention is to run tests that save screenshots in e.g /data/data/MyApp/cache/screenshots and download these with adb pull afterwards.

./gradlew connectedAndroidTest

causes the app to be uninstalled. I also tried

./gradlew connectedAndroidTest -x uninstallAndroidTest

but that didn't make any difference. What's causing the uninstallation, and how can I avoid it?


Solution

  • I solved this by letting gradle only build the apk, and then handling the install/test/uninstall work with adb. Here's an approximation of my script.

    PKGNAME=com.corp.app
    ./gradlew assembleAndroidTest
    adb install -r app/build/outputs/apk/app-debug.apk
    adb install -r app/build/outputs/apk/app-debug-androidTest-unaligned.apk
    
    adb shell am instrument -w ${PKGNAME}.test/android.support.test.runner.AndroidJUnitRunner
    
    [ -d screenshots ] || mkdir screenshots
    adb pull /data/data/${PKGNAME}/cache/screenshots screenshots
    
    # Now we can uninstall.
    adb uninstall ${PKGNAME}.test
    adb uninstall ${PKGNAME}