Search code examples
karateautomation-testing

Using retry with wait until in karate framework


I'm using karate framework version 1.3.1. Below is my code:

* def checkAction =
   """
       function(){
          try{
               waitForEnabled(AddToAddress).click();
               karate.log('Clicked add to address');
               var urlMatched = waitForUrl('my-url');
               karate.log('URL Matched:', urlMatched);
               return urlMatched;
          }
          catch(e){
               karate.log('Retrying due to error:', e)
               return false;
          }
       }
   """
* retry(10,1000).waitUntil(checkAction)

However, it seems an error occurred. The checkAction function is only executed once and is not retried even though it returns false. In my karate log file, the lines 'Clicked add to address'... and 'Retrying due to error:' are only recorded once instead of 10 times. Can someone help me?

I tried some other ways like using js commands instead of waitForUrl or waitForEnabled but they didn't work.


Solution

  • The waitUntil() function has to return null and not false to continue looping. This design is so that the returned value can be used by your main script for convenience, e.g. when you are trying to get the count of rows of a table, etc.

    Refer to the docs: https://github.com/karatelabs/karate/tree/master/karate-core#waituntilfunction

    So if you change return false to return null, I think it should start working.