android - 如何使用 Espresso 测试 fragment ?

标签 android android-fragments android-espresso ui-testing

您好,我正在尝试使用 Espresso 测试 fragment 。我在一个 Activity 中有两个按钮,我在单击按钮时替换 fragment 。我的 XML 如下。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Fragment No.1"
        android:onClick="selectFrag" />

    <Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="selectFrag"
        android:text="Fragment No.2" />

    <fragment
        android:name="com.example.admin1.espressotestingsample.fragment.FragmentOne"
        android:id="@+id/fragment_place"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

我有两个 fragment 。每个 fragment 都有一个按钮和 TextView 。

fragment 是

FragmentOne XML:-

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@android:color/holo_blue_bright">

    <TextView
        android:textSize="28dp"
        android:id="@+id/fragment_textview_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="First Fragment"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:textColor="@android:color/white"
        />

    <Button
        android:id="@+id/fragment_button_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button One"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

FragmentTwo XML:

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@android:color/holo_orange_dark">

    <TextView
        android:textSize="28dp"
        android:id="@+id/fragment_textview_two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Second Fragment"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:textColor="@android:color/white"/>

    <Button
        android:id="@+id/fragment_button_two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button Two"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

我的 Activity 代码是

public class ActivityFragment extends FragmentActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_activity);
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_place, new FragmentOne()).addToBackStack(null)
                .commit();
    }

    public void selectFrag(View view) {
        Fragment fr;

        if(view == findViewById(R.id.button2)) {
            fr = new FragmentTwo();

        }else {
            fr = new FragmentOne();
        }

        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_place, fr).addToBackStack(null)
                .commit();

    }
}

我的测试用例如下。

 @Rule
    public ActivityTestRule<ActivityFragment> testRule =
            new ActivityTestRule<>(ActivityFragment.class);

    @Before
    public void init(){
        testRule.getActivity()
                .getSupportFragmentManager().beginTransaction();
    }

@Test
public void testSecondFragment() {  
  onView(withId(R.id.button2)).perform(click());
  onView(allOf(withId(R.id.fragment_textview_two),withEffectiveVisibility(VISIBLE))).
check(matches(isDisplayed()));
}

当我运行这个测试时它工作正常。但是当我尝试第二个时,我遇到了错误。

@Test
public void testFirstFragment() {  
  onView(withId(R.id.button1)).perform(click());
  onView(allOf(withId(R.id.fragment_textview_one),withEffectiveVisibility(VISIBLE))).
check(matches(isDisplayed()));
}

但是当我运行这个测试时,我收到了以下错误。

android.support.test.espresso.AmbiguousViewMatcherException: '(with id: com.example.admin1.espressotestingsample:id/fragment_textview_one and view has effective visibility=VISIBLE)' matches multiple views in the hierarchy.
Problem views are marked with '****MATCHES****' below.

View Hierarchy:
+>DecorView{id=-1, visibility=VISIBLE, width=1080, height=1920, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params={(0,0)(fillxfill) ty=BASE_APPLICATION wanim=0x10302f8
fl=LAYOUT_IN_SCREEN LAYOUT_INSET_DECOR SPLIT_TOUCH HARDWARE_ACCELERATED DRAWS_SYSTEM_BAR_BACKGROUNDS
pfl=FORCE_DRAW_STATUS_BAR_BACKGROUND}, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3}
|
+->LinearLayout{id=-1, visibility=VISIBLE, width=1080, height=1794, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@704500d, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
+-->ViewStub{id=16908682, res-name=action_mode_bar_stub, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@4483bc2, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0}
|
+-->FrameLayout{id=16908290, res-name=content, visibility=VISIBLE, width=1080, height=1731, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@e334d10, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=63.0, child-count=1}
|
+--->LinearLayout{id=-1, visibility=VISIBLE, width=1080, height=1731, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@9682a3c, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3}
|
+---->Button{id=2131165220, res-name=button1, visibility=VISIBLE, width=1080, height=126, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@4a073c5, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=Fragment No.1, input-type=0, ime-target=true, has-links=false}
|
+---->Button{id=2131165221, res-name=button2, visibility=VISIBLE, width=1080, height=126, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@af5811a, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=126.0, text=Fragment No.2, input-type=0, ime-target=false, has-links=false}
|
+---->ConstraintLayout{id=2131165244, res-name=fragment_place, visibility=VISIBLE, width=1080, height=1479, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@e5a94e6, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=252.0, child-count=4}
|
+----->TextView{id=2131165245, res-name=fragment_textview_one, visibility=VISIBLE, width=479, height=100, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.support.constraint.ConstraintLayout$LayoutParams@5d3aa27, tag=null, root-is-layout-requested=false, has-input-connection=false, x=301.0, y=690.0, text=First Fragment, input-type=0, ime-target=false, has-links=false} ****MATCHES****
|
+----->Button{id=2131165242, res-name=fragment_button_one, visibility=VISIBLE, width=286, height=126, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=android.support.constraint.ConstraintLayout$LayoutParams@a2184d4, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=Button One, input-type=0, ime-target=false, has-links=false}
|
+----->ConstraintLayout{id=-1, visibility=VISIBLE, width=1080, height=1479, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.support.constraint.ConstraintLayout$LayoutParams@bab1972, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
+------>TextView{id=2131165245, res-name=fragment_textview_one, visibility=VISIBLE, width=479, height=100, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.support.constraint.ConstraintLayout$LayoutParams@9c6c1c3, tag=null, root-is-layout-requested=false, has-input-connection=false, x=301.0, y=690.0, text=First Fragment, input-type=0, ime-target=false, has-links=false} ****MATCHES****
|
+------>Button{id=2131165242, res-name=fragment_button_one, visibility=VISIBLE, width=286, height=126, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=android.support.constraint.ConstraintLayout$LayoutParams@87c1a40, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=Button One, input-type=0, ime-target=false, has-links=false}
|
+----->ConstraintLayout{id=-1, visibility=VISIBLE, width=1080, height=1479, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.support.constraint.ConstraintLayout$LayoutParams@30d5abe, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
+------>TextView{id=2131165245, res-name=fragment_textview_one, visibility=VISIBLE, width=479, height=100, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.support.constraint.ConstraintLayout$LayoutParams@b49b71f, tag=null, root-is-layout-requested=false, has-input-connection=false, x=301.0, y=690.0, text=First Fragment, input-type=0, ime-target=false, has-links=false} ****MATCHES****
|
+------>Button{id=2131165242, res-name=fragment_button_one, visibility=VISIBLE, width=286, height=126, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=android.support.constraint.ConstraintLayout$LayoutParams@5289a6c, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=Button One, input-type=0, ime-target=false, has-links=false}
|
+->View{id=16908336, res-name=navigationBarBackground, visibility=VISIBLE, width=1080, height=126, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@cded835, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=1794.0}
|
+->View{id=16908335, res-name=statusBarBackground, visibility=VISIBLE, width=1080, height=63, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@ec064ca, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0}
|
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:1538)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:88)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:51)
at android.support.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:312)
at android.support.test.espresso.ViewInteraction.check(ViewInteraction.java:297)
at com.example.admin1.espressotestingsample.FragmentTest.testSecondFragment(FragmentTest.java:67)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at android.support.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)...

我很困惑。第一个代码正在运行。但是第二个代码不起作用。请告诉我原因并帮助我测试 fragment 。因为我的整个项目仅使用 fragment 构建。

最佳答案

您的测试抛出 AmbigousViewMatcherException 因为它找到了 3 个相同的匹配器 withId(R.id.fragment_textview_one), withEffectiveVisibility(VISIBLE)),这意味着 3 个 FragmentOne 已创建。

在您的 Activity XML 布局中,更改以下 fragment:

<fragment
    android:name="com.example.admin1.espressotestingsample.fragment.FragmentOne"
    android:id="@+id/fragment_place"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

对于其他类型的ViewGroup,说一个FrameLayout:

<FrameLayout
    android:name="com.example.admin1.espressotestingsample.fragment.FragmentOne"
    android:id="@+id/fragment_place"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

我不确定第三个 fragment ,但你不需要调用 testRule.getActivity().getSupportFragmentManager().beginTransaction() 尤其是当你没有做任何事情时它在你的测试中。

关于android - 如何使用 Espresso 测试 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52927526/

相关文章:

android - 确定 Espresso 测试时的 fragment 变化

android - 使用 android gradle + dagger 运行 instrumentTests

android - 网格布局宽度问题

java - 需要更多关于如何将文本设置为 Fragment 内的 TextView 的说明

android - 使用 Android 的 Algolia 和 Cloud Firestore?

android - 将数据从 Activity 发送到 Fragment Android Studio

android - 美国市场或其他地方是否有任何 Android 设备未附带 Google map 附加组件库?

android - ViewPager 中相邻 fragment 的问题有助于持有它的 Activity

java - 解决了: SearchView doesn't filter in each child Tab of TabLayout

java - @BeforeClass 和 @AfterClass 在每次测试之前和之后调用