如何在Android中使用Espresso为Textview设置值 [英] How to set a value to the textview using Espresso in Android
问题描述
我正在为Fragment编写测试用例.我可以将文本设置为Edittext,但无法通过测试将setText设置为文本视图.请任何人帮助我解决此问题.以下是我用于编辑文本的代码.
I am writing the test cases for the Fragment. I can able to set a text to the Edittext but am unable to setText to the text view from testing. Please, anyone, help me to resolve this issue.Bellow is my code for edit text.
onView(withId(R.id.editText))
.perform(typeText("my text"), closeSoftKeyboard());
请帮助我如何在文本视图中设置文本.
Please help me how to set a text to the text view.
推荐答案
由于
查看先决条件:
View preconditions: 必须在屏幕上显示 必须支持输入法 Input method is not supported by 解决方案:您可以实现自己的视图 Solution : you can implement your own view 那你就可以做 这篇关于如何在Android中使用Espresso为Textview设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
TextView
(user cannot input values into it via keyboard or something else) so hence the issueViewAction
ViewAction
public static ViewAction setTextInTextView(final String value){
return new ViewAction() {
@SuppressWarnings("unchecked")
@Override
public Matcher<View> getConstraints() {
return allOf(isDisplayed(), isAssignableFrom(TextView.class));
// ^^^^^^^^^^^^^^^^^^^
// To check that the found view is TextView or it's subclass like EditText
// so it will work for TextView and it's descendants
}
@Override
public void perform(UiController uiController, View view) {
((TextView) view).setText(value);
}
@Override
public String getDescription() {
return "replace text";
}
};
}
onView(withId(R.id.editText))
.perform(setTextInTextView("my text"));