gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:54:04 24 4
gpt4 key购买 nike

这太奇怪了。我有一个 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 的 View :

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 布局中的 View ,而不是 ViewPager 提供的 View 。

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

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

编辑:这是我尝试的 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 .

最佳答案

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

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

关于android - 当包含在 androidTestCompile 中时,Espresso 将找不到 View,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31884355/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com