[英]Selecting off screen item in a spinner using Espresso
I have a Spinner
with 20 items. 我有一个有20个项目的
Spinner
。 The below code works if the item
is displayed on the screen when user click on the spinner. 如果用户单击微调器时屏幕上显示该
item
则以下代码有效。
onData(allOf(is(instanceOf(SpinnerItem.class)), is(item))).perform(click());
For item
that are requiring scrolling, the test will hit the below exception: 对于需要滚动的
item
,测试将遇到以下异常:
android.support.test.espresso.PerformException: Error performing 'single click - At Coordinates: 719, -243 and precision: 16, 16' on view ' displaying data matching: (is an instance of SpinnerItem and is <Text>) within adapter view matching: is assignable from class: class android.widget.AdapterView'.
at android.support.test.espresso.PerformException$Builder.build(PerformException.java:83)
...
you can clearly see that the y-position is at -243 and therefore it couldn't perform the clicking. 你可以清楚地看到y位置是-243,因此无法执行点击。 I tried to add
perform(scrollTo(), click())
but it's complaining the scrollTo
doesn't work with onData. 我试图添加
perform(scrollTo(), click())
但它抱怨scrollTo
不能与onData一起使用。
Appreciate for any help. 感谢任何帮助。 Thanks!
谢谢!
I also have a large spinner and scrolling works fine. 我也有一个大的旋转器和滚动工作正常。 My code is the following:
我的代码如下:
onData(allOf(is(instanceOf(String.class)), containsString("5.1.3"))).perform(click());
Using espresso 2.2.2: 使用espresso 2.2.2:
com.android.support.test.espresso:espresso-core:2.2.2
The only difference is that I use String in my adapter instead of custom class. 唯一的区别是我在我的适配器中使用String而不是自定义类。 Maybe you should try to switch from
SpinnerItem
to String
just to view will this error be gone or not? 也许你应该尝试从
SpinnerItem
切换到String
只是为了查看这个错误是否会消失?
Also problem can be in is(SpinnerItem)
. 问题还
is(SpinnerItem)
。 You can try to write own Matcher for your SpinnerItems, like this: 您可以尝试为SpinnerItem编写自己的Matcher,如下所示:
public static Matcher<Object> withContent(final String content) {
return new BoundedMatcher<Object, SpinnerItem>(SpinnerItem.class) {
@Override
public boolean matchesSafely(SpinnerItem myObj) {
return myObj.content == content;
}
@Override
public void describeTo(Description description) {
description.appendText("with content '" + content + "'");
}
};
}
and use: 并使用:
onData(withContent("5.")).inAdapterView(withId(R.id.contents_theory_listview)).perform(click());
This example suppose you have a content
field in your item and you can check for its equality with the pattern. 此示例假设您的项目中有一个
content
字段,您可以检查它与模式的相等性。
I hope I gave you some ideas and they will help you. 我希望我给你一些想法,他们会帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.