【问题标题】:Android: how to use Espresso to test that a buttons bgcolor has changed when it's being held down?Android:如何使用 Espresso 测试按下按钮时 bgcolor 是否发生了变化?
【发布时间】:2015-12-07 16:50:04
【问题描述】:

我想使用 Espresso 来测试一个具有颜色选择器的按钮,因为它的背景是在用户按下它时真正正确地改变颜色。但是,当我尝试使用 espresso 执行此操作时,实际单击的注册速度如此之快,以至于按钮执行它的行为(启动进度条)并且测试失败并显示“未找到视图”。当用户在没有/之前执行按钮逻辑的情况下按下它时,如何测试背景颜色是否发生了变化?基本上我在这里寻找的是一种模拟 OnTouchListener 的 Action_Down 的方法。

Espresso.onView(ViewMatchers.withId(R.id.somebutton)).perform(ViewActions.click());

Espresso.onView(ViewMatchers.withText(R.string.somebuttonText)).check(ViewAssertions.matches(withTextColor(Color.BLACK)));


   public static Matcher<View> withTextColor(final int color) {
        Checks.checkNotNull(color);
        return new BoundedMatcher<View, TextView>(TextView.class) {
            @Override
            public boolean matchesSafely(TextView warning) {
                return color == warning.getCurrentTextColor();
            }
            @Override
            public void describeTo(Description description) {
                description.appendText("with text color: ");
            }
        };
    }

我得到的错误是:

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with string from resource id: <someid>

我也尝试过这样做,但失败并出现同样的错误:

    ViewInteraction somebuttoninteraction = Espresso.onView(ViewMatchers.withText(R.string.somebuttontext));
    somebuttoninteraction.perform(ViewActions.click()).check(ViewAssertions.matches(withTextColor(Color.BLACK)));

我也试过这样(长按而不是点击),但还是一样:

somebuttoninteraction.perform(ViewActions.longClick()).check(ViewAssertions.matches(withTextColor(Color.BLACK)));

【问题讨论】:

    标签: android unit-testing android-studio android-testing android-espresso


    【解决方案1】:

    解决方案(很奇怪)是做一些长而复杂的事情,如下所示:

    Espresso.onView(ViewMatchers.withText(R.string.somebuttontext)).inRoot(RootMatchers.withDecorView(CoreMatchers.is(getActivity().getWindow().getDecorView()))).perform(ViewActions.click()).check(ViewAssertions.matches(withTextColor(Color.BLACK)));
    

    为了在点击后搜索不在窗口前台的视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-15
      • 2013-06-25
      • 2013-06-11
      • 2015-10-30
      • 2012-04-25
      • 1970-01-01
      • 2022-11-17
      相关资源
      最近更新 更多