Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing toolbar title text with Espresso - Android

I have two fragments attached to one activity,and want to test the toolbar title of that activity.

So I wrote this:

@Test
public void testToolbar(){
    onView(allOf(instanceOf(TextView.class),withParent(withId(R.id.toolbar))))
            .check(matches(withText("Είσοδος/Εγγραφή")));

}

However, the text I put in withText method is not recognised.

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with text: is "Είσοδος/Εγγραφή"' doesn't match the selected view.
Expected: with text: is "Είσοδος/Εγγραφή"

Here is my activity's code.

public class MainActivity extends AppCompatActivity {
public static final String LOGIN_FRAGMENT = "LOGIN_FRAGMENT";
public static final String REGISTER_FRAGMENT = "REGISTER_FRAGMENT";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    setTitle("Eίσοδος/Εγγραφή");

    LoginFragment loginFragment = new LoginFragment();
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(android.R.id.content,loginFragment,LOGIN_FRAGMENT);
    fragmentTransaction.commit();
}

public void userReg(View view){
    RegisterFragment regFragment = new RegisterFragment();
    FragmentManager fragmentManager1 = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction1 = fragmentManager1.beginTransaction();
    fragmentTransaction1.addToBackStack("added");
    fragmentTransaction1.replace(android.R.id.content,regFragment,REGISTER_FRAGMENT);
    fragmentTransaction1.commit();
   }
}

and it's corresponding xml file.

<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="team.football.ael.MainActivity"
 >
 <include
    android:id="@+id/toolbar"
    layout="@layout/tool_lay"
    />
 </RelativeLayout>

What is this happening? Everything is there.

Thanks, Theo.

like image 490
Theo Avatar asked Aug 30 '16 13:08

Theo


2 Answers

onView(withId(R.id.toolbar)).check(matches(hasDescendant(withText("Title"))));

In case you have simple Toolbar without inner TextView.

like image 131
vbevans94 Avatar answered Nov 09 '22 11:11

vbevans94


I assume that your tool_layout looks like this:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="?colorPrimary"
    android:minHeight="?actionBarSize"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:navigationContentDescription="@string/abc_action_bar_up_description"
    app:navigationIcon="?homeAsUpIndicator"
    app:title="">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/app_name"/>
</android.support.v7.widget.Toolbar>

Here is the test:

    onView(withId(R.id.toolbar)).check(matches(isDisplayed()));
    onView(withText(R.string.app_name)).check(matches(withParent(withId(R.id.toolbar))));

Hope it will help

like image 41
piotrek1543 Avatar answered Nov 09 '22 09:11

piotrek1543