dimanche 10 décembre 2017

Run multiple test.begin functions with AJAX calls inside sequentially

I'm using PhantomJS with CasperJS to make some testing for my API.

I have many functions that I need to call one after the other.

Inside these functions I have a code like this, e.g. funcX():

var funcX = function() {
   casper.test.begin('Testing', 1, function (test) {
      casper.thenOpen(urlAPI, {
        method: 'GET'
      }, function () {

        ...

        if (callback) {
          callback();
        }
      });

      test.done();
   });
};

Now I would like to make multiple calls to those functions sequentially, so each call must wait the end of the previous one, e.g.

func1();
func2();
func3();
// etc...

I think that one way to do that is to do something like this:

func1(function() {
   func2(function() {
      func3();
      // etc...
   });
});

but I found it to hard to read and maintain.

Do you have other alternatives?

I was thinking about jQuery $.ajax, so that I can use something like:

$.ajax(...).then(func1).then(func2).then(func3);

but I know that I need to use casper.evaluate() in order to use jQuery, I don't know if there is a way to use jQuery as normal js code.

I hope you can help with!

Thanks.

Aucun commentaire:

Enregistrer un commentaire