当AndroidTestCompile附带Espresso时,找不到View

时间:2015-08-07 18:25:50

标签: android android-gradle android-espresso

这很奇怪。我有ActivityViewPager,其中包含几个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());

    }

}

当我的build.gradle通过

导入Espresso时
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似乎只检查活动布局中的视图,而不是ViewPager提供的视图。

一个。使用compile代替androidTestCompile时,我的测试如何运作?

湾Espresso甚至应该在View内的Fragments内找到ViewPager吗?

修改 这是我尝试的espresso测试的第二个变体,取自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

1 个答案:

答案 0 :(得分:3)

我能想到这件事的唯一原因是: espresso库的一个依赖项(我的支持在其中一个支持库上)也依赖于可穿戴UI库(com.google.android.support:wearable)。 Espresso上的依赖库的版本比可穿戴的版本新。如果将Espresso作为“编译”依赖项包含在内,则会使用该库的较新版本,一切都很好。当您将其用作'androidTestCompile'依赖项时,旧版本用于构建您的应用程序。

我建议您查看是否有可穿戴UI库的更高版本(应该具有最新的依赖项),或者找出该依赖项是什么并自己获取最新版本(并排除)它来自Espresso和可穿戴的UI lib)。