diff --git a/README.md b/README.md index 3fca8708..a9db86a1 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,8 @@ openMenu(); #### Writing into widgets ```java -writeTo(R.id.edittext, "A great text"); +writeTo(R.id.edittext, "A great text"); // Ignores the EditText restrictions like maxLength and textFilter. It's blazing fast. +typeTo(R.id.edittext, "A great text"); // Honors the EditText restrictions like maxLength and textFilter. It slows down the test. writeToAutoComplete(R.id.autocomplete, "Another great text"); clearText(R.id.edittext) ``` diff --git a/library/src/main/java/com/schibsted/spain/barista/interaction/BaristaEditTextInteractions.kt b/library/src/main/java/com/schibsted/spain/barista/interaction/BaristaEditTextInteractions.kt index 9c948358..8026a710 100644 --- a/library/src/main/java/com/schibsted/spain/barista/interaction/BaristaEditTextInteractions.kt +++ b/library/src/main/java/com/schibsted/spain/barista/interaction/BaristaEditTextInteractions.kt @@ -3,6 +3,7 @@ package com.schibsted.spain.barista.interaction import androidx.annotation.IdRes import androidx.test.espresso.action.ViewActions.clearText import androidx.test.espresso.action.ViewActions.replaceText +import androidx.test.espresso.action.ViewActions.typeText import androidx.test.espresso.matcher.ViewMatchers.isAssignableFrom import androidx.test.espresso.matcher.ViewMatchers.isDescendantOfA import androidx.test.espresso.matcher.ViewMatchers.withId @@ -13,6 +14,11 @@ import org.hamcrest.Matchers.anyOf object BaristaEditTextInteractions { + /** + * This uses [androidx.test.espresso.action.ViewActions.replaceText], + * to remove any existing text and insert the characters directly, + * ignoring any EditText properties like `maxLength`, `textFilter`, etc. + */ @JvmStatic fun writeTo(@IdRes editTextId: Int, text: String) { val withId = withId(editTextId) @@ -23,6 +29,25 @@ object BaristaEditTextInteractions { combinedMatcher.performAction(replaceText(text)) } + /** + * This uses [androidx.test.espresso.action.ViewActions.typeText], to + * tap once on the EditText and type the characters like an on-screen keyboard. + * Most EditText properties, like `maxLength` and `textFilter` will be honored. + */ + @JvmStatic + fun typeTo(@IdRes editTextId: Int, text: String) { + val withId = withId(editTextId) + val assignableFrom = isAssignableFrom(EditText::class.java) + val simpleMatcher = allOf(withId, assignableFrom) + val wrapperMatcher = allOf(isDescendantOfA(withId), assignableFrom) + val combinedMatcher = anyOf(simpleMatcher, wrapperMatcher) + combinedMatcher.performAction(typeText(text)) + } + + /** + * Clears text on the view. See also + * [androidx.test.espresso.action.ViewActions.clearText]. + */ @JvmStatic fun clearText(@IdRes editTextId: Int) { withId(editTextId).performAction(clearText()) diff --git a/sample/src/androidTest/java/com/schibsted/spain/barista/sample/EditTextTest.java b/sample/src/androidTest/java/com/schibsted/spain/barista/sample/EditTextTest.java index 470e92e5..8cb856fa 100644 --- a/sample/src/androidTest/java/com/schibsted/spain/barista/sample/EditTextTest.java +++ b/sample/src/androidTest/java/com/schibsted/spain/barista/sample/EditTextTest.java @@ -10,6 +10,7 @@ import static com.schibsted.spain.barista.assertion.BaristaHintAssertions.assertHint; import static com.schibsted.spain.barista.assertion.BaristaVisibilityAssertions.assertDisplayed; import static com.schibsted.spain.barista.interaction.BaristaEditTextInteractions.clearText; +import static com.schibsted.spain.barista.interaction.BaristaEditTextInteractions.typeTo; import static com.schibsted.spain.barista.interaction.BaristaEditTextInteractions.writeTo; @RunWith(AndroidJUnit4.class) @@ -39,6 +40,24 @@ public void checkWriteOnEditText_whenParentIsNotAScrollView() { assertDisplayed("Hello!"); } + @Test + public void checkTypeToEditText_whenEditTextIsVisible() { + typeTo(R.id.edittext_constrained, "Hello1=What's-Your*Name?"); + assertDisplayed("Hello1What"); + } + + @Test + public void checkTypeToEditText_whenScrollIsNeeded() { + typeTo(R.id.edittext_constrained_very_far_away, "Hello,ABC/123"); + assertDisplayed("HelloABC12"); + } + + @Test + public void checkTypeToEditText_whenParentIsNotAScrollView() { + typeTo(R.id.edittext_constrained_centered, " Hello 123 "); + assertDisplayed("Hello123"); + } + @Test public void assertHintById() { assertHint(R.id.edittext_centered, R.string.centered_edittext); diff --git a/sample/src/main/res/layout/activity_edittext.xml b/sample/src/main/res/layout/activity_edittext.xml index b7de5f9b..c273237b 100644 --- a/sample/src/main/res/layout/activity_edittext.xml +++ b/sample/src/main/res/layout/activity_edittext.xml @@ -22,6 +22,16 @@ android:hint="I'm an edittext" /> + + + + @@ -40,4 +60,16 @@ android:layout_gravity="center" android:hint="@string/centered_edittext" /> - \ No newline at end of file + + +