如何使用 espresso 在 SearchView 上键入文本
How to type text on a SearchView using espresso
TypeText
似乎不适用于 SearchView
。
onView(withId(R.id.yt_search_box))
.perform(typeText("how is the weather?"));
给出错误:
Error performing 'type text(how is the weather?)' on view 'with id:../yt_search_box'
对于同样遇到此问题的任何人,解决方案是为 SearchView 类型编写一个 ViewAction,因为 typeText 仅支持 TextEditView
这是我的解决方案:
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);
}
};
}
您可以使用 Resource#getSystem 获取视图
Resources.getSystem().getIdentifier("search_src_text",
"id", "android")
onView(withId(Resources.getSystem().getIdentifier("search_src_text",
"id", "android"))).perform(clearText(),typeText("enter the text"))
.perform(pressKey(KeyEvent.KEYCODE_ENTER))
@MiguelSlv 上面的回答,转换为kotlin
fun typeSearchViewText(text: String): ViewAction {
return object : ViewAction {
override fun getDescription(): String {
return "Change view text"
}
override fun getConstraints(): Matcher<View> {
return allOf(isDisplayed(), isAssignableFrom(SearchView::class.java))
}
override fun perform(uiController: UiController?, view: View?) {
(view as SearchView).setQuery(text, false)
}
}
}
这对我有用:
onView(withId(R.id.search_src_text)).perform(typeText("how is the weather?"))
基于 César Noreña 的回复,我设法在搜索字段中插入了文本。
首先,我单击了我的视图,然后在 android 搜索视图 ID 上键入文本。
onView(withId(R.id.my_own_search_menu_id)).perform(click());
onView(withId(R.id.search_src_text)).perform(typeText("how is the weather?"))
SearchView 的 X 按钮也有 ID search_close_btn
onView(withId(R.id.search_close_btn)).perform(click()); // first time erase the content
onView(withId(R.id.search_close_btn)).perform(click()); // second time close the SearchView
TypeText
似乎不适用于 SearchView
。
onView(withId(R.id.yt_search_box))
.perform(typeText("how is the weather?"));
给出错误:
Error performing 'type text(how is the weather?)' on view 'with id:../yt_search_box'
对于同样遇到此问题的任何人,解决方案是为 SearchView 类型编写一个 ViewAction,因为 typeText 仅支持 TextEditView
这是我的解决方案:
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);
}
};
}
您可以使用 Resource#getSystem 获取视图
Resources.getSystem().getIdentifier("search_src_text",
"id", "android")
onView(withId(Resources.getSystem().getIdentifier("search_src_text",
"id", "android"))).perform(clearText(),typeText("enter the text"))
.perform(pressKey(KeyEvent.KEYCODE_ENTER))
@MiguelSlv 上面的回答,转换为kotlin
fun typeSearchViewText(text: String): ViewAction {
return object : ViewAction {
override fun getDescription(): String {
return "Change view text"
}
override fun getConstraints(): Matcher<View> {
return allOf(isDisplayed(), isAssignableFrom(SearchView::class.java))
}
override fun perform(uiController: UiController?, view: View?) {
(view as SearchView).setQuery(text, false)
}
}
}
这对我有用:
onView(withId(R.id.search_src_text)).perform(typeText("how is the weather?"))
基于 César Noreña 的回复,我设法在搜索字段中插入了文本。 首先,我单击了我的视图,然后在 android 搜索视图 ID 上键入文本。
onView(withId(R.id.my_own_search_menu_id)).perform(click());
onView(withId(R.id.search_src_text)).perform(typeText("how is the weather?"))
SearchView 的 X 按钮也有 ID search_close_btn
onView(withId(R.id.search_close_btn)).perform(click()); // first time erase the content
onView(withId(R.id.search_close_btn)).perform(click()); // second time close the SearchView