尝试单击切换框时出现Espresso模糊ViewMatcherException



当我尝试运行下面的测试时,我得到了模糊的ViewMatcherException,我如何从多个视图中获得预期的视图。

ViewInteraction switch_ = onView(
allOf(withClassName(is("android.widget.Switch"))
childAtPosition(
allOf(withId(android.R.id.widget_frame),
childAtPosition(
withClassName(is("android.widget.LinearLayout")),
2)),
0),
isDisplayed()));
switch_.perform(click());

它给了我以下两个交换机id匹配的例外

Switch{id=16909051, res-name=switchWidget, visibility=VISIBLE, width=136, height=51, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, 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=6.0, text=, input-type=0, ime-target=false, has-links=false, is-checked=false} ****MATCHES**

Switch{id=16909051, res-name=switchWidget, visibility=VISIBLE, width=136, height=51, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, 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=6.0, text=, input-type=0, ime-target=false, has-links=false, is-checked=false} ****MATCHES****

我尝试过使用hasfocus((和EffectiveVisibility(VISIBLE(方法,但无法解决问题。

有人能帮忙吗?

下面的方法解决了我的问题。

现在可以使用index方法访问具有相同id的多个视图。

public static Matcher<View> withIndex(final Matcher<View> matcher, final int index) {
return new TypeSafeMatcher<View>() {
int currentIndex = 0;
@Override
public void describeTo(Description description) {
description.appendText("with index: ");
description.appendValue(index);
matcher.describeTo(description);
}
@Override
public boolean matchesSafely(View view) {
return matcher.matches(view) && currentIndex ++== index;
}
};
}

在定义了方法之后,将这一行添加到测试用例中,以访问正确的交换帧

onView(withIndex(withId(android.R.id.widget_frame), 3)).perform(click());

相关内容

  • 没有找到相关文章

最新更新