Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating an EditText with Espresso

People also ask

How do I change my EditText value?

This example demonstrates how do I set only numeric value for editText in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I edit a EditText file?

How to include a Edittext in an Android App: First of all, create a new Android app, or take an existing app to edit it. In both the case, there must be an XML layout activity file and a Java class file linked to this activity. Open the Activity file and include a Edittext field in the layout (activity_main.

How do I get TextView espresso from text?

The basic idea is to use a method with an internal ViewAction that retrieves the text in its perform method. Anonymous classes can only access final fields, so we cannot just let it set a local variable of getText() , but instead an array of String is used to get the string out of the ViewAction .

What is the difference between EditText and TextView?

EditText is used for user input. TextView is used to display text and is not editable by the user. TextView can be updated programatically at any time.


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"?


To set value in EditText with Espresso simple like this

onView(withId(R.id.yourIdEditText)).perform(typeText("Your Text"))


I was having a similar issue and solved it using the containsString matcher and Class.getSimpleName(). Like this:

onView(withClassName(containsString(PDFViewPagerIVZoom.class.getSimpleName()))).check(matches(isDisplayed()));

You can see the full code here


You could try two things. First I would try to use

onView(withId(<id>).perform... 

This way you would always have access to the EditText field even when other EditText fields are on the screen.

If that's not an option, you could split up your perform calls.

onView(allOf(withClassName(endsWith("EditText")),withText(is("Test")))).perform(clearText());
onView(withClassName(endsWith("EditText"))).perform(click());
onView(withClassName(endsWith("EditText"))).perform(typeText("Another Test");