居preSSO - 通过点击列表视图中的文本视图、文本、列表、preSSO

2023-09-04 09:58:46 作者:万劫不复

我想点击使用长者preSSO列表视图中的文本。我知道他们有this指南,但我看不出如何通过寻找文本,使这项工作。这是我曾尝试

 居presso.onData(Matchers.allOf(Matchers.is(Matchers.instanceOf(ListView.class)),Matchers.hasToString(Matchers.startsWith(ASDF) 。)))执行(ViewActions.click());
 

正如预期的那样,这没有奏效。该错误表示,在层次结构没有意见。有谁知道如何选择一个字符串? (ASDF在这种情况下)在此先感谢。

更新,由于@haffax

我收到错误:

  

com.google.android.apps.common.testing.ui.es presso.AmbiguousViewMatcherException:从级分配:类android.widget.AdapterView'匹配层次结构中的多个视图

第二个错误

通过这个code

onData(hasToString(startsWith("ASDF"))).inAdapterView(withContentDescription("MapList")).perform(click()); IDA Pro进行DEX文件修改

我得到这个错误

  

com.google.android.apps.common.testing.ui.es presso.PerformException:错误执行对视图'装入适配器的数据'。'与内容介绍:为MAPLIST

     

产生的原因:java.lang.RuntimeException的:没有找到数据匹配:asString(一是以ASDF)

解决方案

  

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

解决方案

现在的问题是,你尝试匹配列表视图本身与的instanceof(ListView.class)作为参数为昂达()昂达()需要数据匹配匹配的ListView的调整数据,而不是 ListView控件本身,也不是查看 Adapter.getView()的回报,但的实际数据。

如果你有这样的事情在生产code:

 的ListView ListView的=(的ListView)findViewById(R.id.myListView);
ArrayAdapter< MyDataClass>适配器= 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

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());