突破量角器.filter()或.map()循环 [英] Breaking out of a Protractor .filter() or .map() loop

查看:88
本文介绍了突破量角器.filter()或.map()循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用量角器和黄瓜框架;如何突破.filter或.map循环?如果发现匹配,我不想继续进行迭代!

I'm using Protractor and cucumber framework; how do I break out of a .filter or .map loop? I do not want to continue to iterate further if I found a match!

Page.prototype.getElementByKey = function (key) {
      var foundElement = null;
      return someElement.all(by.css('.someClass')).map(function (rawItem, index) {
        var itemObject = new ItemObjectClass(rawItem);
        return itemObject.getItemKey().then(function (foundItemKey) {
          var matched = String(foundItemKey).trim() === String(key).trim();

         console.log(' Matched: { ' + matched + ' }  index {'+index+'}');
          //if we have a match break out of the .filter function
          if (matched) {
            foundElement = itemObject;
            throw new Error("Just our way of breaking out of .filter() above");
          }
        });
      }).then(function () {
        //callback
        throw new Error('\n!!!!!Callback should not be called; 
       this means that we could not find an element that matched the passed in key above');
      }, function (error) {
        //error
        console.log('\n*******************errorCallback was called; '+error);
        return foundElement;
      });
    };

上面的代码找到了元素,但是继续迭代直到最后,而不是在存在匹配项时停止并通过调用 errorCallback 函数中断。

The above code finds the element but continues to iterate until the end instead of stopping when there's a match and breaking out by calling the errorCallback function.

鉴于.map函数返回 一个可以解析为map函数返回的值数组的承诺 http://www.protractortest.org/#/api?view= ElementArrayFinder.prototype.map ,我利用了一个事实,即如果无法解决承诺,则承诺将调用其errCallback。

Given that .map function returns "a promise that resolves to an array of values returned by the map function" http://www.protractortest.org/#/api?view=ElementArrayFinder.prototype.map, I'm taking advantage of the fact that a promise will call its errCallback if the promise cannot be resolved.

通过抛出假错误,应调用 errorCallback ,从而退出.map循环

By throwing an a fake error, the errorCallback should be called and thereby break out of the .map loop.

不幸的是,它成功引发了错误,但继续循环而不是中断。我知道是因为,当我

Unfortunately, it successfully throws the error but continues with the loop instead of breaking out. I know that because when I


console.log( boolean + matched + and index + index);

console.log("boolean "+matched+" and index "+index);

我明白了:

matched: false index: 0
matched: false index: 1
matched: true index 2 //it should have stopped here since matched = true
matched false index 3 // this should NOT have printed

所以突破行不通吗?

推荐答案

您将返回单个元素,因此最好使用 .reduce

You are returning a single element, so .reduce would be preferable.

这里是一个使用示例,该示例返回文本为 mylink的第一个链接:

Here is a usage example to return the first link where the text is "mylink":

var link = element.all(by.css('a')).reduce(function (result, elem, index) {
    if(result) return result;

    return elem.getText().then(function(text){
        if(text === "mylink") return elem;
    });

}).then(function(result){
    if(!result) throw new Error("Element not found");
    return result;
});

这篇关于突破量角器.filter()或.map()循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆