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

后端 未结 3 415
盖世英雄少女心
盖世英雄少女心 2020-12-08 11:43

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:

相关标签:
3条回答
  • 2020-12-08 12:02

    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]);
        }
    });
    
    0 讨论(0)
  • 2020-12-08 12:03

    npm Q is the first thing to do then use requirejs on top of your script like that

          var Q = require('q');
    
          element.all(by.repeater('object in objects')).then(function (arr) {
              var promises = [];
              for (var i = 0; i < arr.length; i++) {
                  promises.push(arr[i].getText());
              }
    
              Q.all(promises).done(function (result) {
                  // print the results when the lookups and processing are done
                  console.log(result.length);
                  console.log(result);
              });
          });
    

    BTW I think my second option it is cleaner.

    0 讨论(0)
  • 2020-12-08 12:10

    Fixed using Q

    var Q = require('q');
    
    element.all(by.repeater('unit in units')).then(function (arr) {
        var promises = [];
        for (var i = 0; i < arr.length; i++) {
            promises.push(arr[i].getText());
        }
    
        Q.all(promises).done(function (result) {
            // print the results when the lookups and processing are done                
            console.log(result.length);
            console.log(result);                
        });
    });
    
    0 讨论(0)
提交回复
热议问题