gpt4 book ai didi

android - 在 TextInputLayout 中输入一些文本时 Espresso 出错

转载 作者:行者123 更新时间:2023-12-04 23:57:17 25 4
gpt4 key购买 nike

我想测试我的登录 UI,其中包含一些 TextInputLayout 字段,我已经设置了一些代码来运行,但是它崩溃并抛出错误,任何人都可以解决这个问题,我提前感谢

  • 我只想在 TextInputLayout 中输入一些文本
  •     @Test
    fun testCaseSimulateLoginOnButtonClick(){
    onView(withId(R.id.loginEmail)).perform(typeText("xxxxxxxx@gmail.com"))
    onView(withId(R.id.loginPassword)).perform(typeText("123456"))
    onView(withId(R.id.loginBtn)).perform(click())
    onView(withId(R.id.drawer)).check(matches(isDisplayed()))
    }
  • 这是我得到的错误
  • Error performing 'type text(xxxxxxxx@gmail.com)' on view 'view.getId() is <2131362123/com.example.app:id/loginEmail>'.

    最佳答案

    您不能在 TextInputLayout 中键入文本看来,那只是一个容器。 TextInputLayout有一个 FrameLayout作为一个直接的 child ,在那个FrameLayout第一个 subview 是 EditText (您可以输入)
    你可以得到EditText在带有一些额外匹配器的 Espresso 中(找到一个 View ,它是基本 R.id.text_layout 布局的后代,并且类名以 EditText 结尾)。

    onView(
    allOf(
    isDescendantOfA(withId(R.id.text_layout)),
    withClassName(endsWith("EditText"))
    )
    ).perform(
    typeText("Hello world")
    )
    如果你在很多地方都必须这样做,你可以写一个辅助函数
    fun isEditTextInLayout(parentViewId: Int): Matcher<View> {
    return allOf(
    isDescendantOfA(withId(parentViewId)),
    withClassName(endsWith("EditText"))
    )
    }
    并将其用作
    onView(isEditTextInLayout(R.id.text_layout)).perform(typeText("Hello world"))
    对于看起来像的 XML
    <com.google.android.material.textfield.TextInputLayout
    android:id="@+id/text_layout"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Type words here"
    android:layout_margin="16dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" >

    <androidx.appcompat.widget.AppCompatEditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

    </com.google.android.material.textfield.TextInputLayout>
    为此工作所需的一些进口是
    import androidx.test.espresso.matcher.ViewMatchers.*
    import org.hamcrest.Matcher
    import org.hamcrest.Matchers.allOf
    import org.hamcrest.Matchers.endsWith
    当然,您也可以只添加 android:id对于 EditText并得到它也是这样......

    关于android - 在 TextInputLayout 中输入一些文本时 Espresso 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70690820/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com