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

Testing toolbar title text with Espresso - Android

我有两个片段附加到一个 activity,我想测试那个 activity 的工具栏标题。

所以我写了这个:

@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 "Είσοδος/Εγγραφή"

这是我的 activity 的代码。

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>

这是怎么回事?什么都有。

谢谢, 西奥.

我假设你的 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))));

希望对您有所帮助

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

用法:

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

用于测试的 Kotlin 代码 Activity 标题:

  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"))
onView(withId(R.id.toolbar)).check(matches(hasDescendant(withText("Title"))));

如果你有没有内部 TextView 的简单工具栏。