Question

I am trying to read a value from my UI view. This is the method I use to find the string I need. This only returns the textView id, and not the content.

    public static String getSpeedIndex(){
    return onView(allOf(withId(R.id.slider_index),
            isDescendantOfA(withId(R.id.speed_number_column))))
            .toString();
    }

How can I return the string I need?

Was it helpful?

Solution

As mentioned in comments, we didn't make this action a first class citizen of the Espresso API in order to encourage test authors to be explicit with actions and assertions and discourage conditional logic in tests. With that in mind, I encourage you to look into writing a fake for your server, so that you can control the test input (and avoid flakiness) and avoid conditional logic.

However, if you absolutely must get the value, you can do something like this:

final AtomicReference<String> textFromView = new AtomicReference<String>();

onView(<matcher for your view>).perform(new ViewAction() {
  @Override
  public Matcher<View> getConstraints() {
    // TODO - at the very least, check that this is an instance of TextView 
  }

  @Override
  public String getDescription() {
    return "get text from text view"
  }

  @Override
  public void perform(UiController uiController, View view) {
    textFromView.set(((TextView) view).getText());
  }
}

Of course, if you do this a lot (which you shouldn't), you can make it nicer by putting all this in a separate class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top