I want to set visibility for the text view from test cases.I am using espresso for testing the UI. I used the viewAction to set the text to the text view. But I want to set visibility for the text view. Please, any one helps me to resolve this issue. Here is my code for setting the text to the text view.
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";
}
};
}
One simple way to check for a View or its subclass like a Button is to use method getVisibility from View class. I must caution that visibility attribute is not clearly defined in the GUI world. A view may be considered visible but may be overlapped with another view, for one example, making it hidden.
The most used assertion is the matches() assertion. It uses a ViewMatcher object to assert the state of the currently selected view. onView(...). check(matches(withText("Hello!")))
Try this,
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";
}
};
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With