Undefined, returning function value to protractor

I have a function that returns a value:

checkValue = function(Name){
    var tempIndex=-1;
    var nameIndex=0;
    return selectElement.all(by.tagName('option')).each(function (element) {
        return element.getText().then(function(text){
            tempIndex++;
            if(text.toString().indexOf(Name)!=-1){
                nameIndex=tempIndex;
                return nameIndex;
            }else{
                return nameIndex;
            };
        });
    });

      

This is called in another function:

checkValue(Name).then(function(value){
    logger.info("value ::"+value);
});

      

When I call the above function, the value is displayed as undefined and in the logs it is displayed before the call checkValue

.

Any suggestions?

+3


source to share


1 answer


You get undefined

, since this each()

returns (returns nothing), the implementation is :

ElementArrayFinder.prototype.each = function(fn) {
  return this.map(fn).then(function() {
    return null;
  });
};

      

Approach it differently using map()

:

return selectElement.all(by.tagName('option')).map(function (option, index) {
    return {
        'text': option.getText(),
        'index': index
    };
}).then(function (options) {
    for (var i = 0; i < options.length; i++) {
        if (options[i].text === Name)
        {
            return options[i].index;
        }
    }
});

      




I'm still not sure about the motivation behind the question why do you need an option index in a select. In any case, this is what you might consider by moving on to working with constructs select->option

:

0


source







All Articles