Id of Jetpack composable for autotesting

107 Views Asked by At

In my company's Android project we have several screens implemented using Jetpack Compose. For autotesting purposes I have to provide some kind of resource-id for each composable element used on specific screen. In android xml we have @id field for that. How to implement the same id reference to the composable(s) on the specific screen for autotesting? Many thanks for your upcoming answers! The following is the example of the target screen:

@SuppressLint("UnusedMaterialScaffoldPaddingParameter")
@Composable
internal fun ThankYouPageUi(
    activity: FragmentActivity?,
    viewModel: ThankYouPageViewModel,
    initParams: ThankYouPageInitParams,
    backgroundColor: Int,
) {
    when (val state = viewModel.state.collectAsStateWithLifecycle().value) {
        is ThankYouPageState.Content -> {
            val thankYouPageStepInfo = state.data
            val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.lottie_confetti))

            Scaffold(
                backgroundColor = colorResource(id = backgroundColor),
                topBar = {
                    TopAppBar(activity, viewModel, initParams, backgroundColor)
                },
                bottomBar = {
                    BottomBar(viewModel, thankYouPageStepInfo)
                },
            ) {
                Column(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(horizontal = 16.dp).testTag("column"),
                ) {
                    Image(
                        modifier = Modifier
                            .fillMaxWidth()
                            .fillMaxHeight(0.45f)
                            .padding(vertical = 8.dp),
                        painter = painterResource(id = thankYouPageStepInfo.imageDrawableRes),
                        contentDescription = "",
                    )
                    Text(
                        text = thankYouPageStepInfo.title,
                        color = colorResource(id = R.color.primary_label),
                        style = h2TextStyle,
                        modifier = Modifier
                            .h2BaseLinePadding()
                            .fillMaxWidth()
                            .padding(top = 24.dp),
                        textAlign = TextAlign.Center,
                    )
                    thankYouPageStepInfo.subtitle?.let { subtitle ->
                        Text(
                            text = subtitle,
                            color = colorResource(id = R.color.primary_label),
                            style = subtitle1TextStyle,
                            modifier = Modifier
                                .subtitle1BaseLinePadding()
                                .fillMaxWidth()
                                .padding(top = 12.dp),
                            textAlign = TextAlign.Center,
                        )
                    }
                    Text(
                        text = thankYouPageStepInfo.body,
                        color = colorResource(id = R.color.secondary_label),
                        style = body1TextStyle,
                        modifier = Modifier
                            .body1BaseLinePadding()
                            .fillMaxWidth()
                            .padding(top = 12.dp),
                        textAlign = TextAlign.Center,
                    )
                    if (thankYouPageStepInfo.showConfetti) {
                        LottieAnimation(
                            composition,
                            modifier = Modifier
                                .fillMaxSize(),
                        )
                    }
                }
            }
        }

        else -> Unit
    }
}

I tried to use something like Modifier().///.///.testTag("myTag") in my composables - but that's not a desired solution

0

There are 0 best solutions below