无法使用Espresso从AutoCompleteTextView下拉列表中选择一个项目

时间:2018-09-01 06:43:58

标签: android android-espresso

以下是我的AutoCompleteTextView下拉菜单的快照,我正从中选择第二项

enter image description here

我的Espresso代码:

onView(withId(R.id.searchContainer)).check(matches(isDisplayed()));
onData(anything())
      .inAdapterView(withId(R.id.searchContainer))
      .atPosition(1)
      .perform(click());

但是我收到以下异常:

android.support.test.espresso.PerformException: Error performing 'load adapter data' on view 'with id: com.sampleaddressdd.demo:id/searchContainer'

使用Espresso Recorder,我得到以下代码:

DataInteraction appCompatTextView = onData(anything())
                .inAdapterView(childAtPosition(
                        withClassName(is("android.widget.PopupWindow$PopupBackgroundView")),
                        0))
                .atPosition(0);
        appCompatTextView.perform(click());

下拉充气机代码:

        mSearchView = findViewById(R.id.textSearch);
        mSearchView.setDropDownAnchor(R.id.searchContainer);
        mHttpClient.fetchAddresss(new HttpClient.AddressCallback() {
            @Override
            public void run() {
                mAdapter = new AddressAdapter(MainActivity.this, mAddresss, new AddressAdapter.OnAddressClickCallback() {
                    @Override
                    public void execute(Address Address) {
                        startActivity(AddressProfileActivity.createIntent(MainActivity.this, Address));
                    }
                });
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mSearchView.setAdapter(mAdapter);
                    }
                });
            }
        });

1 个答案:

答案 0 :(得分:1)

最终通过使用

实现了这一目标
onView(withText(LOCATION_TO_BE_SEARCHED)) .inRoot(withDecorView(not(mActivityTestRule.getActivity().getWindow().getDecorView()))) .check(matches(isDisplayed())) .perform(click());