我正在尝试从我的UI视图中读取一个值。这是我用来找到我需要的字符串的方法。这仅返回textView ID,而不是内容。

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

如何返回我需要的字符串?

有帮助吗?

解决方案

如评论中所述,我们没有使这款动作成为浓缩咖啡API的第一类公民,以便鼓励作者明确地用行动和断言,并阻止有条件的逻辑在测试中。考虑到这一点,我鼓励您为您的服务器写作假装,以便您可以控制测试输入(并避免片质)并避免条件逻辑。

但是,如果你绝对必须得到这个值,你可以做这样的事情:

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());
  }
}
. 当然,如果你这样做了很多(你不应该这样),你可以通过将所有这些放入单独的类中来使其更好。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top