dropbox / dropshots

Easy on-device screenshot testing for Android.
Apache License 2.0
256 stars 17 forks source link

Compatibility with Gradle Managed Devices #31

Open saket opened 1 year ago

saket commented 1 year ago

Dropshots doesn't seem to be able to generate snapshots when tests are run on managed devices. This can be reproduced in the sample app. Any ideas what might be happening?

saket commented 1 year ago

An official sample project from Google has some answers:

https://github.com/android/testing-samples/blob/dc7a21f08125c4293e5b970dd42ae31e1c6ae933/ui/espresso/ScreenshotSample/app/src/androidTest/java/com/example/android/testing/espresso/screenshotsample/ScreenshotTest.kt#L21

CodyEngel commented 1 year ago

Are there any updates on this? It looks like the screenshots location for Gradle Managed Devices will differ from where Dropshots stores screenshots BUT from the linked Google project it does look like screenshots work.

Would it be possible to configure the gradle plugin to check for a different output folder to retrieve the screenshots?

ajesh-n commented 2 months ago

Based on the answer here Storing the result in Android/media/<Bundle Identifier>/additional_test_output will fix this problem

 private fun writeImage(name: String, filePath: String?, screenshot: Bitmap): String {
    val externalStorageDir = Environment.getExternalStoragePublicDirectory("Android")
    val screenFolder = File(externalStorageDir, "/media/${targetContext.packageName}/additional_test_output/screenshots".appendPath(filePath))
    if (!screenFolder.exists() && !screenFolder.mkdirs()) {
      throw IllegalStateException("Unable to create screenshot storage directory.")
    }

    val file = File(screenFolder, "${name.replace(" ", "_")}.png")
    file.outputStream().use {
      screenshot.compress(Bitmap.CompressFormat.PNG, 100, it)
    }
    return file.absolutePath
  }