Reputation: 35
I wanted to assert a part of the 'text' i get from a textview and later store it in a string, but not sure how i am going to do this.
Following is the code snippet for reference :
private void validateFlightOverviewWidgetDate(int resId, String value, boolean outBound) throws Throwable {
if (ProductFlavorFeatureConfiguration.getInstance().getDefaultPOS() == PointOfSaleId.UNITED_STATES) {
onView(allOf(outBound ? isDescendantOfA(withId(R.id.package_outbound_flight_widget))
: isDescendantOfA(withId(R.id.package_inbound_flight_widget)),
withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE),
withId(resId)))
.check(matches(withText(containsString("Dec 22 "))));
I want to store the value of "Dec 22" in a string so that later i can use it for assertion.
Upvotes: 0
Views: 1956
Reputation: 3894
You may have to create a custom ViewAction
to help you to get text from TextView
:
public class GetTextAction implements ViewAction {
private CharSequence text;
@Override public Matcher<View> getConstraints() {
return isAssignableFrom(TextView.class);
}
@Override public String getDescription() {
return "get text";
}
@Override public void perform(UiController uiController, View view) {
TextView textView = (TextView) view;
text = textView.getText();
}
@Nullable
public CharSequence getText() {
return text;
}
}
Then you can get text by:
GetTextAction action = new GetTextAction();
onView(allOf(isDescendantOf(...), withId(...), withEffectiveVisibility(...)))
.perform(action);
CharSequence text = action.getText();
Though I'd not recommend to use this way for test assertion, it seems unconventional and awkward. Also, you don't really need to have isDescendantOf(...)
in your allOf
combination because of withId
, unless the id is not unique.
Upvotes: 1