0
fnReturnElements: function(pathElems ){
    var strArray= new Array();
    selenium.waitForElements(pathElems).then(function(elements) {


            for(i=0; i< elements.length;i++){

                var a= selenium.getText(elements[i]);

                    a.then(function(value){
                        strArray.push(value);
                    });
            }

    });
    console.log (strArray +" func");
    return strArray;
}

I get blank in console for strArray outside the loop. Anything to do with promises? Here 'elements' are a list of webElements.

After going through the other posts about callbacks and asynchronous functions i made the following changes -

fnReturnElements: function(xpath){
    var strArray= new Array();

    return selenium.waitForElements(xpath).then(function(elements) {            
            for(i=0; i< elements.length;i++){
                var a= selenium.getText(elements[i]);

                    a.then(function(value){
                        i++;
                        strArray.push(value);
                        console.log (i + value);
                        if ( i=== (elements.length*2 )){
                            return strArray;                                
                        }   
                    });                     
            }               
    });             
}

Invoking the function as -

    fnReturnElements ( xpath).then(function(value) {    
                        console.log (value);
    }); 

But still getting 'undefined' . [a.then(function(value)] line with a return before it, also does not change the output. Any pointers what i am doing wrong here?

KBM
  • 341
  • 4
  • 16
  • Thanks for the pointer.I understand that a callback needs to be used here, but being new to it, what i am failing is to properly organise my code syntax wise to get it running. Will keep trying. – KBM Nov 06 '15 at 10:02
  • Try to catch the idea from the following example. It was written for PhantomJS, but does exactly what you need. The idea is to chain promises in the FOR loop. function parseDealersExtra(dealerIds) { var df = Q.defer(); var tail = df.promise; dealerIds.forEach(function(dealerId) { tail = tail.then(function() { return parseDealerExtra(dealerId); }); }); df.resolve(); return tail; } – a-bobkov Nov 09 '15 at 15:39

0 Answers0