这很奇怪。我有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());
}
}
当我的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似乎只检查活动布局中的视图,而不是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。
答案 0 :(得分:3)
我能想到这件事的唯一原因是: espresso库的一个依赖项(我的支持在其中一个支持库上)也依赖于可穿戴UI库(com.google.android.support:wearable)。 Espresso上的依赖库的版本比可穿戴的版本新。如果将Espresso作为“编译”依赖项包含在内,则会使用该库的较新版本,一切都很好。当您将其用作'androidTestCompile'依赖项时,旧版本用于构建您的应用程序。
我建议您查看是否有可穿戴UI库的更高版本(应该具有最新的依赖项),或者找出该依赖项是什么并自己获取最新版本(并排除)它来自Espresso和可穿戴的UI lib)。