让gridView有一些图像。 gridView的单元格来自相同的预定义布局,具有相同的id和desc。
R.id.item_image == 2131493330
onView(withId(is(R.id.item_image))).perform(click());
由于网格中的所有单元格都具有相同的ID,因此它得到AmbiguousViewMatcherException
。
如何选择第一个或其中任何一个?
谢谢!
android.support.test.espresso.AmbiguousViewMatcherException:' id为:< 2131493330>'匹配层次结构中的多个视图。 问题观点标有' **** MATCHES ****'下方。
+ -------------> ImageView {id = 2131493330,res-name = item_image,desc = Image,visibility = VISIBLE,width = 262,height = 262,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 = false,is -selected = false,root-is-layout-requested = false,has-input-connection = false,x = 0.0,y = 0.0} **** MATCHES ****
+ -------------> ImageView {id = 2131493330,res-name = item_image,desc = Image,visibility = VISIBLE,width = 262,height = 262,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 = false,is -selected = false,root-is-layout-requested = false,has-input-connection = false,x = 0.0,y = 0.0} **** MATCHES **** |
答案 0 :(得分:71)
我很惊讶我只是通过简单地提供索引和匹配器(即withText,withId)找不到解决方案。当你处理onData和ListViews时,接受的答案只能解决问题。
如果屏幕上有多个具有相同resId / text / contentDesc的视图,则可以使用此自定义匹配器选择所需的视图而不会导致AmbiguousViewMatcherException:
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(R.id.my_view), 2)).perform(click());
将对R.id.my_view的第三个实例执行单击操作。
答案 1 :(得分:20)
您应该使用onData()
来操作GridView
:
onData(withId(R.id.item_image))
.inAdapterView(withId(R.id.grid_adapter_id))
.atPosition(0)
.perform(click());
此代码将点击GridView
答案 2 :(得分:20)
与网格视图情况不完全相关,但您可以使用hamcrest allOf
匹配器来组合多个条件:
import static org.hamcrest.CoreMatchers.allOf;
onView(allOf(withId(R.id.login_password),
withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))
.check(matches(isCompletelyDisplayed()))
.check(matches(withHint(R.string.password_placeholder)));
答案 3 :(得分:13)
我创建了一个ViewMatcher,它匹配它找到的第一个视图。 也许这对某人有帮助。 例如。当你没有AdapterView使用onData()时。
/**
* Created by stost on 15.05.14.
* Matches any view. But only on first match()-call.
*/
public class FirstViewMatcher extends BaseMatcher<View> {
public static boolean matchedBefore = false;
public FirstViewMatcher() {
matchedBefore = false;
}
@Override
public boolean matches(Object o) {
if (matchedBefore) {
return false;
} else {
matchedBefore = true;
return true;
}
}
@Override
public void describeTo(Description description) {
description.appendText(" is the first view that comes along ");
}
@Factory
public static <T> Matcher<View> firstView() {
return new FirstViewMatcher();
}
}
像这样使用:
onView(FirstViewMatcher.firstView()).perform(click());
答案 4 :(得分:11)
尝试@FrostRocket答案,因为看起来最有希望,但需要添加一些自定义:
public static Matcher<View> withIndex(final Matcher<View> matcher, final int index) {
return new TypeSafeMatcher<View>() {
int currentIndex;
int viewObjHash;
@SuppressLint("DefaultLocale") @Override
public void describeTo(Description description) {
description.appendText(String.format("with index: %d ", index));
matcher.describeTo(description);
}
@Override
public boolean matchesSafely(View view) {
if (matcher.matches(view) && currentIndex++ == index) {
viewObjHash = view.hashCode();
}
return view.hashCode() == viewObjHash;
}
};
}
答案 5 :(得分:5)
案例:
onView( withId( R.id.songListView ) ).perform( RealmRecyclerViewActions.scrollTo( Matchers.first(Matchers.withTextLabeled( "Love Song"))) );
onView( Matchers.first(withText( "Love Song")) ).perform( click() );
在我的Matchers.class中
public static Matcher<View> first(Matcher<View> expected ){
return new TypeSafeMatcher<View>() {
private boolean first = false;
@Override
protected boolean matchesSafely(View item) {
if( expected.matches(item) && !first ){
return first = true;
}
return false;
}
@Override
public void describeTo(Description description) {
description.appendText("Matcher.first( " + expected.toString() + " )" );
}
};
}
答案 6 :(得分:2)
也不特别与网格视图相关,但如果对其他人有用,我也会遇到类似的问题,即我的 RecyclerView 和根目录布局具有相同的ID ,并在屏幕上两者 显示。帮助我解决的是检查descendancy
,例如:
onView(allOf(withId(R.id.my_view), not(isDescendantOfA(withId(R.id.recyclerView))))).check(matches(withText("My Text")));
答案 7 :(得分:0)
You can simply make NthMatcher like:
class NthMatcher internal constructor(private val id: Int, private val n: Int) : TypeSafeMatcher<View>(View::class.java) {
companion object {
var matchCount: Int = 0
}
init {
var matchCount = 0
}
private var resources: Resources? = null
override fun describeTo(description: Description) {
var idDescription = Integer.toString(id)
if (resources != null) {
try {
idDescription = resources!!.getResourceName(id)
} catch (e: Resources.NotFoundException) {
// No big deal, will just use the int value.
idDescription = String.format("%s (resource name not found)", id)
}
}
description.appendText("with id: $idDescription")
}
public override fun matchesSafely(view: View): Boolean {
resources = view.resources
if (id == view.id) {
matchCount++
if(matchCount == n) {
return true
}
}
return false
}
}
Declare like this:
fun withNthId(resId: Int, n: Int) = CustomMatchers.NthMatcher(resId, n)
And use like this:
onView(withNthId(R.id.textview, 1)).perform(click())
答案 8 :(得分:0)
最晚*运行->记录浓缩咖啡测试
通过单击具有不同位置的相同ID视图,会为它们生成不同的代码,因此请尝试。
它实际上解决了这些问题。