isPresent 和 isDisplay方法有什么区别

What is the difference between the isPresent and isDisplayed methods

本文关键字:什么 区别 方法 isDisplay isPresent      更新时间:2023-09-26

我刚开始使用量角器来编写测试。我想知道isPresent()isDisplayed()方法有什么区别。

接口定义

  1. 是现在

  2. 是显示的

那么......它们在什么情况下是不同的呢?

如果

元素存在于页面中(在DOM中),则isPresent为真,但可以隐藏(显示:在css中无)仅当 isPresent 为真元素可见时,isDisplay 才为真

isDisplayed解析为元素是否可见,但如果它不在 DOM 中,则会引发异常。

isPresent解析它是否在 DOM 中,无论它是否实际可见。它不会引发异常。

以下代码可用于避免在 DOM 中找不到元素时 isDisplay引发的异常:

function isVisible(e) {
  var deferred = protractor.promise.defer();
  if (e) {
    e.isDisplayed().then(
      // isDisplayed Promise resolved
      function(isDisplayed) {
        deferred.fulfill(isDisplayed);
      },
      // Silencing the error thrown by isDisplayed.
      function(error) {
        deferred.fulfill(false);
      }
    );
  }
  else {
    deferred.reject(new Error('No element passed'));
  }    
  return deferred.promise;
}

即使是同时具有可见性和存在感的对象也可以在解析时传递,例如:

deferred.fulfill({
  visible: isDisplayed,
  present: true
});

但是,这不适用于期望语句。

IsPresent(): 如果元素存在于 DOM 中(可能隐藏也可能不隐藏),则返回 TRUE,否则返回 false

已显示():

  • 如果元素存在于 DOM 中并且可见,则返回 TRUE。
  • 如果元素存在于 DOM 中并且处于隐藏状态,则返回 FALSE。
  • 如果元素在 DOM 中不存在,则引发异常

如果在调用isDisplayed()时因为元素不在页面上而出错,即您得到NoSuchElementError: No element found using locator,然后这样做:

只需将.isDisplayed()包装在您自己的方法中并处理unresolved/rejected promise,如下所示:

function isTrulyDisplayed (elementToCheckVisibilityOf) {
  return elementToCheckVisibilityOf.isDisplayed().then(function (isDisplayedd) {
       return isDisplayedd;
}).then(null, function (error) {
  console.log('A NoSuchElement exception was throw because the element is NOT displayed so we return false');
  return false;
});  };

希望这对那里的人有所帮助!

isDisplay() 和 isPresent() 之间有一个主要区别。

isDisplay() - 您的元素存在于页面上,但它会显示。

isPresent() - 您的元素存在于页面的整个 DOM 中。可能它可以隐藏或不禁用,但存在。

当您需要验证正在搜索的特定元素时,不应使用 isPresent(),而是可以使用它来验证基于该元素存在的其他一些检查。