美文网首页
【Espresso】withText

【Espresso】withText

作者: 秀叶寒冬 | 来源:发表于2019-08-28 22:36 被阅读0次
/**
   * Returns a matcher that matches {@link TextView}s based on text property value.
   *
   * <p><b>Note:</b> A View text property is never {@code null}. If you call {@link
   * TextView#setText(CharSequence)} with a {@code null} value it will still be "" (empty string).
   * Do not use a null matcher.
   *
   * @param stringMatcher <a
   *     href="http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matcher.html"><code>Matcher
   *     </code></a> of {@link String} with text to match
   */
  public static Matcher<View> withText(final Matcher<String> stringMatcher) {
    return new WithTextMatcher(checkNotNull(stringMatcher));
  }

由上述注释可知,该方法适用于TextView,所以使用onData时不能使用这条属性,即不能像下面那样调用

onData(withText("item1"));

它可以与onView配合使用,withText返回的是一个WithTextMatcher类型,WithTextMatcher继承自BoundedMatcher,重写了BoundedMatcher的一些方法,其中matchesSafely方法如下:

@Override
    protected boolean matchesSafely(TextView textView) {
      String text = textView.getText().toString();
      // The reason we try to match both original text and the transformated one is because some UI
      // elements may inherit a default theme which behave differently for SDK below 19 and above.
      // So it is implemented in a backwards compatible way.
      if (stringMatcher.matches(text)) {
        return true;
      } else if (textView.getTransformationMethod() != null) {
        CharSequence transformedText =
            textView.getTransformationMethod().getTransformation(text, textView);
        if (transformedText != null) {
          return stringMatcher.matches(transformedText.toString());
        }
      }
      return false;
    }

由此可见,withText最终找到的对象必须是TextView类型的。

相关文章

  • 【Espresso】withText

    由上述注释可知,该方法适用于TextView,所以使用onData时不能使用这条属性,即不能像下面那样调用 它可以...

  • Espresso之Matcher

    只要使用了Espresso,那么你一定不会对withId(R.id.xxx)和withText(R.string....

  • 友盟分享消息类型

    文本 new ShareAction(ShareActivity.this).withText("hello")....

  • 格格老师教你在家轻松玩咖啡

    意大利咖啡(Espresso)——“Espresso”原意为“快速”,Espresso Cafe则指在瞬间提炼出来...

  • Android UI 测试指南之 Espresso

    关于 Espresso Espresso 是一个简单好用的 Android UI 测试框架 Espresso 主要...

  • Espresso的详细使用

    本篇详细介绍了Espresso的使用方式. Espresso 测试代码位置和静态导入 Espresso 测试代码必...

  • Espresso之ViewAction

    Espresso的官方文档中提示,尽量使用Espresso提供的操作动作,来控制view,而且Espresso也确...

  • 初识Espresso

    本篇内容介绍了Espresso框架的基本使用. Espresso测试框架介绍 Espresso 是一个可以轻松编写...

  • Espresso UI测试

    Espresso测试框架 Espresso 是 Google 在 2013 年推出的 Android UI 测试的...

  • Espresso:自定义Idling Resource

    本篇文章翻译自Espresso: Custom Idling Resource Espresso的一个关键功能是测...

网友评论

      本文标题:【Espresso】withText

      本文链接:https://www.haomeiwen.com/subject/gcpvectx.html