简体   繁体   English

使用Espresso在微调器中选择关闭屏幕项目

[英]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.

相关问题 将浓缩咖啡设置为浓缩咖啡 - Set Spinner item with espresso 浓缩咖啡,点击一个微调项目 - Espresso, click on a spinner Item 使用DialogFragment时,Espresso不会在微调器中选择项目 - Espresso won't select item in spinner when using a DialogFragment 从Android微调器中选择项目并使用它 - Selecting Item from Android Spinner and Using It 选择项目后,微调框下拉列表视图中的屏幕顶部 - Spinner dropdown drawing top of screen in listview after selecting item 在对话框中选择微调器时,Android espresso 中的 RunTimeException - RunTimeException in Android espresso when selecting spinner in dialog 微调器没有选择第一项 - spinner not selecting first item Android Espresso:从Web视图中的微调器中选择一个项目 - Android Espresso : select an item from a spinner in a webview 为什么记录的用于选择 Spinner 下拉项的 espresso 测试代码会在不同的 Android 版本上导致不同的、不兼容的代码? - Why does recorded espresso test code for selecting a Spinner drop-down item result in different, incompatible code on different Android versions? 我该如何通过从Android的微调器中选择项目(只需点击该项目)进入下一个屏幕? - How do i go to the next screen by selecting item (just tapping on the item) from spinner in android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM