Issue#100 Create TestRule Saving Screenshots on UI Test failure

This commit is contained in:
Gergely Hegedus 2022-07-13 11:51:10 +03:00
parent 45bcd20b2a
commit ca2dff2304
11 changed files with 190 additions and 7 deletions

View file

@ -0,0 +1,16 @@
package org.fnives.test.showcase.android.testutil.activity
import androidx.test.core.app.ActivityScenario
import org.junit.rules.TestWatcher
import org.junit.runner.Description
/**
* Test Rule which closes the given [scenario] safely when the Test is finished.
*/
class SafeCloseActivityRule(val scenario: () -> ActivityScenario<*>) : TestWatcher() {
override fun finished(description: Description) {
super.finished(description)
scenario().safeClose()
}
}

View file

@ -0,0 +1,68 @@
package org.fnives.test.showcase.android.testutil.screenshot
import android.graphics.Bitmap
import androidx.test.runner.screenshot.ScreenCapture
import androidx.test.runner.screenshot.ScreenCaptureProcessor
import androidx.test.runner.screenshot.Screenshot
import org.junit.rules.TestWatcher
import org.junit.runner.Description
import java.io.IOException
class ScreenshotRule(
private val prefix: String = "",
private val takeBefore: Boolean = false,
private val takeOnSuccess: Boolean = false,
private val takeOnFailure: Boolean = true,
private val timestampSuffix: Boolean = true,
private val processor: ScreenCaptureProcessor = basicScreenCaptureProcessor(),
) : TestWatcher() {
override fun starting(description: Description) {
super.starting(description)
if (takeBefore) {
takeScreenshot(baseName = description.beforeTestScreenshotName)
}
}
override fun failed(e: Throwable?, description: Description) {
super.failed(e, description)
if (takeOnFailure) {
takeScreenshot(baseName = description.failTestScreenshotName)
}
}
override fun succeeded(description: Description) {
super.succeeded(description)
if (takeOnSuccess) {
takeScreenshot(baseName = description.successTestScreenshotName)
}
}
fun takeScreenshot(prefix: String = this.prefix, baseName: String) {
val fileName = if (timestampSuffix) {
"$prefix-$baseName-${System.currentTimeMillis()}"
} else {
"$prefix-$baseName"
}
takeScreenshot(filename = fileName)
}
@Suppress("PrintStackTrace")
private fun takeScreenshot(filename: String) {
val capture: ScreenCapture = Screenshot.capture()
capture.name = filename
capture.format = Bitmap.CompressFormat.JPEG
try {
capture.process(setOf(processor))
} catch (e: IOException) {
e.printStackTrace()
}
}
companion object {
val Description.testScreenshotName get() = "${testClass.simpleName}-$methodName"
val Description.beforeTestScreenshotName get() = "$testScreenshotName-BEFORE"
val Description.successTestScreenshotName get() = "$testScreenshotName-SUCCESS"
val Description.failTestScreenshotName get() = "$testScreenshotName-FAIL"
}
}

View file

@ -0,0 +1,7 @@
@file:Suppress("PackageDirectoryMismatch")
package androidx.test.runner.screenshot
import java.io.File
fun basicScreenCaptureProcessor(file: File) = BasicScreenCaptureProcessor(file)

View file

@ -0,0 +1,12 @@
package org.fnives.test.showcase.android.testutil.screenshot
import android.os.Environment
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.runner.screenshot.basicScreenCaptureProcessor
import java.io.File
fun basicScreenCaptureProcessor(subDir: String = "test-screenshots") =
basicScreenCaptureProcessor(File(getTestPicturesDir(), subDir))
fun getTestPicturesDir() =
InstrumentationRegistry.getInstrumentation().targetContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES)