【发布时间】: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