Wednesday, 5 April 2017

Return a value from forEach in protractor - Managing closures

I have the following helper code from which I am trying to return the number of occurences of a string.

this.getActualFilteredStatusCount = function(strFilter){
    return this.getTotalRows().then(function(count){
        var totalCount = count;
        var statusElementsObjs = $$(CommonElements.filterElement);
        var occurence = 0;
        var index = 0;
         $$(CommonElements.filterElement).each(function(item){
            return item.getText().then(function(input){
                ++index;
                    if (input.indexOf(strFilter)>=0){
                         ++occurence;
                         console.log(occurence);//this worked fine
                    }   
                    if(index>=totalCount){
                        return occurence;
                    }
            });                             
        }); 
    });
}

This function only returns undefined or 0 but not the right value.

I read up a lot of articles on using Closures but just couldn't understand on breaking this problem up. Any suggestions or alternate ways of doing this would be helpful.



via Venkat Raman

No comments:

Post a Comment