当包含在 androidTestCompile 中时,Espresso 将找不到 View

Espresso won't find View when included with androidTestCompile

这真奇怪。我有一个 Activity 和一个 ViewPager 托管几个 Fragment,第一个有一个 RadioButton 和 ID android:id="@+id/backjudgeRadionButton"

我有一个 Espresso 测试,如下所示:

import android.test.ActivityInstrumentationTestCase2;

import model.GameSetup;
import ui.SetupActivity;
import weigl.fm.refwatch.R;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

/**
 * Created by asco on 8/7/15.
 */
public class SetupActivityEspressoTest extends ActivityInstrumentationTestCase2<SetupActivity> {


    public SetupActivityEspressoTest() {
        super(SetupActivity.class);
    }


    @Override
    protected void setUp() throws Exception {
        super.setUp();
        getActivity();
    }

    public void testUserRoleIsSet() {


        onView(withId(R.id.backjudgeRadionButton)).perform(click());

        assertEquals(GameSetup.UserRole.backjudge, getActivity().getGameSetup().getUserRole());

    }

}

当 Espresso 通过

导入我的 build.gradle
compile('com.android.support.test.espresso:espresso-core:2.2') {
    exclude module: 'support-annotations'
}

compile('com.android.support.test:runner:0.3') {
    exclude module: 'support-annotations'
}
compile('com.android.support.test.espresso:espresso-contrib:2.2') {
    exclude module: 'support-annotations'
}

测试正常。

当我使用导入依赖项的预期变体进行仪器测试时:

androidTestCompile('com.android.support.test.espresso:espresso-core:2.2') {
    exclude module: 'support-annotations'
}

androidTestCompile('com.android.support.test:runner:0.3') {
    exclude module: 'support-annotations'
}
androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2') {
    exclude module: 'support-annotations'
}

使用 androidTestCompile 而不是 compile 测试失败,因为未找到具有提供的 ID 的视图:

Running tests
Test running started
android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with string from resource id: <2131230756>

View Hierarchy:
+>DecorView{id=-1, visibility=VISIBLE, width=280, height=280, has-focus=true, 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, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+->LinearLayout{id=-1, visibility=VISIBLE, width=280, height=280, has-focus=true, 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, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
+-->ViewStub{id=16909171, 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, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0}
|
+-->FrameLayout{id=16908290, res-name=content, visibility=VISIBLE, width=280, height=280, has-focus=true, 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, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+--->LinearLayout{id=-1, visibility=VISIBLE, width=280, height=280, has-focus=true, 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, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+---->ViewPager{id=2131558442, res-name=viewPager, visibility=VISIBLE, width=280, height=280, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=true, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=0}
|
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:580)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:82)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:53)
at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:184)
at android.support.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:115)
at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:87)
at SetupActivityEspressoTest.testUserRoleIsSet(SetupActivityEspressoTest.java:30)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)

似乎 Espresso 只检查 Activity 布局中的视图,而不是 ViewPager 提供的视图。

一个。使用 compile 而不是 androidTestCompile 时我的测试如何工作?

b。 Espresso 甚至应该在 ViewPager 内的 Fragments 内找到 Views 吗?

编辑: 这是我尝试的浓缩咖啡测试的第二个变体,取自 the new Android testing template

@RunWith(AndroidJUnit4.class)
@LargeTest
public class SetupActivityTest {


    @Rule
    public ActivityTestRule<SetupActivity> mActivityRule =
            new ActivityTestRule<>(SetupActivity.class);

    @Test
    public void findViewPerformActionAndCheckAssertion() {
        // Find Button and Click on it
        onView(withId(R.id.backjudgeRadionButton)).perform(click());

    }

}

效果一样。

如果重要的话,这一切都发生在 wear 模块中。

EDIT2: 你可以看看整个项目on GitHub

我能想到的唯一原因是: espresso 库的依赖项之一(我的赌注是支持库之一)也是对可穿戴 UI 库(com.google.android.support:wearable)的依赖项。 Espresso 上的依赖库版本比可穿戴设备上的版本更新。如果您将 Espresso 包含为 'compile' 依赖项,则会使用该库的较新版本并且一切正常。当您将它用作 'androidTestCompile' 依赖项时,旧版本将用于构建您的应用程序。

我建议你看看是否有更高版本的可穿戴 UI 库(它应该有最新的依赖项)或者,弄清楚那个依赖项是什么并获得最新版本为此你自己(并将其从 Espresso 和可穿戴 UI 库中排除)。