1

I am able to get the count value with the following:

element.all(by.options('type as type for type in types')).then(function(elems){
    return elems.length;
})
.then(function(count){
    cnt = count;
});

Then later in the code I want to use cnt in a for loop where I also use closure:

for(var x = 1;x < cnt; x++){
    search_options(x);
}

function test(y){
    console.log('input'+y);
}

function search_options(input){
    it('tess', function(){
        test(input);

    });
}

The problem is that the for does not execute.

Any tips or suggestions, guidance is appreciated or point out any errors. I have read about IIFE but I find most samples use arrays, I believe 'cnt' has resolved

Unfortunately, I have to use the for loop. 'each' is not suitable.

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
edm
  • 53
  • 7

1 Answers1

1

The problem is that cnt would only be set when the promise would be resolved by the control flow mechanism. The for loop would be executed earlier.

Instead, define cnt as a promise and resolve it in your test:

cnt = element.all(by.options('type as type for type in types')).count();

cnt.then(function (actualCount) {
    for(var x = 1; x < actualCount; x++){
        search_options(x);
    } 
});

Also see: Using protractor with loops.

Also, I'm not exactly sure if dynamically creating its this way would actually work, here are some relevant threads:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Hi, thx a bunch, I am going to try it out and let you know how it goes. – edm Feb 13 '16 at 23:52
  • hi Alecxe, thanks for your help. in 'search_options(x)' there are a few statements, one of them does not execute / resolve: `code` element.all(by.options('type as type for type in types')).count().then(function(e){ e[input].click(); }); `code` any help or feedback is appreciated. – edm Feb 14 '16 at 13:05
  • @edmond okay, difficult to tell since I don't have any way to reproduce your problem. Are you sure the loop should start from 1 and not 0? Thanks. – alecxe Feb 14 '16 at 14:35
  • thanks for the pointer about the loop starting at 1. I changed it to 0 and it works. It was super helpful, much appreciated. Now, the loop is able to execute but not loop till the end. After looping about 10 times, the following message is return: `Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.` There are no BeforeEach and AfterEach, there are no arguments in any code it code. I have protractor version 3.0.0 and this is executed on Win10. – edm Feb 15 '16 at 03:46