在我的单元测试期间,我想基本上测试是否通过对按钮的简单压力启动了一个Activity,即HorizontalListView
。我已经成功地在之前的单元测试中开始了新的活动,但是这个并没有按照需要行事。我有一个断言失败告诉我,我想要开始的活动仍然是空的。
实际上,当我启动单元测试时,似乎没有执行单击。我必须在测试期间使用我的小指针进行测试才能通过我自己。
要查看我是否点击右侧视图我使用了日志来查看我的测试点击了哪个视图以及我实际想要点击哪个视图:它似乎是正确的视图(正确的ID,正确的位置和右视图)。所以我认为问题不在于我点击的地方。
我正在测试的Activity被称为StudioActivity,我想要启动的Activity被称为AddBoasterActivity。
我的单元测试方法:
public void testSwitchToAddBoaster(){
assertTrue(mBoasterPreviewFragment.getWithAddButton());
ActivityMonitor activityMonitor = getInstrumentation().addMonitor(AddBoasterActivity.class.getName(), null, false);
mStudioActivity.runOnUiThread(new Runnable() {
@Override
public void run(){
int lPosition = mBoasterPreviewFragment.getAdapter().getCount()-1;
View lView = mBoasterPreviewFragment.getBoaster().getChildAt(lPosition);
//HERE ARE LOGS TO SEE IF MY TEST IS PERFORMING
//THE CLICK ON THE RIGHT VIEW AT THE GOOD POSITION
Log.i("TEST POSITION:", ""+lPosition);
Log.i("TEST ID: ",""+lView.getId());
Log.i("TEST VIEW: ", ""+lView);
//AND IT'S THE GOOD VIEW!
mBoasterPreviewFragment.getBoaster().performItemClick(lView,lPosition,lView.getId());
}
});
AddBoasterActivity lAddBoasterActivity = (AddBoasterActivity) getInstrumentation().waitForMonitorWithTimeout(activityMonitor,5000);
assertNotNull(lAddBoasterActivity); //HERE IS THE FAILURE, THE ACTIVITY HASN'T BEEN STARTED
mWFBoasterPreviewFragment = new WeakReference<BoasterPreviewFragment>((BoasterPreviewFragment) lAddBoasterActivity.getSupportFragmentManager().findFragmentById(R.id.fragment_preview));
mBoasterPreviewFragment = mWFBoasterPreviewFragment.get();
assertNotNull(mBoasterPreviewFragment);
lAddBoasterActivity.finish();
}
OnItemClickListener:
mBoaster.setAdapter(mAdapter);
if (withAddBoasterButton) {
mBoaster.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == mAdapter.getCount() - 1) {
//HERE IS THE CURRENT VIEW
Log.i("POSITION:", ""+position);
Log.i("ID: ",""+id);
Log.i("VIEW: ", ""+view);
Intent intent = new Intent(getActivity().getApplicationContext(), AddBoasterActivity.class);
intent.putExtra(AddBoasterActivity.EXTRA_BOASTER_SELECTED, mSelectedUsers);
intent.putExtra(AddBoasterActivity.EXTRA_IS_STREAM, isStream);
startActivityForResult(intent, AddBoasterActivity.REQUEST_CODE_ACTIVITY);
}
}});
}
手动执行点击对于必须自动化的测试来说非常尴尬。所以,如果有人知道为什么会这样做。
感谢您的帮助!
答案 0 :(得分:0)
好吧,我终于找到了......只是一个晚上睡觉,没关系。我应该早点考虑一下,但 Robotium 已经帮我几次了。
如果您尚未测试,请查看它:https://code.google.com/p/robotium/
所以这就是Robotium的样子:
public void testSwitchToAddBoaster(){
Solo solo = new Solo(getInstrumentation(), getActivity());
getInstrumentation().waitForIdleSync();
ActivityMonitor activityMonitor = getInstrumentation().addMonitor(AddBoasterActivity.class.getName(), null, false);
assertTrue(mBoasterPreviewFragment.getWithAddButton());
int lPosition = mBoasterPreviewFragment.getAdapter().getCount()-1;
View lView = mBoasterPreviewFragment.getBoaster().getChildAt(lPosition);
solo.clickOnView(lView); //SIMPLE CLICK
AddBoasterActivity lAddBoasterActivity = (AddBoasterActivity) getInstrumentation().waitForMonitorWithTimeout(activityMonitor,5000);
assertNotNull(lAddBoasterActivity); //NOW THE ACTIVITY IS STARTED
mWFBoasterPreviewFragment = new WeakReference<BoasterPreviewFragment>((BoasterPreviewFragment) lAddBoasterActivity.getSupportFragmentManager().findFragmentById(R.id.fragment_preview));
mBoasterPreviewFragment = mWFBoasterPreviewFragment.get();
assertNotNull(mBoasterPreviewFragment);
lAddBoasterActivity.finish();
}