【问题标题】:Android testing. How to change text of a TextView using Espresso安卓测试。如何使用 Espresso 更改 TextView 的文本
【发布时间】:2015-09-29 14:32:31
【问题描述】:
使用 Espresso 更新 EditText 很容易,但在测试过程中我找不到更改文本的方法(例如使用 TextView.setText("someText"); 方法)。
ViewAction.replaceText(stringToBeSet);
不工作,因为它应该是EditText
【问题讨论】:
标签:
android
testing
textview
android-espresso
android-testing
【解决方案1】:
您可以考虑实现自己的 ViewAction。
这是来自 espresso 库的 replaceText viewaction 的修改版本,适用于 TextView。
public static ViewAction setTextInTextView(final String value){
return new ViewAction() {
@SuppressWarnings("unchecked")
@Override
public Matcher<View> getConstraints() {
return allOf(isDisplayed(), isAssignableFrom(TextView.class));
}
@Override
public void perform(UiController uiController, View view) {
((TextView) view).setText(value);
}
@Override
public String getDescription() {
return "replace text";
}
};
}
【解决方案2】:
Kotlin 版本的 @Be_Negative 很棒的答案,
由于在 Espresso 中没有用于在 TextView 上设置文本的默认 ViewAction,因此您必须创建自己的。
第 1 步:定义一个新的 ViewAction 以在 TextView 上设置文本,例如,
fun setTextInTextView(value: String): ViewAction {
return object : ViewAction {
override fun getConstraints(): Matcher<View> {
return CoreMatchers.allOf(ViewMatchers.isDisplayed(), ViewMatchers.isAssignableFrom(TextView::class.java))
}
override fun perform(uiController: UiController, view: View) {
(view as TextView).text = value
}
override fun getDescription(): String {
return "replace text"
}
}
}
然后将其用作,
onView(withId(R.id.my_text_view)).perform(setTextInTextView("Espresso is awesome"))
【解决方案3】:
您可以简单地将布局 TextView 添加到您声明的布局中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:textColor="#123456"
android:textSize="20dp"
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
在测试中编写代码
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
@Rule
public ActivityTestRule<MainActivity> mMain = new ActivityTestRule<> (MainActivity.class);
/*set text view in textView */
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";
}
};
}
现在你的方法就是这样
//need add your test case here
@Test
public void showTextView(){
delay(2000);
onView(withId(R.id.editText))
.perform(setTextInTextView("my text"));
delay(2000);
}
**现在您还可以添加延迟方法来显示模拟器或真实设备*
/* delay checking of this position */
private void delay(long i) {
try {
Thread.sleep(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
【解决方案4】:
您可以简单地在您声明的布局中添加布局 TextView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:textColor="#123456"
android:textSize="20dp"
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
** 在测试类中编写代码来检查文本是否显示在 textview 上**
@Test
public void checkUserId(){
Espresso.onView(withId(R.id.textView)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
}