Espresso.onData在AdapterView中设置为AlertDialog [英] Espresso.onData set inAdapterView to be AlertDialog
问题描述
我有一个AlertDialog,其中包含设置的选项列表:
最终AlertDialog.Builder builder = new AlertDialog。 Builder(getContext())
.setTitle(args.getString(TITLE))
.setSingleChoiceItems(ARRAY_OF_CHARSEQUENCE,args.getInt(SELECTED_INDEX),新DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){
//处理单击
dismiss();
}
});
我正在尝试测试每个项目的点击率。
如果我运行:
onView(allOf(withClassName(Matchers.equalTo(AppCompatCheckedTextView.class.getName()) ),withText(displayName)))
.perform(click());
除了AlertDialog中的选项列表太长以致无法完全显示之外,其他所有功能都非常有效。 / p>
因此,我试图改用onData查找项目,但是我不确定如何指示onData创建的DataInteraction对象在哪个视图中查找数据。
我尝试单击的AppCompatCheckedTextView对象包含在ID为select_dialog_listview的AlertController $ RecyleListView中。因此,我尝试了:
onData(withText(displayName))
.inAdapterView(withId(R.select_dialog_listview) )
.perform(click());
和
onData(withText(displayName))
.inAdapterView(withClassName(Matchers.equalTo( android.internal.app.AlertController $ RecyleListView)))
.perform(click());
但是会收到一个错误,即层次结构中没有匹配的视图。
因此,然后我尝试将自定义视图添加到警报对话框中,以查看是否可以通过ID找到该视图。
我有一个仅包含LinearLayout的布局文件:
< ; LinearLayout xmlns:android = http://schemas.android.com/apk/res/android
android:orientation = vertical android:layout_width = match_parent
android:layout_height = match_parent
android:id = @ + id / dialog_view>
< / LinearLayout>
我添加到AlertDialog构建器中的位置:
final AlertDialog.Builder builder = new AlertDialog.Builder(getContext())
.setTitle(args.getString(TITLE))
.setView(LayoutInflater.from (getContext())。inflate(R.layout.alert_dialog_view,null))
.setSingleChoiceItems(ARRAY_OF_CHARSEQUENCE,args.getInt(SELECTED_INDEX),新DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){
//处理点击
dismiss();
}
});
然后在Espresso中寻找:
onData(withText(displayName))
.inAdapterView(withId(R.dialog_view))
.perform(click());
但是会收到错误:
android.support.test.espresso.PerformException:在视图 ID为org.OUR.APPLICATION:id / dialog_view的视图上执行加载适配器数据时出错。
如果我正确使用onData执行点击,将DataInteraction指向的正确方法是什么
onData()与 com.android.internal.app.AlertController $ RecycleListView确实适合我,请注意,您在 RecyleListView中有错字。
此外,onData()参数应为 dataMatcher,在用作ArrayAdapter的T的类型上应为instanceOf()
对我有用:
onData(is(instanceOf(String.class)))。inAdapterView(allOf(withClassName(equalTo( com.android.internal.app.AlertController $ RecycleListView)),isDisplayed()))。atPosition(index).perform (click());
I have an AlertDialog that contains a list of options which is set:
final AlertDialog.Builder builder = new AlertDialog.Builder(getContext())
.setTitle(args.getString(TITLE))
.setSingleChoiceItems(ARRAY_OF_CHARSEQUENCE, args.getInt(SELECTED_INDEX), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// handle click
dismiss();
}
});
I am trying to test clicking on each item. If I run:
onView(allOf(withClassName(Matchers.equalTo(AppCompatCheckedTextView.class.getName())), withText(displayName)))
.perform(click());
Things work wonderfully, except if the list of options in the AlertDialog is too long to display entirely.
So, I am trying to find the item by using onData instead, but I am unsure how to instruct the DataInteraction object created by onData in what view to look for the data.
The AppCompatCheckedTextView objects that I am trying to click on are contained by an AlertController$RecyleListView with ID select_dialog_listview. So, I have tried:
onData(withText(displayName))
.inAdapterView(withId(R.select_dialog_listview))
.perform(click());
and
onData(withText(displayName))
.inAdapterView(withClassName(Matchers.equalTo("android.internal.app.AlertController$RecyleListView")))
.perform(click());
But receive an error that there is no matching view in the hierarchy.
So, then I tried adding a custom view to the Alert Dialog to see if I could find that by ID.
I have a layout file containing only a LinearLayout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/dialog_view">
</LinearLayout>
Which I add to my AlertDialog builder:
final AlertDialog.Builder builder = new AlertDialog.Builder(getContext())
.setTitle(args.getString(TITLE))
.setView(LayoutInflater.from(getContext()).inflate(R.layout.alert_dialog_view, null))
.setSingleChoiceItems(ARRAY_OF_CHARSEQUENCE, args.getInt(SELECTED_INDEX), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// handle click
dismiss();
}
});
And then look for in Espresso:
onData(withText(displayName))
.inAdapterView(withId(R.dialog_view))
.perform(click());
But receive the error:
android.support.test.espresso.PerformException: Error performing 'load adapter data' on view 'with id: org.OUR.APPLICATION:id/dialog_view'.
If I am correct in using onData to perform the click, what is the proper method of pointing DataInteraction to the AlertDialog view.
onData() with "com.android.internal.app.AlertController$RecycleListView" does work for me, note that you have typo in "RecyleListView".
Also, onData() parameter should be "dataMatcher", instanceOf() on the type used as T for ArrayAdapter (String in this case) works.
This works for me:
onData(is(instanceOf(String.class))).inAdapterView(allOf(withClassName(equalTo("com.android.internal.app.AlertController$RecycleListView")), isDisplayed())).atPosition(index).perform(click());
这篇关于Espresso.onData在AdapterView中设置为AlertDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!