【问题标题】:Espresso check view either doesNotExist or not isDisplayedEspresso 检查视图 doesNotExist 或 not isDisplayed
【发布时间】:2016-12-23 08:04:38
【问题描述】:

以下语句不起作用,因为 doesNotExist() 返回的是 ViewAssertion 而不是匹配器。没有try-catch,有什么方法可以让它工作吗?

.check(either(matches(doesNotExist())).or(matches(not(isDisplayed()))));

【问题讨论】:

  • 你不喜欢try-catch的什么?
  • @SimonSchnell 它不太适合 hamcrest 匹配器的语法。我认为可能有更漂亮的解决方案。
  • 在我看来这个说法是不正确的。 (not(isDisplayed())) 用于检查层次结构中存在的视图是否未显示,但doesNotExist() 验证层次结构中根本不存在视图。他们互相矛盾。
  • .check(either(matches(is(doesNotExist()))).or(matches(not(isDisplayed())))); `
  • @piotrek1543 不能应用于 ViewAssertion,需要 hamcrest 匹配器。

标签: android android-espresso hamcrest


【解决方案1】:

如果要检查层次结构中是否不存在视图,请使用以下断言。

ViewInteraction.check(doesNotExist());

如果要检查层次结构中是否存在视图但未显示给用户,请使用以下断言。

ViewInteraction.check(matches(not(isDisplayed())));

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,我的一个视图最初不会有某个视图,但可以添加它并稍后隐藏它。 UI 处于哪种状态取决于后台活动是否被破坏。

    我最终只是写了一个关于 dosNotExist 实现的变体:

    public class ViewAssertions {
        public static ViewAssertion doesNotExistOrGone() {
            return new ViewAssertion() {
                @Override
                public void check(View view, NoMatchingViewException noView) {
                    if (view != null && view.getVisibility() != View.GONE) {
                        assertThat("View is present in the hierarchy and not GONE: "
                                   + HumanReadables.describe(view), true, is(false));
                    }
                }
            };
        }
    }
    

    【讨论】:

      【解决方案3】:

      not(isDisplayed) 并不完美,因为视图可能会显示在 ScrollView 的屏幕下方。

      简单检查view.getVisibility() != View.GONE 也不是100% 的解决方案。如果视图父级被隐藏,则视图被有效地隐藏,因此测试应该通过场景。

      我建议检查视图及其父级是否可见:

      fun isNotPresented(): ViewAssertion = object : ViewAssertion {
          override fun check(view: View?, noViewFoundException: NoMatchingViewException?) {
              if (view != null) {
                  if (view.visibility != View.VISIBLE) {
                      return
                  }
                  var searchView: View = view
                  while (searchView.parent != null && searchView.parent is View) {
                      searchView = searchView.parent as View
                      if (searchView.visibility != View.VISIBLE) {
                          return
                      }
                  }
                  assertThat<Boolean>(
                      "View is present in the hierarchy and it is visible" + HumanReadables.describe(view),
                      true,
                      `is`(false)
                  )
              }
          }
      }
      

      【讨论】:

      • 我认为处理您的特定示例的更好方法是实际使测试滚动视图。 not(isDisplayed()) 失败是因为 espresso 测试是从用户的角度构建的,如果视图在屏幕之外,则不会显示。如果即使在您滚动之后,视图仍然不可见,那么您就有问题了。如果您想确保用户看到您希望他看到的内容,检查父视图的可见性并不是一个好方法。
      猜你喜欢
      • 2016-01-19
      • 1970-01-01
      • 1970-01-01
      • 2017-06-23
      • 2020-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-08
      相关资源
      最近更新 更多