簡體   English   中英

使用Espresso測試工具欄標題文本 - Android

[英]Testing toolbar title text with Espresso - Android

我有兩個片段附加到一個活動,並希望測試該活動的工具欄標題。

所以我寫了這個:

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

}

但是,我用inText方法放入的文本無法識別。

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>

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

謝謝,西奧。

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

如果您有簡單的工具欄沒有內部TextView。

我假設你的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代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM