浓咖啡:选择后为什么纺纱厂不关门? [英] Espresso: Why don't spinners close after selection?

查看:215
本文介绍了浓咖啡:选择后为什么纺纱厂不关门?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于在Spinners with Espresso中选择商品的问题。或者更确切地说:选择有效,但之后视图断言失败,因为微调器仍处于打开状态。

I have a question about selecting items in Spinners with Espresso. Or to be more exact: The selection works, but after that the view assertions fail because the spinner is still open.

假设我有一个非常简单的活动包含一个微调器和显示选择的文本视图,如下所示:

Let's say I have a really simple activity that contains a spinner and a textview that shows the selection, like this:

现在,我写了一个选择'狗'的Espresso测试,并验证textview是否相应更新:

Now, I wrote an Espresso test that selects 'dogs' and verifies the textview is updated accordingly:

@Test
public void selectDogs() throws Exception {
    onData(allOf(is(instanceOf(String.class)), is("dogs")))
            .inAdapterView(withId(R.id.spinner))
            .perform(click());
    onView(withId(R.id.selected)).check(matches(withText("dogs")));
}

该测试失败,因为 onData().. .perform(click()); 不关闭微调器,它只是让它打开。通过调试验证:

That test fails because the onData()...perform(click()); does not close the spinner, it just leaves it open. Verified it via debugging:

异常是:

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: org.flinc.app:id/selected
If the target view is not part of the view hierarchy, you may need to use Espresso.onData to load it from one of the following AdapterViews:android.widget.ListPopupWindow$DropDownListView{226cb88 VFED.VC.. .F...... 0,0-231,432}

我能(排序)通过在微调器选择后添加 pressBack()来修复我的测试,但对我来说这可能不是正确的解决方案。它也会在模拟器上出现问题,但它似乎在我的设备上正常运行。
如何在微调器中正确选择并关闭它?

I can (sort of) fix my tests by adding pressBack() after the spinner selection, but to me this can't be the correct solution. Also it makes problems on the emulator, but it seems to run fine on my device. How to correctly select something in the spinner and then close it?

非常感谢您的帮助!

以下是供参考的活动代码:

Here is the activity code for reference:

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test_spinner);

    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    final TextView selected = (TextView) findViewById(R.id.selected);

    final List<String> items = Arrays.asList("cats", "dogs", "unicorns");
    final ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, items);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            selected.setText(items.get(position));
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            selected.setText("");
        }

    });
}


推荐答案

你必须点击首先使用此代码:

You have to click on the spinner first, use this code:

@Test
public void selectDogs() throws Exception {
    onView(withId(R.id.spinner)).perform(click());
    onData(allOf(is(instanceOf(String.class)), is("dogs")))
            .perform(click());
    onView(withId(R.id.selected)).check(matches(withText("dogs")));
}

这篇关于浓咖啡:选择后为什么纺纱厂不关门?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆