Quiero probar mi interfaz de usuario de inicio de sesión que contiene algunos campos de TextInputLayout, configuré un código para que se ejecute pero falla y arroja un error, ¿alguien puede por favor con este problema? Se lo agradezco de antemano.
@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>'.No puede escribir texto en la vista TextInputLayout , eso es solo un contenedor. TextInputLayout tiene un FrameLayout como elemento secundario directo, y en ese FrameLayout la primera vista secundaria es EditText (en la que puede escribir)
Puede obtener EditText en Espresso con algunos emparejadores adicionales (encuentre una vista que sea descendiente del diseño base R.id.text_layout y tenga un nombre de clase que termine con EditText ).
onView( allOf( isDescendantOfA(withId(R.id.text_layout)), withClassName(endsWith("EditText")) ) ).perform( typeText("Hello world") )Si tiene que hacer esto en muchos lugares, puede escribir una función de ayuda
fun isEditTextInLayout(parentViewId: Int): Matcher<View> { return allOf( isDescendantOfA(withId(parentViewId)), withClassName(endsWith("EditText")) ) }y usarlo como
onView(isEditTextInLayout(R.id.text_layout)).perform(typeText("Hello world"))para un XML que parece
<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>Algunas de las importaciones requeridas para que esto funcione son
import androidx.test.espresso.matcher.ViewMatchers.* import org.hamcrest.Matcher import org.hamcrest.Matchers.allOf import org.hamcrest.Matchers.endsWith Por supuesto, también podría simplemente agregar un android:id para EditText y obtenerlo de esa manera también ...