我目前正在嘗試使用Espresso在Android上測試ListView
,並使用自定義ArrayAdapter
。 下面是測試:Android Espresso - 測試ListView行
final AssociationsListActivity listActivity = getActivity();
final Association association1 = new Association(ASSOCIATION_NAMES[0], ASSOCIATION_DESCRIPTIONS[0]);
final Association association2 = new Association(ASSOCIATION_NAMES[1], ASSOCIATION_DESCRIPTIONS[1]);
final Association association3 = new Association(ASSOCIATION_NAMES[2], ASSOCIATION_DESCRIPTIONS[2]);
listActivity.addAssociation(association1);
listActivity.addAssociation(association2);
listActivity.addAssociation(association3);
assertTrue(listActivity.getAssociationsList().size() == 3);
onView(withId(R.id.associationsListView)).check(matches(isDisplayed()));
onData(anything())
.inAdapterView(withId(R.id.associationsListView))
.atPosition(0)
.perform(click());
assertCurrentActivityIsInstanceOf(AssociationsListActivityTest.this
, AssociationsPageActivity.class);
的方法,簡單地listActivity.addAssociation(association);
確實associationArrayAdapter.add(association);
。
的ArrayAdapter
用於元素與ID添加到ListView
(默認可見)R.id.associationsListView
當我運行這個測試,我得到以下錯誤:
android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'is displayed on the screen to the user' doesn't match the selected view.
Expected: is displayed on the screen to the user
Got: "ListView{id=2131427414, res-name=associationsListView, visibility=VISIBLE, width=996, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=42.0, y=42.0, child-count=0}"
這是造成下面的斷言,onView(withId(R.id.associationsListView)).check(matches(isDisplayed()));
此外,當評論這個斷言,下一個例外導致以下異常:
android.support.test.espresso.PerformException: Error performing 'load adapter data' on view 'with id: ch.epfl.sweng.project:id/associationsListView'.
...
Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints:
(is assignable from class: class android.widget.AdapterView and is displayed on the screen to the user)
Target view: "ListView{id=2131427414, res-name=associationsListView, visibility=VISIBLE, width=996, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=42.0, y=42.0, child-count=0}"
我經歷了很多覆蓋這些異常的堆棧溢出線程,我用其他等價物替換了那些測試,但是這些錯誤仍然顯示出來,我不知道該怎麼做!謝謝你的幫助!