1

In a small application I am trying to have hidden EditText field(onCreate of activity we set focus to it). So once user start activity and start typing I render character images on Activity.

As functionality is working find however I have found difficulty in writing espresso test for it. I have came across behaviour where if EditText has background color set to white(#FFFFFF) then espresso is not able to perform typeText() operation on it. Shows error in console.

android.support.test.espresso.PerformException: Error performing 'replace text' on view 'with id: some.test.test.espressopractice:id/main_label_2'

Any inputs would be helpful to understand this. Following is the sample code. github url for code https://github.com/anilnamde/AndroidStudy/tree/master/EspressoPractice.

Layout activity_main.xml

  <EditText
    android:text="First"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="#FF0000"
    android:textSize="40sp"
    android:id="@+id/main_label"/>


  <EditText
    android:text="Second"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#00FF00"
    android:height="0dp"
    android:width="0dp"
    android:textSize="40sp"
    android:id="@+id/main_label_1"/>

  <EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#0000FF"
    android:background="#FFFFFF"
    android:height="0dp"
    android:width="0dp"
    android:id="@+id/main_label_2"/>

</LinearLayout>

Activity Code MainActivity.class

public class MainActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }
  ....
}

Test code is following

@RunWith(AndroidJUnit4.class)
public class MainActivityTest {

  @Rule
  public ActivityTestRule<MainActivity> rule = new ActivityTestRule<MainActivity>(MainActivity.class);
// Normal EditText : Passed
  @Test
  public void testNormalEditText(){
    onView(withId(R.id.main_label)).check(matches(withText("First")));
    onView(withId(R.id.main_label)).perform(clearText(), typeText("Hello"));
    onView(withId(R.id.main_label)).check(matches(withText("Hello")));
  }
// EditText with 0dp width and 0dp height - Passed
  @Test
  public void testEditTextWithHeighWidthZero(){
    onView(withId(R.id.main_label_1)).check(matches(withText("Second")));
    onView(withId(R.id.main_label_1)).perform(clearText(), typeText("Hello"));
    onView(withId(R.id.main_label_1)).check(matches(withText("Hello")));
  }
// EditText has white background : Failed
  @Test
  public void testEditTextWithHeighWidthZeroAndWhiteBackground(){
    onView(withId(R.id.main_label_2)).perform(clearText(), typeText("Hello"));
    onView(withId(R.id.main_label_2)).check(matches(withText("Hello")));
  }


}
Anil Namde
  • 6,452
  • 11
  • 63
  • 100

1 Answers1

2

It's your clearText() that fails, not typeText().

But the reason for the failure is that Espresso can't do anything with the view as the hight and width of the view is 0.

If you inspect your other views using the Hierarchy View tool you will notice that all your other edittexts have hight, even your main_label_1 has a height set to 22, so espresso is able to type text there.

Be_Negative
  • 4,892
  • 1
  • 30
  • 33