【问题标题】:Android Espresso - Testing a ListView rowAndroid Espresso - 测试 ListView 行
【发布时间】:2016-10-24 13:48:06
【问题描述】:

我目前正在尝试使用 Espresso 在 android 上测试 ListView,并使用自定义 ArrayAdapter。 这是测试:

 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为R.id.associationsListViewListView(默认可见)

当我运行这个测试时,我收到以下错误:

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}"

我经历了很多覆盖这些异常的堆栈溢出线程,我用其他等价物替换了这些测试,但这些错误仍然出现我不知道该怎么办!感谢您的帮助!

【问题讨论】:

    标签: java android unit-testing testing android-espresso


    【解决方案1】:

    我发现了问题所在!

    我将元素添加到ArrayAdapter 的方法使用了runOnUIThread,但似乎测试并未等待此方法,因此associationsListView 被认为未向用户显示!所以我像这样修复了我的方法listActivity.addAssociation(Association association)

    public void addAssociation(final Association association) {
        final CountDownLatch latch = new CountDownLatch(1);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (!associationList.contains(association)) {
                    associationArrayAdapter.add(association);
                }
                latch.countDown();
            }
        });
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (!associationList.contains(association)) {
            associationList.add(association);
        }
    }
    

    现在测试的所有部分都通过了 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-11
      • 2023-03-28
      • 1970-01-01
      • 2017-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多