android-testify icon indicating copy to clipboard operation
android-testify copied to clipboard

Provide a standard way to assert multiple screenshots in a single test

Open DanielJette opened this issue 3 years ago • 5 comments

Provide a way to assert multiple screenshots in a single test case. This would allow users to iterate over a list of "test cases" and generate screenshots for each test.

cc @gantonious ported from https://github.com/Shopify/android-testify/issues/126

DanielJette avatar Aug 05 '22 21:08 DanielJette

This is a pretty crucial feature for us to be able to switch to this library. Hoping that this would be included soon. 🤞

findjigar avatar Jan 27 '23 15:01 findjigar

Hey @findjigar thanks for the feedback!

Would you mind providing more details on your requirements? I really hope to offer this feature very soon, but I would like to ensure that it meets your needs.

Do you have any thought about what the API should look like? I was thinking something like supporting multiple calls to assertSame() in the same test. Something like this:

// Take first screenshot
screenshotRule
    .setViewModifications { ... }
    .setEspressoActions { ... }
    .assertSame()

// Take second screenshot
screenshotRule
    .setViewModifications { ... }
    .setEspressoActions { ... }
    .assertSame()

DanielJette avatar Jan 27 '23 16:01 DanielJette

@DanielJette

Sure. We have lots of UI tests where each tests capture/compare multiple screenshots. Two primary use cases are,

  • Taking screenshot of top and bottom of the screen since a lot of screens are scrollable
    • We take a top screenshot, then scroll to bottom and take another screenshot.
    • Doing this in two separate tests would take a lot of time and we would have to navigate to a particular screen again. Sometimes a screen can be 4-5 flow down the chain so we try to take multiple screenshots in single test.
  • We have feature flagging for UI components, so we have tests where we would have a List of cases (each case has a unique visibility toggle), and then we would iterate on each case and take screenshot.

Another important part of this feature would be the ability to provide unique file name since there are multiple screenshots per test.

For example, our code looks like this,

class DemoTest {

     // a handy JUnit rule that stores the method name, so it can be used to generate unique
    // screenshot files per test method
    @get:Rule
    var nameRule = org.junit.rules.TestName()

    @Test
    fun screenContent_withOptional() {
        // goToParticularSreen()

        captureScreenshotAndCompare("top")
        // Scroll to Bottom
        captureScreenshotAndCompare("bottom")
    }
    
    private fun captureScreenshotAndCompare(fileNameSuffix: String) {
        val screenshotName = "${javaClass.simpleName}_${nameRule.methodName}_$name"
        // assertScreenshot(screenshotName)
    }
}

findjigar avatar Jan 27 '23 17:01 findjigar

@findjigar Great stuff! Thanks for the details.

DanielJette avatar Jan 27 '23 17:01 DanielJette