我想从测试用例中设置文本视图的可见性.我正在使用espresso来测试UI.我使用viewAction将文本设置为文本视图.但我想为文本视图设置可见性.请任何人帮我解决这个问题.
这是我将文本设置为文本视图的代码.
public ViewAction setTextInTextView(final String value){
return new ViewAction() {
@SuppressWarnings("unchecked")
@Override
public void perform(UiController uiController, View view) {
((TextView) view).setText(value);
}
@Override
public Matcher<View> getConstraints() {
return allOf(isDisplayed(), isAssignableFrom(TextView.class));
}
@Override
public String getDescription() {
return "replace text";
}
};
}
解决方法:
试试这个,
public class MainActivityInstrumentationTest {
@Rule
public ActivityTestRule<MainActivity> activityTestRule = new ActivityTestRule<>(MainActivity.class);
@Test
public void validateEditText() {
onView(withId(R.id.out)).perform(setTextViewVisibitity(true));
// Just for viewing the results. Remove after use.
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
onView(withId(R.id.out)).perform(setTextViewVisibitity(false));
// Just for viewing the results. Remove after use.
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static ViewAction setTextViewVisibitity(final boolean value) {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isAssignableFrom(TextView.class);
}
@Override
public void perform(UiController uiController, View view) {
view.setVisibility(value ? View.VISIBLE : View.GONE);
}
@Override
public String getDescription() {
return "Show / Hide View";
}
};
}
}