使用Espresso的Android UI测试在AlertDialog内部视图 [英] Android UI testing with Espresso on an AlertDialog inner views
本文介绍了使用Espresso的Android UI测试在AlertDialog内部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
AlertDialog.Builder builder = new AlertDialog.Builder(activity_reference);
builder.setTitle(R.string.main_dialog_title);
ListView lv = new ListView(activity_reference);
ArrayAdapter< String> adapter = new ArrayAdapter< String>
(activity_reference,android.R.layout.simple_list_item_1,android.R.id.text1,
gUsernameList);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> parent,View view,int position,long id){
Toast.makeText(activity_reference, 你选择了:+ gUsernameList.get(position),Toast.LENGTH_LONG).show();
account_selected = gUsernameList.get(position);
}
}
) ;
builder.setView(lv);
builder.setPositiveButton(OK,new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int whichButton){
if(account_selected == null){
return;
}
dialog.dismiss();
Intent myIntent = new Intent(activity_reference,GraphActivity.class);
myIntent.putExtra(account_selected account_selected); //可选参数
activity_reference.startActivity(myIntent);
}
}
);
final对话框= builder.create();
dialog.show();
在测试中
onView(withText(R.string.test_account))器InRoot(isDialog())检查(匹配(isDisplayed()))。。
我做错了吗?
事先感谢。
编辑:我尝试使用UIAutomator,但是得到UIObjectNotFoundException,虽然它显然在uiautomatorviewer中。
解决方案
似乎我只是缺少启动代码。
mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
activityTestRule.getActivity();
对于错误的闹钟,对不起。
I'm testing a click on an AlertDialog's inner ListView using Espresso but keep getting NoMatchingViewException. The Dialog has a ListView that reads the accounts in the device.
AlertDialog.Builder builder = new AlertDialog.Builder(activity_reference);
builder.setTitle(R.string.main_dialog_title);
ListView lv = new ListView(activity_reference);
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(activity_reference, android.R.layout.simple_list_item_1, android.R.id.text1,
gUsernameList);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(activity_reference, "You selected :" + gUsernameList.get(position), Toast.LENGTH_LONG).show();
account_selected = gUsernameList.get(position);
}
}
);
builder.setView(lv);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton) {
if (account_selected == null) {
return;
}
dialog.dismiss();
Intent myIntent = new Intent(activity_reference, GraphActivity.class);
myIntent.putExtra("account_selected", account_selected); //Optional parameters
activity_reference.startActivity(myIntent);
}
}
);
final Dialog dialog = builder.create();
dialog.show();
And in the test
onView(withText(R.string.test_account)).inRoot(isDialog()).check(matches(isDisplayed()));
Am I doing anything wrong ? Thanks beforehand.
Edit : I tried using UIAutomator too but gets UIObjectNotFoundException, although it is clearly there in the uiautomatorviewer.
解决方案
It seems I was just missing the initiation codes.
mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
activityTestRule.getActivity();
Sorry for the false alarm.
这篇关于使用Espresso的Android UI测试在AlertDialog内部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文