Updating an EditText with Espresso
I'm attempting to update an EditText
as part of an Espresso test with:
onView(allOf(withClassName(endsWith("EditText")), withText(is("Test")))).perform(clearText())
.perform(click())
.perform(typeText("Another test"));
However I receive the following error:
com.google.android.apps.common.testing.ui.espresso.NoMatchingViewException: No views in hierarchy found matching: (with class name: a string ending with "EditText" and with text: is "Test")
By breaking down the test line I can see that this occurs after performing clearText()
, so I assume that the matchers are being re-run prior to each perform
and fail the prior to the second action. Although this makes sense, it leaves me somewhat confused as to how to update the EditText
using Espresso. How should I do this?
Note that I cannot use a resource ID or similar in this scenario and have to use the combination as shown above to identify the correct view.
You can use the replaceText
method.
onView(allOf(withClassName(endsWith("EditText")), withText(is("Test"))))
.perform(replaceText("Another test"));
Three things to try:
1. You can run performs in succession.
onView(...)
.perform(clearText(), typeText("Some Text"));
2. There is a recorded issue on the Espresso page which was marked as invalid (but is still very much a bug). A workaround for this is to pause the test in-between performs.
public void test01(){
onView(...).perform(clearText(), typeText("Some Text"));
pauseTestFor(500);
onView(...).perform(clearText(), typeText("Some Text"));
}
private void pauseTestFor(long milliseconds) {
try {
Thread.sleep(milliseconds);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
3. Are you absolutely sure that your EditText contains the text, "Test"?