I'm using HorizontalPager which shows a Skip option to scroll to last page.
val pagerState = rememberPagerState()
val coroutineScope = rememberCoroutineScope()
ConstraintView {
val (skipText, pager) = createRefs()
....
Text(
text = stringResource(id = R.string.skip),
modifier = Modifier
.constrainAs(skipText) {
....
}
.clickable {
coroutineScope.launch {
pagerState.scrollToPage(page = pageCount - 1)
}
}
)
....
}
As this is working as expected, I'm writing composeTest for this use case.
@get:Rule
val composeTestRule = createComposeRule()
@Before
fun setup() {
composeTestRule.setContent {
content()
}
}
@Test
fun testOnClickSkipNavigateToLastPageInNodeTree() = {
composeTestRule.apply {
onNodeWithText("First Page").assertIsDisplayed() // Text displayed in first page
onNodeWithText("Skip").assertIsDisplayed()
onNodeWithText("Skip").performClick()
onRoot(useUnmergedTree = true).printToLog("TAG") // Printing the nodes
onNodeWithText("Last Page").assertIsDisplayed() // Text displayed in last page
But the above code fails with error,
java.lang.AssertionError: Failed to perform isDisplayed check.
Reason: Expected exactly '1' node but could not find any node that satisfies: (Text + EditableText contains 'Last Page' (ignoreCase: false))
I assume this is because of the coroutineScope
. I tried few options that I'm aware of such as waitUntil
, waitUntilExists
and even Thread.sleep
on Skip text. But still it fails. I doubt the Coroutines
and unable to find how to resolve this.
Printing the nodes results,
Node #1 at (l=0.0, t=279.0, r=1080.0, b=2141.0)px
|-Node #2 at (l=0.0, t=279.0, r=1080.0, b=2141.0)px
DesignInfoProvider = 'androidx.constraintlayout.compose.Measurer@96f3373'
|-Node #4 at (l=909.0, t=279.0, r=1080.0, b=419.0)px
| Focused = 'false'
| Text = '[Skip]'
| Actions = [OnClick, RequestFocus, GetTextLayoutResult]
| MergeDescendants = 'true'
|-Node #5 at (l=0.0, t=349.0, r=1080.0, b=2211.0)px
HorizontalScrollAxisRange = 'ScrollAxisRange(value=0.0, maxValue=4.0, reverseScrolling=false)'
CollectionInfo = 'androidx.compose.ui.semantics.CollectionInfo@3f82230'
Actions = [IndexForKey, ScrollBy, ScrollToIndex]
|-Node #12 at (l=225.0, t=1069.0, r=856.0, b=1209.0)px
| Text = ‘[First Page]’
| Actions = [GetTextLayoutResult]
As one can see it prints First Page
but I expect it to print Last Page
. Any help is appreciated. Thank you
UPDATE: This test passes in pixel emulator but fails in pixel device