简体   繁体   English

使用 JetpackCompose LazyColumn,Compose-Espresso 链接变为空闲超时

[英]Compose-Espresso link to become idle timed out With JetpackCompose LazyColumn

I am trying to test my lazy column in JetpackCompose and I keep getting this error: [Compose-Espresso link] to become idle timed out我正在尝试在 JetpackCompose 中测试我的惰性列,但我不断收到此错误:[Compose-Espresso 链接] 变得空闲超时

I tried using composeTestRule.waitforIdle() but it doesn't work.我尝试使用 composeTestRule.waitforIdle() 但它不起作用。 What am I missing here?我在这里想念什么?

@HiltAndroidTest
class MainTest {

    @get:Rule(order = 1)
    var hiltTestRule = HiltAndroidRule(this)

    @get:Rule(order = 2)
    var composeTestRule = createAndroidComposeRule<MainActivity>()

    private val context = InstrumentationRegistry.getInstrumentation().context

    @Before
    fun setup() {
        hiltTestRule.inject()
        composeTestRule.setContent {
            HomePage(
                context = context,
            viewModel = composeTestRule.activity.viewModels<MarvelViewModel>().value,
            onClick = {}
            )
        }
        composeTestRule.onRoot().printToLog("currentLabelExists")

    }


    @Test
    fun isResultDisplayedOnLazyColumn() {
        composeTestRule.waitForIdle()
        composeTestRule.onNode(hasImeAction(ImeAction.Done)).performTextInput("iron man")
        composeTestRule.onNode(hasImeAction(ImeAction.Done)).performImeAction()

        composeTestRule.onNodeWithTag(TAG_LAZY_COLUMN, useUnmergedTree = true).assertIsDisplayed()

I've got this issue too.我也有这个问题。 The root cause was having my composeview GONE.根本原因是我的 composeview 消失了。 It seems ComposeTestRule IdlingResource don't like then you call setContent on a Gone ComposeView似乎 ComposeTestRule IdlingResource 不喜欢然后你在 Gone ComposeView 上调用 setContent

After several hours of trials and experimentation, l found a way to fix this issue in a recent project.经过几个小时的试验和实验,我在最近的一个项目中找到了解决这个问题的方法。 I would advise to try the following:我建议尝试以下方法:

Update compose version to 1.2.0-Alpha06 as Skav mentioned.如 Skav 所述,将 compose 版本更新为1.2.0-Alpha06 For me that implies bumping my kotlin version to 1.7.0 which my team is not ready for.对我来说,这意味着将我的 kotlin 版本升级到 1.7.0,而我的团队还没有准备好。

So I did some digging around the IdlingRegistry and found that unregistering Espresso-Compose Idling resource at the right spot in the test fixed the issue and my UI test passed afterwards.所以我对IdlingRegistry进行了一些挖掘,发现在测试中的正确位置取消注册Espresso-Compose Idling 资源解决了这个问题,之后我的 UI 测试通过了。

IdlingRegistry.getInstance().resources.forEach {
   Timber.e("resource ${it.name}    idleNow: ${it.isIdleNow}")
   if( it.name == "Compose-Espresso link" ) {
       IdlingRegistry.getInstance().unregister(it)
   }
}

It is important to note that unregistering Espresso-Compose too early can make your compose ui to fail to display.请务必注意,过早取消注册 Espresso-Compose 会使您的 compose ui 无法显示。 In my case, l ran this code after the UI is ready before testing for specific UI functionalities.就我而言,我在 UI 准备好之后运行此代码,然后再测试特定的 UI 功能。

Building on top of the answer from @leeCoder, I've discovered that upgrading to Compose 1.2.0 ( alpha no longer necessary ) fixed my problem and there was no need to unregister anything (which was the part that made me the most nervous about his answer).在@leeCoder的回答之上,我发现升级到Compose 1.2.0不再需要 alpha )解决了我的问题,并且不需要注销任何东西(这是让我最紧张的部分他的回答)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM