Breaking out of a Protractor .filter() or .map() loop

前端 未结 2 1132
抹茶落季
抹茶落季 2020-12-20 04:24

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!

         


        
2条回答
  •  暖寄归人
    2020-12-20 04:52

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

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

提交回复
热议问题