我正在使用 HorizontalPager,它显示一个跳过选项以滚动到最后一页。
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)
}
}
)
....
}
因为这是按预期工作的,所以我正在为这个用例编写 composeTest。
@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()
// Printing the nodes
onAllNodesWithTag("Skip").fetchSemanticsNodes().isEmpty()
onNodeWithText("Last Page").assertIsDisplayed() // Text displayed in last page
但是上面的代码失败并报错,
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))
我认为这是因为
coroutineScope
。我尝试了几个我知道的选项,例如 waitUntil
、waitUntilExists
甚至 Thread.sleep
跳过文本。但它仍然失败。我怀疑Coroutines
并且无法找到解决方法。
打印节点结果,
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]
正如人们所看到的,它打印出
First Page
,但我希望它打印出Last Page
。任何帮助表示赞赏。谢谢