【问题标题】:Getting button background color in Espresso在 Espresso 中获取按钮背景颜色
【发布时间】:2020-10-22 14:20:48
【问题描述】:

我正在尝试借助此 Matcher 获取按钮背景颜色。

    public static Matcher<View> withButtonColor(int expectedColor) {

    return new BoundedMatcher<View, Button>(Button.class) {
        @Override
        public boolean matchesSafely(Button button) {
            int actualColor = 0;
            try {
                actualColor = ContextCompat.getColor(currentActivity.getBaseContext(), expectedColor);
            } catch (Exception e) {
                e.printStackTrace();
            }

            return expectedColor == actualColor;

        }

        @Override
        public void describeTo(Description description) {
            description.appendText("ERROR");
        }
    };
}

使用

onView(withId(trueButton)).check(matches(withButtonColor(ActivityPage.greenColor)));

但没有任何帮助。

我传递了一个有效的颜色作为参数,尽管实际颜色总是等于 0。 我想获取上下文可能有问题。

【问题讨论】:

    标签: android testing automated-tests android-espresso


    【解决方案1】:

    您的自定义 ViewMatcher 看起来不错。

    尝试改变:

    currentActivity.getBaseContext()
    

    为:

    InstrumentationRegistry.getInstrumentation().getTargetContext()
    

    作为上下文参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-26
      • 2022-01-02
      • 2016-08-12
      • 1970-01-01
      • 2013-06-24
      • 2012-04-07
      • 2020-08-19
      • 2013-08-06
      相关资源
      最近更新 更多