如何在搜索上按下查看espresso中的操作按钮

时间:2019-05-14 16:34:23

标签: android android-espresso searchview

建议输入文本请求后如何使用键盘上的espresso按下搜索按钮

//Fragment class
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.search_menu, menu);
    searchMenuItem = menu.findItem(R.id.action_search);
    if (searchMenuItem != null) {
        searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
        searchView.setOnQueryTextListener(this);
        searchView.setQueryHint("Search");
    }
    super.onCreateOptionsMenu(menu, inflater);
}
androidTest中的

类是:

public class GridFragmentTest {

    private final static String TAG = GridFragmentTest.class.getSimpleName();

    @Rule
    public ActivityTestRule activityTestRule = new ActivityTestRule(MainActivity.class);

    @Test
    public void GridFragmentRecycleViewTest(){
        IdlingResource componentIdlingResource = getIdlingResource();
        Espresso.registerIdlingResources(componentIdlingResource);
        Log.d(TAG, "GridFragmentRecycleViewTest()");
        onView(withId(R.id.action_search)).perform(click());
        onView(isAssignableFrom(SearchView.class)).perform(typeSearchViewText("lord")).perform(pressImeActionButton());
        EspressoIdlingResource.increment();
        onView(withId(R.id.recycler_view)).check(new RecyclerViewItemCountAssertion(10));
    }

    public static ViewAction typeSearchViewText(final String text){
        return new ViewAction(){
            @Override
            public Matcher<View> getConstraints() {
                //Ensure that only apply if it is a SearchView and if it is visible.
                return allOf(isDisplayed(), isAssignableFrom(SearchView.class));
            }

            @Override
            public String getDescription() {
                return "Change view text";
            }

            @Override
            public void perform(UiController uiController, View view) {
                ((SearchView) view).setQuery(text,false);
            }


        };
    }
}

这是代码的PerformException:

   .perform(pressImeActionButton()

例外文字为:

androidx.test.espresso.PerformException: Error performing 'androidx.test.espresso.action.EditorAction@9c9ab29' on view 'is assignable from class: class androidx.appcompat.widget.SearchView'.
at androidx.test.espresso.PerformException$Builder.build(PerformException.java:84)
at androidx.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:85)
at androidx.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:57)
at androidx.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:316)
at androidx.test.espresso.ViewInteraction.desugaredPerform(ViewInteraction.java:177)
at androidx.test.espresso.ViewInteraction.perform(ViewInteraction.java:118)

2 个答案:

答案 0 :(得分:1)

要获取视图,可以使用resources.getSystem()。getIdentifier方法获取搜索文本字段,然后应用以下方法

clearText() typeText(“ bla bla bla”)

df['Zip'] = df['Zip'].map(lambda x: f"{x.split('-')[0]:0>5}")

答案 1 :(得分:0)

代替

.perform(pressImeActionButton())

应该是:

.perform(pressKey(KeyEvent.KEYCODE_ENTER))