Tak*_*aki 5 android kotlin android-espresso
我想测试我的登录 UI,其中包含一些 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()))
}
Run Code Online (Sandbox Code Playgroud)
Error performing 'type text(xxxxxxxx@gmail.com)' on view 'view.getId() is <2131362123/com.example.app:id/loginEmail>'.
Run Code Online (Sandbox Code Playgroud)
您无法在TextInputLayout
视图中键入文本,它只是一个容器。TheTextInputLayout
具有 aFrameLayout
作为直接子视图,并且FrameLayout
第一个子视图是EditText
(您可以在其中键入)
您可以使用EditText
一些额外的匹配器来获取 Espresso 中的 (找到一个视图,该视图是基本布局的后代R.id.text_layout
,并且具有以 结尾的类名EditText
)。
onView(
allOf(
isDescendantOfA(withId(R.id.text_layout)),
withClassName(endsWith("EditText"))
)
).perform(
typeText("Hello world")
)
Run Code Online (Sandbox Code Playgroud)
如果你必须在很多地方这样做,你可以编写一个辅助函数
fun isEditTextInLayout(parentViewId: Int): Matcher<View> {
return allOf(
isDescendantOfA(withId(parentViewId)),
withClassName(endsWith("EditText"))
)
}
Run Code Online (Sandbox Code Playgroud)
并将其用作
onView(isEditTextInLayout(R.id.text_layout)).perform(typeText("Hello world"))
Run Code Online (Sandbox Code Playgroud)
对于看起来像这样的 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>
Run Code Online (Sandbox Code Playgroud)
要使其工作所需的一些导入是
import androidx.test.espresso.matcher.ViewMatchers.*
import org.hamcrest.Matcher
import org.hamcrest.Matchers.allOf
import org.hamcrest.Matchers.endsWith
Run Code Online (Sandbox Code Playgroud)
当然,你也可以只添加一个android:id
forEditText
并以这种方式得到它......
归档时间: |
|
查看次数: |
1128 次 |
最近记录: |