使用Espresso测试工具栏标题文本 - Android

时间:2016-08-30 13:54:21

标签: java android android-studio android-espresso

我有两个片段附加到一个活动,并且想要测试该活动的工具栏标题。

所以我写了这个:

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

}

但是,我用withText方法输入的文字无法识别。

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

这是我的活动代码。

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();
   }
}

它是相应的xml文件。

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

这是怎么回事?一切都在那里。

谢谢, 西奥。

4 个答案:

答案 0 :(得分:6)

我认为您的tool_layout看起来像这样:

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

以下是测试:

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

希望它会有所帮助

答案 1 :(得分:6)

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

如果您具有不带内部TextView的简单工具栏。

答案 2 :(得分:0)

我以更通用的方式创建了Espresso Utils Gist,您可以在其中看到Google不提供的不同匹配器。

用法:

onView(isAssignableFrom(Toolbar.class)).check(matches(withToolbarTitle(R.string.all_name_app)));

答案 3 :(得分:0)

用于测试活动标题的Kotlin代码:

  val activityTitleTextView = onView(
                    allOf(
                            childAtPosition(
                                    allOf(withId(R.id.action_bar),
                                            childAtPosition(
                                                    withId(R.id.action_bar_container),
                                                    0)),
                                    0),
                            isDisplayed()))
  activityTitleTextView.check(matches(withText("My Activity Title"))