Espresso - 在列表视图中按文本单击 [英] Espresso - Click by text in List view

查看:27
本文介绍了Espresso - 在列表视图中按文本单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<块引用>

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

正如预期的那样,这不起作用.错误表示在层次结构中没有视图.有谁知道如何选择一个字符串?(在本例中为ASDF")提前致谢.

更新由于@haffax

我收到错误:

<块引用>

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

第二个错误

使用此代码

<块引用>

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

我收到此错误

<块引用>

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

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

<小时>

解决方案

<块引用>

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

解决方案

问题是,您尝试将列表视图本身与 instanceOf(ListView.class) 作为 onData().onData() 需要一个数据匹配器来匹配 ListView 的适配数据,而不是 ListView 本身,也不是 View Adapter.getView() 返回的是实际数据.

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

ListView listView = (ListView)findViewById(R.id.myListView);ArrayAdapter适配器 = getAdapterFromSomewhere();listView.setAdapter(适配器);

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

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

(您可以使用org.hamcrest.Matchers的方法使用另一个Matcher)

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

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

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.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.

Update due to @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.

Second error

With this code

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")


Solution

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

解决方案

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 ListView itself, and also not the View that Adapter.getView() returns, but the actual data.

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:

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

(You can use another Matcher using a method of 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:

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

这篇关于Espresso - 在列表视图中按文本单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆