Android Espresso – ¿Cómo comprobar la pista EditText?

Estoy empezando a jugar con Espresso, tengo mis pruebas básicas corriendo. Ahora tratando de averiguar cómo comprobar que mi texto de edición tiene un texto de sugerencia específica? Gracias.

onView(withId(R.id.locationInput)).check(matches...?)

Ya que Espresso 2.0 sólo usa ViewMatcher interno conHint :

 onView(withId(R.id.locationInput)).check(matches(withHint("your_hint"))) 

Parece que lo descubrí. Básicamente, usted necesita crear su propio matcher:

 public static Matcher<View> withHint(final String expectedHint) { return new TypeSafeMatcher<View>() { @Override public boolean matchesSafely(View view) { if (!(view instanceof EditText)) { return false; } String hint = ((EditText) view).getHint().toString(); return expectedHint.equals(hint); } @Override public void describeTo(Description description) { } }; } 

Entonces usted puede utilizarlo:

 onView(withId(R.id.locationInput)).check(matches(withHint("Location (Optional)"))); 

Hay una manera ligeramente diferente de hacerlo. En mi caso, comprueba que la String no es null, antes de pasarla a matcher (como se explica en ejemplos de Espresso). Y también en código abajo no necesitas el R.id del EditText con esta pista. Usted apenas comprueba si la indirecta con "hintText" se exhibe:

 import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; public final class Matchers { public static Matcher<View> withItemHint(String hintText) { // use preconditions to fail fast when a test is creating an invalid matcher. checkArgument(!(hintText.equals(null))); return withItemHint(is(hintText)); } public static Matcher<View> withItemHint(final Matcher<String> matcherText) { // use preconditions to fail fast when a test is creating an invalid matcher. checkNotNull(matcherText); return new BoundedMatcher<View, EditText>(EditText.class) { @Override public void describeTo(Description description) { description.appendText("with item hint: " + matcherText); } @Override protected boolean matchesSafely(EditText editTextField) { return matcherText.matches(editTextField.getHint().toString()); } }; } } 

Uso:

 import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.isDisplayed; import static com.google.android.apps.common.testing.ui.espresso.assertion.ViewAssertions.matches; import static com.google.android.apps.common.testing.ui.espresso.Espresso.onView; import static com.your.package.withMatcher.withItemHint; . . . onView(withItemHint(someHintString)).check(matches(isDisplayed())); 

He creado un Matcher que soporta pasar un resourceId en lugar de un String

 public static Matcher<View> withHint(final int resourceId) { return new BoundedMatcher<View, TextView>(TextView.class) { private String resourceName = null; private String expectedHint = null; @Override public boolean matchesSafely(TextView editText) { if (null == expectedHint) { try { expectedHint = editText.getResources().getString(resourceId); resourceName = editText.getResources().getResourceEntryName(resourceId); } catch (Resources.NotFoundException ignored) { /* view could be from a context unaware of the resource id. */ } } if (null != expectedHint) { return expectedHint.equals(editText.getHint()); } else { return false; } } @Override public void describeTo(Description description) { description.appendText("with string from resource id: "); description.appendValue(resourceId); if (null != resourceName) { description.appendText("["); description.appendText(resourceName); description.appendText("]"); } if (null != expectedHint) { description.appendText(" value: "); description.appendText(expectedHint); } } }; } 

Es una réplica de Espresso con Matriz de texto apuntada por Valera Zakharov ( withText (resourceId )

  • ¿Cómo volver a ejecutar la prueba fallada en Espresso? - lluvia de ideas
  • Cómo usar Espresso para probar el elemento en el adaptador en una posición específica
  • Android - Fragmento independiente herramienta de prueba de interfaz de usuario
  • Android - Espresso - menú de opciones largas - haga clic en una opción-elemento de menú que no esté visible
  • Android + Espresso + solicitud HTTP asíncrona -> cómo probar?
  • Espresso 2 en Android, las pruebas intermedias fallan después de no iniciar la actividad bajo prueba mientras las actividades de las pruebas anteriores aún están vivas
  • IdlingResource Espresso con RxJava
  • Android Espresso - Navegador Web
  • Android Espresso: el filtrado de prueba no es compatible con la versión dada de JUnit. Actualice la versión de JUnit por lo menos a 4.6
  • Android: Espresso no espera hasta que se muestre fragmento o actividad para que cada prueba fallara
  • Dependencias de Gradle: Cómo averiguar qué está causando esta duplicación
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.