I have a function which 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 gets displayed before the checkValue
is called.
Any suggestions?
You are getting undefined
since this is what each()
returns (returns nothing), implementation:
ElementArrayFinder.prototype.each = function(fn) {
return this.map(fn).then(function() {
return null;
});
};
Let's 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 side of the question, why do you need an index of an option in the select. Anyway, this is something you may consider switching to while dealing with select->option
constructions: