What is the difference between the isPresent and isDisplayed methods

后端 未结 5 581
走了就别回头了
走了就别回头了 2020-12-08 06:40

I just started using Protractor to write tests. I am wondering what the difference is between the isPresent() and isDisplayed() methods.

5条回答
  •  春和景丽
    2020-12-08 07:21

    isDisplayed resolves to whether the element is visible or not, but throws an exception if it is not in the DOM.

    isPresent resolves to whether it is there in the DOM or not, regardless of whether it is actually visible or not. It doesn't throw an exception.

    The following code can be used to avoid the exception that isDisplayed throws if the element is not found in the DOM :

    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;
    }
    

    Even an object with both the visibility and presence can be passed while resolving, for example :

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

    However, this won't work well with expect statements.

提交回复
热议问题