- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这太奇怪了。我有一个 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/
我正在尝试在 Windows 上运行的小于 1GB 的 VM 上设置 YouTrack 和 TeamCity。使用率将非常低(用户和请求)。这是一个 POC 环境,如果它有效,我可能会将它推送到一个超
所以我在尝试使用 FORFILES 解决这个问题时遇到了麻烦。我正在尝试获取不超过 4 天的文件。所以基本上少于 4 天。然而,这似乎不太可能,因为/d -4 获取所有 4 天或更早的项目。 以下是我
如何从下面的 events 表中选择小于 15 分钟前创建的 events? CREATE TABLE events ( created_at timestamp NOT NULL DEFAU
Google Analytics Realtime提供 rt:minutesAgo ,可以过滤查询。 然而,它是一个维度而不是一个度量标准,<=不能在过滤器中使用。 假设我想在最后 n 分钟内获得一些
iOS 核心数据 - 严重的应用程序错误 - 尝试插入 nil 你好, 我的应用程序实际上运行稳定,但在极少数情况下它会崩溃并显示此错误消息... 2019-04-02 20:48:52.437172
我想制作一个 html div 以快速向右移动(例如不到 1 秒)并消失。然后1秒后再次直接出现在这个过程最开始div的位置。此过程将由单击按钮并重复 10 次触发。 我试图在 CSS 中使用过渡属性
我发现使用 TimeTrigger 是 Windows 10 (UWP) 上计划后台任务的方式。但是看起来我们需要给出的最小数字是 15 分钟。只是想知道,即使我们安排它在接下来的 1 分钟内运行,警
我必须在 1 秒内在屏幕上打印 2^20 行整数 printf 不够快,还有其他易于使用的快速输出替代方法吗? 每一行只包含 1 个整数。 我要求它用于竞争性编程问题,我必须将其源代码提交给法官。 最
我是一名优秀的程序员,十分优秀!