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

查看:17
本文介绍了打破量角器 .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('
!!!!!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('
*******************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,我'm 利用这样一个事实,即如果无法解决承诺,承诺将调用其 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

所以突破没有任何想法?

so breaking out isn't working any ideas?

推荐答案

你返回的是单个元素,所以 .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天全站免登陆