问题描述
我想点击使用长者preSSO列表视图中的文本。我知道他们有<一个href="https://$c$c.google.com/p/android-test-kit/wiki/Es$p$pssoSamples#Matching_data_using_onData_and_a_custom_ViewMatcher">this指南,但我看不出如何通过寻找文本,使这项工作。这是我曾尝试
正如预期的那样,这没有奏效。该错误表示,在层次结构没有意见。有谁知道如何选择一个字符串? (ASDF
在这种情况下)在此先感谢。
更新,由于@haffax
我收到错误:
第二个错误
通过这个code
我得到这个错误
解决方案
现在的问题是,你尝试匹配列表视图本身与的instanceof(ListView.class)
作为参数为昂达()
。 昂达()
需要数据匹配匹配的ListView的调整数据
,而不是 ListView控件
本身,也不是查看
的 Adapter.getView()
的回报,但的实际数据。
如果你有这样的事情在生产code:
的ListView ListView的=(的ListView)findViewById(R.id.myListView);
ArrayAdapter&LT; MyDataClass&GT;适配器= getAdapterFromSomewhere();
listView.setAdapter(适配器);
然后居presso.onData()的匹配器参数
应与 MyDataClass
的所需的实例。所以,这样的事情应该工作:
昂达(hasToString(startsWith(ASDF)))执行(点击());
(可以使用的方法,用另一个匹配 org.hamcrest.Matchers
)
如果你有你的活动多适配器的意见,你可以叫 ViewMatchers.inAdapterView()
,以便匹配指定这样的适配器视图:
昂达(hasToString(startsWith(ASDF)))
.inAdapterView(withId(R.id.myListView))
.perform(点击());
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
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:
Second error
With this code
I get this error
Solution
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());
这篇关于居preSSO - 通过点击列表视图中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!