Android Espresso - 在列表视图中按文本单击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22965839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:37:36  来源:igfitidea点击:

Espresso - Click by text in List view

androidandroid-testingandroid-espresso

提问by Chad Bingham

I am trying to click on a text in a list view using Espresso. I know they have this guide, but I can't see how to make this work by looking for text. This is what I have tried

我正在尝试使用 Espresso 单击列表视图中的文本。我知道他们有这个指南,但我不知道如何通过查找文本来完成这项工作。这是我尝试过的

Espresso.onData(Matchers.allOf(Matchers.is(Matchers.instanceOf(ListView.class)), Matchers.hasToString(Matchers.startsWith("ASDF")))).perform(ViewActions.click());
Espresso.onData(Matchers.allOf(Matchers.is(Matchers.instanceOf(ListView.class)), Matchers.hasToString(Matchers.startsWith("ASDF")))).perform(ViewActions.click());

As expected, this didn't work. The error said no view in hierarchy. Does anyone know how to select a String? ("ASDF"in this case) Thanks in advance.

正如预期的那样,这没有用。错误表示在层次结构中没有视图。有谁知道如何选择一个字符串?("ASDF"在这种情况下)提前致谢。

Update due to @haffax

更新由于@haffax

I received error:

我收到错误:

com.google.android.apps.common.testing.ui.espresso.AmbiguousViewMatcherException: 'is assignable from class: class android.widget.AdapterView' matches multiple views in the hierarchy.

com.google.android.apps.common.testing.ui.espresso.AmbiguousViewMatcherException: 'isassignable from class: class android.widget.AdapterView' 匹配层次结构中的多个视图。

Second error

第二个错误

With this code

有了这个代码

onData(hasToString(startsWith("ASDF"))).inAdapterView(withContentDescription("MapList")).perform(click());
onData(hasToString(startsWith("ASDF"))).inAdapterView(withContentDescription("MapList")).perform(click());

I get this error

我收到这个错误

com.google.android.apps.common.testing.ui.espresso.PerformException: Error performing 'load adapter data' on view 'with content description: is "MapList"'.

Caused by: java.lang.RuntimeException: No data found matching: asString(a string starting with "ASDF")

com.google.android.apps.common.testing.ui.espresso.PerformException:在视图上执行“加载适配器数据”时出错,内容描述为“MapList”。

引起:java.lang.RuntimeException:找不到匹配的数据:asString(以“ASDF”开头的字符串)



Solution

解决方案

onData(anything()).inAdapterView(withContentDescription("desc")).atPosition(x).perform(click())

onData(anything()).inAdapterView(withContentDescription("desc")).atPosition(x).perform(click())

回答by haffax

The problem is, that you try to match the list view itself with the instanceOf(ListView.class)as argument for onData(). onData()requires a data matcher that matches the adapted data of the ListView, not the ListViewitself, and also not the Viewthat Adapter.getView()returns, but the actual data.

问题是,你尝试匹配列表视图本身与instanceOf(ListView.class)对作为参数onData()onData()需要一个数据匹配器来匹配 的适应数据,而ListView不是它ListView本身,也不ViewAdapter.getView()返回的 ,而是实际数据。

If you have something like this in your production code:

如果您的生产代码中有这样的内容:

ListView listView = (ListView)findViewById(R.id.myListView);
ArrayAdapter<MyDataClass> adapter = getAdapterFromSomewhere();
listView.setAdapter(adapter);

Then the Matcher argument of Espresso.onData()should match the desired instance of MyDataClass. So, something like this should work:

然后 Matcher 参数Espresso.onData()应该匹配 的所需实例MyDataClass。所以,这样的事情应该工作:

onData(hasToString(startsWith("ASDF"))).perform(click());

(You can use another Matcher using a method of org.hamcrest.Matchers)

(您可以使用 的方法使用另一个匹配器org.hamcrest.Matchers

In case you have multiple adapter views in your activity, you can call ViewMatchers.inAdapterView()with a view matcher that specifies the AdapterView like this:

如果您的活动中有多个适配器视图,您可以ViewMatchers.inAdapterView()使用指定 AdapterView 的视图匹配器进行调用,如下所示:

onData(hasToString(startsWith("ASDF")))
    .inAdapterView(withId(R.id.myListView))
    .perform(click());

回答by Veer

If adapter have custom model class for example Item:

例如,如果适配器具有自定义模型类Item

public static Matcher<Object> withItemValue(final String value) {
        return new BoundedMatcher<Object, Item>(Item.class) {
            @Override
            public void describeTo(Description description) {
                description.appendText("has value " + value);
            }

            @Override
            public boolean matchesSafely(Item item) {
                return item.getName().toUpperCase().equals(String.valueOf(value));
            }
        };
    }

Then call following:

然后调用如下:

onData(withItemValue("DRINK1")).inAdapterView(withId(R.id.menu_item_grid)).perform(click());

回答by anuja jain

onData(hasEntry(equalTo(ListViewActivity.ROW_TEXT),is("List item: 25")))
        .onChildView(withId(R.id.rowTextView)).perform(click());

This works best for me with row text data..

这对行文本数据最适合我..