the*_*e64 9 android android-testing android-instrumentation android-jetpack-compose
我有一个Composable有一个Text和Button。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")
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我测试的方法
@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()
}
Run Code Online (Sandbox Code Playgroud)
但是当我运行测试以查看文本是否更新时,出现以下错误。(手动测试有效)
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)
Run Code Online (Sandbox Code Playgroud)
如果您想自己尝试一下,这里是完整的测试文件。
问题
Pri*_*čič 24
测试时屏幕亮着吗?
就我而言,只需关闭屏幕即可轻松重现。请注意,我使用的是模拟器。
| 归档时间: |
|
| 查看次数: |
4198 次 |
| 最近记录: |