Promises inside the loops

I am struggling with this:

  • Click a button to get a dataset.
  • Checking the number of rows returned is what I expect
  • I need to run this 10 times, each time, I expect a different number of lines

The code snippet below does not work because "i" is not what I expect. How can I do this job?

for (var i = 0; i < subElements.length; ++i) {
    element(by.id('get-data')).click();

    // Check users returned
    element.all(by.id('users')).map(function (elm) {
        return elm;
    }).then(function (users) {
        expect(users.length).toBe(expectedRecords[i]);
        // Some more checks to be added later
    });
}
+4
source share
2 answers

What about:

for (var i = 0; i < subElements.length; ++i) {
    element(by.id('get-data')).click();

    var count = element.all(by.id('users')).count();
    expect(count).toBe(expectedRecords[i]);
}

Until you access iin then, this will be what you want, otherwise it iwill be the last value in your loop, and you will need to close.

EDIT: see Using a protractor with loops

+2
source

Using bluebird

var Promise = require('bluebird');

it('my test', function(done){
  var promises = subElements.map(function(subElm, i){
    return element(by.id('get-data')).click()
      .then(function(){
        // Check users returned
        return element.all(by.id('users')).map(function (elm) {
          return elm;
        })
      })
      .then(function(users) {
        expect(users.length).toBe(expectedRecords[i]);
      });
  });

  Promise.all(promises).nodeify(done);
})
0

Source: https://habr.com/ru/post/1570130/


All Articles