Skip to content

Instantly share code, notes, and snippets.

@hernandazevedo
Last active April 21, 2022 15:52
Show Gist options
  • Save hernandazevedo/5b2b1d68690a9a8cc3ce789c6e9f1607 to your computer and use it in GitHub Desktop.
Save hernandazevedo/5b2b1d68690a9a8cc3ce789c6e9f1607 to your computer and use it in GitHub Desktop.
Snapshots tests for android for a remote device such as Firebase Test Lab (Bitrise)
apply plugin: 'shot'
/*
Put this on any module you want to test with snapshots:
apply from: "../screenshots.gradle"
Put this on your root build.gradle
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.karumi:shot:3.1.0'
}
}
Here some example of instrumentation test:
@RunWith(AndroidJUnit4::class)
class MainActivityTest {
@Rule
@JvmField
val rule = ActivityTestRule(MainActivity::class.java, true, false)
@Test
fun testSnapshot() {
rule.launchActivity(Intent())
Screenshot.snapActivity(rule.activity)
.record()
}
}
class SampleTestRunner : AndroidJUnitRunner() {
override fun onCreate(args: Bundle) {
ScreenshotRunner.onCreate(this, args)
super.onCreate(args)
}
override fun finish(resultCode: Int, results: Bundle) {
ScreenshotRunner.onDestroy()
super.finish(resultCode, results)
}
}
* `./gradlew executeScreenshotTests -Precord` - Executes snapshots recording at folder `"${projectDir}/screenshots/`
* `./gradlew executeScreenshotTests` - Verifies if the current screens against the previous snapshots at folder `"${projectDir}/screenshots/` using the current connected device
* `./gradlew copyVerificationFiles executeScreenshotTests -PrunInstrumentation=false` - Verifies if the current screens against the previous snapshots at folder `"${projectDir}/screenshots/` without using the current connected device.
In that case the current screenshots(that may be generated on a remote device) must be copied to the folder `"${projectDir}/screenshots/screenshots-default`
*/
ext {
localProp = new Properties()
fileName = 'local.properties'
if (project.rootProject.file(fileName).exists()) {
localProp.load(new FileInputStream(rootProject.file(fileName)))
}
screenShotsRecordedBaseDir =
System.env.VDTESTING_DOWNLOADED_FILES_DIR != null ?
"${System.env.VDTESTING_DOWNLOADED_FILES_DIR}" :
getArgument("screenShotsRecordedBaseDir", "${project.buildDir}/reports/shot/record/images/recorded")
screenShotsVerificationBaseDir =
System.env.SCREENSHOTS_VERIFICATION_BASE_DIR != null ?
System.env.SCREENSHOTS_VERIFICATION_BASE_DIR :
getArgument("screenShotsVerificationBaseDir", "${projectDir}/screenshots/screenshots-default")
}
shot {
appId = 'your.package.id'
runInstrumentation = Boolean.valueOf(getArgument("runInstrumentation", "true"))
}
project.afterEvaluate {
task copyVerificationFiles() {
doFirst {
println("screenShotsRecordedBaseDir=${screenShotsRecordedBaseDir}")
println("screenShotsVerificationBaseDir=${screenShotsVerificationBaseDir}")
}
fileTree(dir: "${screenShotsRecordedBaseDir}")
.filter { f ->
f.name.contains("_screenshots-default_")
}.each { foundFile ->
def nameWithoutPrefix = foundFile.name.split("_screenshots-default_")[1]
copy {
from "${foundFile.path}"
into "${screenShotsVerificationBaseDir}"
rename { String fileName ->
fileName.replace(foundFile.name, nameWithoutPrefix)
}
}
}
}
//This ensures that the screenshots are recorded when running on Firebase test lab
project.tasks.connectedDebugAndroidTest.finalizedBy project.tasks.executeScreenshotTests
}
private def getArgument(prop, defaultValue) {
def r = project.hasProperty(prop) ? project.getProperties().get(prop) : defaultValue
println("${prop}=${r}")
return r
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment