我正在尝试使用 BDD + Compose 编写 UI 测试,但我不断收到以下错误:
测试设置不正确。在测试中使用 ComposeTestRule 以便能够与可组合项交互
我已经尝试了
createComposeRule
和createAndroidComposeRule
:
@RunWith(AndroidJUnit4::class)
class LoginSteps {
@get:Rule
val composeTestRule = createAndroidComposeRule<MainActivity>()
@Given("^I am on the signup page$")
fun I_am_on_the_signup_page() {
// the following line produces the error:
composeTestRule.onNodeWithTag("logo").assertIsDisplayed()
}
@When("^I click the login button$")
fun when_I_click_login() {
composeTestRule.onNodeWithTag("loginBtn").assertIsDisplayed().performClick()
}
@Then("^I should see the login page$")
fun See_the_Sign_in_page() {
composeTestRule.onNodeWithTag("LoginWelcomemsg").assertIsDisplayed()
}
}
完整的堆栈跟踪:
java.lang.IllegalStateException: Test not setup properly. Use a ComposeTestRule in your test to be able to interact with composables
at androidx.compose.ui.test.junit4.ComposeRootRegistry_androidKt.ensureComposeRootRegistryIsSetUp(ComposeRootRegistry.android.kt:254)
at androidx.compose.ui.test.junit4.ComposeRootRegistry_androidKt.waitForComposeRoots(ComposeRootRegistry.android.kt:261)
at androidx.compose.ui.test.AndroidComposeUiTestEnvironment.waitForIdle(ComposeUiTest.android.kt:338)
at androidx.compose.ui.test.AndroidComposeUiTestEnvironment.access$waitForIdle(ComposeUiTest.android.kt:230)
at androidx.compose.ui.test.AndroidComposeUiTestEnvironment$AndroidTestOwner.getRoots(ComposeUiTest.android.kt:533)
at androidx.compose.ui.test.TestContext.getAllSemanticsNodes$ui_test_release(TestOwner.kt:86)
at androidx.compose.ui.test.SemanticsNodeInteraction.fetchSemanticsNodes$ui_test_release(SemanticsNodeInteraction.kt:63)
at androidx.compose.ui.test.SemanticsNodeInteraction.fetchSemanticsNodes$ui_test_release$default(SemanticsNodeInteraction.kt:56)
at androidx.compose.ui.test.AndroidAssertions_androidKt.checkIsDisplayed(AndroidAssertions.android.kt:29)
at androidx.compose.ui.test.AssertionsKt.isDisplayed(Assertions.kt:359)
at androidx.compose.ui.test.AssertionsKt.assertIsDisplayed(Assertions.kt:33)
at uk.co.topcashback.topcashback.qa.steps.login.LoginSteps.I_am_on_the_dual_page(LoginSteps.kt:61)
at ✽.I am on the dual page(file:/features/login.feature:4)
您似乎正在尝试使用步骤类中的测试规则。 相反,请将其移至您的测试类并尝试。 测试规则属于测试类。