0

To be honest, I'm new in automation js and nightwatchjs, but there is the problem with creating a loop in nightwatchjs. The task is to click on the button while an element is not visible, it can become visible at random after clicking. So, I'm struggling with the problem, how to add while loop in code in nightwatchjs, because my vision how to solve this problem is a loop, for example:

while(check for element)
.click('[Our_Button]')

And I tried to use this nightwatch js while loop but doesn't help, it was an error "Error while running clickUntilNotVisible command: browser.waitUntil is not a function" Maybe it is possible to use better way without the loop, but I didn't find anything.

Vlad
  • 3
  • 2
  • Don't use someone else's code; it's lazy and when there is something wrong with it you won't understand why or how it once worked or is now broken. You can do a while loop in Nightwatch.js. Think about the condition that needs to be met to exit the loop. Set a variable to check for that condition. At the end of each loop re-set the variable again by checking for that condition. – QualiT May 29 '18 at 05:01

2 Answers2

0

You could try using setInterval and clearInterval, read about them here. setInterval takes a function argument and a delay argument, and will execute the function every time the assigned delay interval passes. You can even make it so that when your condition is met, the interval will clear itself as described in this stack overflow answer.

Using this you should be able to check for your element and submit clicks until it appears on whatever interval you desire. Be advised that if it never becomes visible this will never stop executing, so you may want to also use setTimeout with a generous delay to clear out your interval or fail the test.

r3dw00d
  • 208
  • 2
  • 8
0

You can use .perform() method, and from inside you could make a function. Something like this:

this.api.perform(function (browser, done){
  while (condition) {
    //code block                     
  }
  done();
})

Works for me. Hope this helps.

Cheers

ochs.tobi
  • 3,214
  • 7
  • 31
  • 52