【问题标题】:Passing Protractor ElementFinder to deferred.fulfill() results in a promise containing a null value将 Protractor ElementFinder 传递给 deferred.fulfill() 会导致一个包含空值的承诺
【发布时间】:2014-06-14 11:21:38
【问题描述】:

我使用 Protractor elementfinder 作为参数调用 deferred.fulfill()。在完成上放置断点时,我可以看到元素查找器“solutionElement”不为空。承诺得到解决,我的“then”回调被执行。但是回调中“myElement”的值为null。

如果我不将元素查找器传递给完成,而是使用其他值(即“cnt”var),“myElement”变量将解析为“cnt”的实际值。

我想知道这个问题是否与我的完成调用在 then 回调中这一事实有关,但我不确定。

任何帮助/建议将不胜感激。谢谢你

it('Should select when clicked',function() {
      sb.getSolutionElementIndexByName("test").then(function(myElement) {
         myElement.click();
      });
});



SbPageObject.prototype.getSolutionElementIndexByName = function(name){
    var deferred = protractor.promise.defer();
    var cnt = 0;
    var notFulFilled = true;
    var allSolutions = this.allSolutions;

    allSolutions.count().then(function(solCnt){
        allSolutions.each(function (solutionElement) {
            solutionElement.element(by.className("sb-solution-name")).getText().
                then(function (solutionText) {

                    if (solutionText === name) {
                        console.log("*****Fullfiled");
                        deferred.fulfill(solutionElement);
                        notFulFilled = false;
                    }
                    //if this is the last element and it's still not a match, reject promise
                    else if (cnt + 1 == solCnt){
                        deferred.reject(new Error ("Solution " + name + " was not found."));
                    }
                    cnt++;
                });
        });
    });
    return deferred.promise;
};

【问题讨论】:

    标签: promise protractor


    【解决方案1】:

    我通常使用地图、过滤器、动作/断言模式。它看起来像这样:

    element.all(locator).map(function(elm, index) {
      // Get the value you are interested in finding and return the element too.
      return {
        elm: elm,
        text: elm.getText(),
        index: index
      };
    }).then(function(list) {
      // Find your text here. Otherwise fail.
      for(var i = 0; i<list.length; i++) {
        if(list[i].text === name) {
          return list[i].elm;
        }
      }
      throw new Error('Solution not found');
    }).then(function(elm) {
      // Perform an action on the element you found.
      elm.click();
    });
    

    Map 将在结果传递到链中的下一个then 之前完全解析所有承诺。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-03
      • 2021-10-24
      • 2016-03-02
      • 2018-04-01
      • 1970-01-01
      • 2018-10-29
      相关资源
      最近更新 更多