Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Espresso in Android - how to insert the text into more text inputs?

I'm trying to set text into text inputs on the same activity (one by one).

Problem is that on first input is text filled correctly but on the second input i get exception message.

android.support.test.espresso.PerformException: 
Error performing 'single click' on view 'with id: com.example.xxx.test:id/txtField2'.

Here is the test method:

  @Test
    public void buttonShouldUpdateText(){
        this.clickOnActionBarMenuIcon();
        this.clickOnActionBarMenuItem(10);
        this.clickOnButton();
        this.checkTextOnActivity("Hello world!");
        this.insertTextIntoInput(R.id.txtFieldOne, "Hello world!"); // Works fine 
        this.insertTextIntoInput(R.id.txtFieldTwo, "Hello world2"); // Throws exception
    }

And method is following:

public void insertTextIntoInput(Integer inputId, String text) {
    onView(withId(inputId)).perform(typeText(text));
}

Layout is following:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="activities.SecondActivity">

    <TextView android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text1SecondView" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txtFieldOne"
        android:layout_below="@+id/text1SecondView"
        android:layout_alignParentStart="true"
        android:layout_marginTop="103dp"
        android:layout_alignParentEnd="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txtFieldTwo"
        android:layout_marginTop="51dp"
        android:layout_below="@+id/txtFieldOne"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

How can i solve it and what i am doing wrong please?

like image 768
redrom Avatar asked Oct 15 '25 20:10

redrom


1 Answers

Most likely you have a keyboard covering the edit text. Try closing it with

closeSoftKeyboard() ViewAction when you enter text in first input field.

Keep in mind that closeSoftKeyboard() will not wait for keyboard animation to stop: https://code.google.com/p/android-test-kit/issues/detail?id=79

like image 156
Be_Negative Avatar answered Oct 17 '25 12:10

Be_Negative