Iterate with each or eachThen and use wait()

12 views
Skip to first unread message

Andreas Adlung

unread,
Jul 10, 2018, 8:47:24 AM7/10/18
to CasperJS
Hi,

I'm running CasperJS to open a single-page-app, and then I try to iterate through a number of API calls which I fetch from an Ajax call. I cannot use the casper.wait or this.wait as it seems this only works, if it's happening in a small then() execution step, not if it's required within a do/while or each() function.


I use the following code, to run an Ajax call within the application (using the casperutils module).

var apiResponse = this.evaluate(function(apiSearchUrl, ajaxSettings, curPageStart, itemsPerPage, dateYesterday) {
var apiData = {
    "date": {"since":dateYesterday,"until":dateYesterday},
    "offset":curPageStart,
    "count":itemsPerPage
};
apiData = JSON.stringify(apiData);
var apiSearch = __utils__.sendAJAX(apiSearchUrl, 'POST', apiData, false, ajaxSettings);
apiSearch = JSON.parse(apiSearch);
return apiSearch;
}, {apiSearchUrl: apiSearchUrl, ajaxSettings: ajaxSettings, curPageStart: curPageStart, itemsPerPage: itemsPerPage, dateYesterday: dateYesterday});



In the response, there is an items array which I want to loop through, but I want to add a wait (random or fixed) after each of the calls to let it look more organic.

In general, the request flow is like this:

casper.then(function() {

  do {

    // Get the API response with all the pages and items (see above)
    ....
   
    this.each(apiItems, function(self, apiItem, i) {

        
var apiItemResponse = this.evaluate(function(apiItem, itemUrl, apiSearchUrl, ajaxSettings) {
            var apiItemUrl = apiSearchUrl + '/' + apiItem.id
            var apiItem = __utils__.sendAJAX(apiItemUrl, 'POST', null, false, ajaxSettings);
            apiItem=JSON.parse(apiItem);
            return apiItem;
        }, {apiItem:apiItem, itemUrl: itemUrl, apiSearchUrl: apiSearchUrl, ajaxSettings: ajaxSettings});

        // HERE I WANT TO ADD SOME WAIT FUNCTIONALITY...

    });

    // HERE I WANT TO ADD SOME WAIT FUNCTIONALITY AS WELL...

  } while(currentPage < lastPage)

}


My Solution was, to use eachThen instead of each, which runs through the Do/While quickly, but I can use it to add wait() to the steps. The issue there is that I lose the index and the root object, so I cannot access the current position where I am at and I cannot update the overall array.


Is there just a SIMPLE sleep() or similar, which I can use everywhere, no matter if in a single then() step, within a loop or anywhere else? Or can I add parameters to the eachThen, so my response.data is enriched with the index of the array or similar?

Regards,
Andy

Ken Soh

unread,
Jul 26, 2018, 1:27:18 AM7/26/18
to CasperJS
[ARCHIVE] - dropping a quick note that CasperJS, similar to PhantomJS, is not actively maintained for now.
Reply all
Reply to author
Forward
0 new messages