[英]Getting "No compose views found in the app. Is your Activity resumed?"
我有一個具有Text
和Button
的Composable
。 如果當前方向是縱向, Text
將顯示P
,否則顯示L
單擊Button
會將方向更改為橫向,(之后,它應該將文本從P
更改為L
)
這是可組合的
@Composable
fun MyApp() {
val currentOrientation = LocalConfiguration.current.orientation
val orientation = if (currentOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
"P"
} else {
"L"
}
val activity = LocalContext.current as Activity
Column {
Text(text = orientation)
Button(onClick = {
// change orientation to landscape
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
}) {
Text(text = "DO IT")
}
}
}
這是我如何測試它
@get:Rule
val composeRule = createComposeRule()
@Test
fun test() {
composeRule.setContent { MyApp() }
// Starts with portrait
composeRule.onNodeWithText("P").assertIsDisplayed()
// Change the orientation to Landscape
composeRule.onNodeWithText("DO IT").performClick()
// Now the text should be `L`
composeRule.onNodeWithText("L").assertIsDisplayed()
}
但是當我運行測試以查看文本是否更新時出現以下錯誤。 (盡管手動測試有效)
java.lang.IllegalStateException: No compose views found in the app. Is your Activity resumed?
at androidx.compose.ui.test.TestContext.getAllSemanticsNodes$ui_test_release(TestOwner.kt:96)
at androidx.compose.ui.test.SemanticsNodeInteraction.fetchSemanticsNodes$ui_test_release(SemanticsNodeInteraction.kt:82)
at androidx.compose.ui.test.SemanticsNodeInteraction.fetchOneOrDie(SemanticsNodeInteraction.kt:155)
at androidx.compose.ui.test.SemanticsNodeInteraction.assertExists(SemanticsNodeInteraction.kt:147)
at androidx.compose.ui.test.SemanticsNodeInteraction.assertExists$default(SemanticsNodeInteraction.kt:146)
如果您想自己嘗試,這里有完整的測試文件。
問題
我在執行點擊事件時遇到了同樣的問題。 在我使用我的設備進行測試時,當您的測試設備/模擬器的屏幕未喚醒(關閉)時發生。
我的修復只是打開屏幕。
測試期間屏幕是否打開?
就我而言,只需關閉屏幕即可輕松重現。 請注意,我正在使用模擬器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.