Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

protractor - how to get the result of an array of promises into another array

I got an array of promises from this code: element.all(by.repeater('unit in units')), and I am finding it really difficult to get the data into another array:

element.all(by.repeater('unit in units')).then(function (arr) {
    var items = [];

    for (var i = 0; i < arr.length; i++) {
      arr[i].getText().then(function(text) {
        items.push(text);
      });
    }

   //PROBLEM ITEMS is Empty
   console.log(items);
});
like image 253
Acosta Avatar asked Feb 12 '14 18:02

Acosta


1 Answers

Managed to get the same result on a simpler way avoiding using Q and the repeater. Using the inbuilt map does the trick.

var tabs = element.all(by.css('.unitTabs li a')).map(function (elm) {
    return elm.getText();
});

tabs.then(function (result) {
    var sorted = _.sortBy(result, function (name) { return name; });
    for (var i = 0; i < result.length; i++) {
        expect(result[i]).toBe(sorted[i]);
    }
});
like image 84
Acosta Avatar answered Oct 11 '22 13:10

Acosta