0

am trying to check element on regular interval and click only if it is present. Some times this element appears in 2-3 minutes. If it is not present, i want to wait for few seconds and then refresh page

Here is what i tried:

for(var i = 1; i < 60; i++){
    element(that.proposalByOrderPath(num)).isPresent().then(function(result){
        if(result){
            console.log(i);
            return element(that.proposalByOrderPath(num)).click();
        }
        else{
            browser.sleep(15000);
            browser.refresh();
       }
    });
}

As an output, it prints 60 twice. It clicks on the element once but tries to look again for the element and throws "element not visible" error.

2 Answers2

0

This is a closure issue. For more understanding read the following-

How do JavaScript closures work?

http://www.w3schools.com/js/js_function_closures.asp

Community
  • 1
  • 1
Rahul Vig
  • 716
  • 1
  • 7
  • 24
0

We can solve async call in for loop with any of following 3 ways:

  1. Callback

    var a = function(callback){ //code
    callback(); }

    var b = function(){ for(loop details){ that.a(function(){ }) } }

  2. By recursive call of function instead of using for loop

  3. By self iteration

    for(loop details){ (function(i){ //your code })(i); }