facebook / screenshot-tests-for-android

Generate fast deterministic screenshots during Android instrumentation tests
http://facebook.github.io/screenshot-tests-for-android
Apache License 2.0
1.74k stars 229 forks source link

Keep screenshots between builds in different flavors #251

Open angelica-oliv opened 4 years ago

angelica-oliv commented 4 years ago

I have 2 flavors in my current project, these flavors have differences on its layout colors, so we would like to validate them with some screenshot tests. But we faced a problem when trying to implement them.

When I run the record command in flavor one with the command: ./gradlew verify<flavor one + build type>AndroidTestScreenshotTest The screenshots are saved on screenshots folder of my project.

The problem occurs when I try to run the command with my flavor two: ./gradlew verify<flavor two + build type>AndroidTestScreenshotTest All my screenshots of flavor one are deleted, and the screenshots of flavor two are added.

I tried to give different titles to these tests that I have to run in each flavor, but it didn’t help.

My current CI is supposed to run the verify command in both flavors, so it would be necessary to keep both screenshots.

xiphirx commented 4 years ago

What version are you using? It should put each flavor's screenshots into a separate different folder, at least thats what I would assume from https://github.com/facebook/screenshot-tests-for-android/blob/master/plugin/src/main/kotlin/com/facebook/testing/screenshot/build/PullScreenshotsTask.kt#L31

angelica-oliv commented 4 years ago

I am using 0.12.0

malkes commented 4 years ago

@xiphirx I'm using the vervion 0.13.0 and facing the same issue.

malkes commented 4 years ago

I figure out how to solve this issue.

You should set the property recordDir.

screenshots {
    def flavor = getCurrentFlavor()
    recordDir = "$projectDir/screenshot_tests/$flavor"
}

Then use the function below to get the name of current flavor The regex is used to get the word between the words record and Debug Reference: https://stackoverflow.com/questions/30621183/how-to-get-current-flavor-in-gradle

def getCurrentFlavor() {
    Gradle gradle = getGradle()
    String taskName = gradle.getStartParameter().getTaskNames().toString()
    Pattern pattern = Pattern.compile("(?<=record).*(?=Debug)")
    Matcher matcher = pattern.matcher(taskName)
    if (matcher.find()) {
        String flavor = matcher.group(0)
        // This makes first character to lowercase.
        char[] c = flavor.toCharArray()
        c[0] = Character.toLowerCase(c[0])
        flavor = new String(c)
        return flavor
    } else {
        return ""
    }
}