📸
AndroidUiTestingUtils
  • Overview
  • Setup
    • Instrumentation setup
    • Robolectric setup
    • Cross-Library setup
  • Usage
    • Instrumentation tests usage
      • Android-Testify usage
    • Robolectric usage
    • Cross-Library usage
    • TestRules & other utils
  • Ready-to-Run Samples
  • Github
Powered by GitBook
On this page
  • Android-Testify 3.x.x
  • Android-Testify 2.x.x
  • Activity
  • Fragment
  • Android View
  • Jetpack Compose
  1. Usage
  2. Instrumentation tests usage

Android-Testify usage

PreviousInstrumentation tests usageNextRobolectric usage

Last updated 8 months ago

Android-Testify 3.x.x

Since Android-Testify 3.0.0, it supports ActivityScenarios! That means, you could use AndroidUiTestingUtils ActivityScenarios for Views, Composable, etc. together with Android-Testify 3.x.x.

Check some examples in .

Android-Testify 2.x.x

Android-Testify 2.x.x does not support ActivityScenario but the deprecated ActivityTestRule. So, in order to generate screenshot under different configurations, you'd need to resort to the TestRules described in . Nevertheless, AndroidUiTestingUtils provides optimized ScreenshotTestRules to leverage Android-Testify tests for Fragments, Android Views and Jetpack Compose. These are used in the upcoming examples. In order to access them, add the following dependencies

// Available from version 2.1.0+
androidTestImplementation 'com.github.sergio-sastre.AndroidUiTestingUtils:utils:<version>'
androidTestImplementation 'com.github.sergio-sastre.AndroidUiTestingUtils:android-testify:<version>'

Activity

Use AndroidUiTesting with the standard Android-Testify ScreenshotRule

Fragment

@get:Rule
val screenshotRule =
    ScreenshotRuleWithConfigurationForFragment(
        exactness = 0.85f,
        fragmentClass = MyFragment::class.java,
        fragmentArgs = bundleOf("key" to "value"),
        config = FragmentConfigItem(
            locale = "ar_XB",
            theme = R.style.myCustomTheme,
            uiMode = UiMode.NIGHT,
            fontSize = FontSize.SMALL,
            displaySize = DisplaySize.SMALL,
            orientation = Orientation.LANDSCAPE
        ),
    )

@ScreenshotInstrumentation
@Test
fun snapFragment() {
    screenshotRule
        .withExperimentalFeatureEnabled(GenerateDiffs)
        .waitForIdleSync()
        .assertSame(name = "nameOfMyScreenshot")
}

Android View

for a View Holder, for instance

@get:Rule
var screenshotRule = 
    ScreenshotRuleWithConfigurationForView(
        exactness = 0.85f,
        config = ViewConfigItem(
            uiMode = UiMode.DAY,
            locale = "en",
            orientation = Orientation.PORTRAIT
        ),
    )

@ScreenshotInstrumentation
@Test
fun snapMemoriseViewHolderHappyPath() {
    screenshotRule
        .setTargetLayoutId(R.layout.memorise_row)
        .setViewModifications { targetLayout ->
            MemoriseViewHolder(
                container = targetLayout,
                itemEventListener = null,
                animationDelay = 0L
            ).apply {
                bind(
                    generateMemoriseItem(
                        rightAligned = false,
                        activity = screenshotRule.activity
                    )
                )
            }
        }
        .setScreenshotFirstView() // this is to screenshot the view only
        .withExperimentalFeatureEnabled(GenerateDiffs)
        .waitForIdleSync()
        .assertSame(name = "nameOfMyScreenshot")
}

Jetpack Compose

@get:Rule
val screenshotRule = 
    ComposableScreenshotRuleWithConfiguration(
        exactness = 0.85f,
        config = ComposableConfigItem(
            locale = "en",
            uiMode = UiMode.DAY,
            fontSize = FontSize.NORMAL,
            displaySize = DisplaySize.NORMAL,
            orientation = Orientation.PORTRAIT
        )
    )

@ScreenshotInstrumentation
@Test
fun snapComposable() {
    screenshotRule
        .setCompose { myComposable() }
        .withExperimentalFeatureEnabled(GenerateDiffs)
        .assertSame(name = "nameOfMyScreenshot")
}
this repo
this section
TestRules