Jean-David YAPO
Jean-David YAPO

Reputation: 17

Casperjs waitForResource

I am making UI end-to-end tests and before the tests are started, we want to build the data required by the test. To do that, we use CasperJS and the waitForResource() function.

We want to wait until the resource has been created in the database. Like wait for http GET response status switch from 404 to 200 (or 201). It's an API REST.

The code:

casper.waitForResource(urls.rootBE + 'ratecards/default?api_key=' + user.apiKey + '&company=' + organization.id, function(resource) {
    utils.dump(resource);
});

This doesn't work. Do you have an idea how to do this?

Upvotes: 0

Views: 971

Answers (3)

jakequade
jakequade

Reputation: 45

Consider using either waitForSelector() on something the valid pages have that the others do not, or instead getting the page status codes and sorting appropriately, like so:

casper.on('resource.received', function(resource) {
    statuscode = resource.status;

});

if (statuscode < 404) {
    // Thing to do on valid pages
}

The value with waitForSelector() would be that you can set the timeout quite easily, as well as state an implicit action for if you hit the timeout.

A potential downside is that some sites/APIs have a redirect in place that broken URLs are redirected to a non-404 page, which would not be picked up in the second instance.

Upvotes: 0

catals.liu
catals.liu

Reputation: 33

Do you mean you want to get a jsonp or an ajax response? It is not easy right now.

If no wait when open a page in casperjs, meanwhile the current step is the last one, casperjs will exit,so a wait step must follows an open page step to check if your resource has been loaded.

Anytime you can not get the response body from a json or ajax request but only the response meta data.

You can hook ajax or jsonp request in client and receive the response body with casper.evaluate(),or if your request restful is just a "GET" method, you can use casperjs clientutils module with sendAJAX() method detail.

And this link may give you a big help.

Upvotes: 0

Mario Nikolaus
Mario Nikolaus

Reputation: 2406

Can you show me your code which is actually calling the API?

waitForResource actually just waits for resources that are invoked by page itself or by casperjs manually. This can be done to implement some kind of loop in which you will call API every 5 seconds and assert that the API will respond with 200 or 201.

edit:

First of you need to know how much time should you wait for resource to be in your database. Take the maximum time you calculated it could take. Then write function which will iterate over API call something like this.

var condition = false;
function loop(index,iteration){
if(condition) {
    return;
}
else if(index >= iteration) {
    casper.test.fail('Record not found');
    return;
}
casper.then(function(){
            this.thenOpen('http://www.webpage.com/api/record/543',function(response){
        if(response.status==200) {
            condition = true;
            this.test.pass("Record found");
        }
        this.echo(response.status);
    })
})
.then(function(){
    loop.call(this,index+1,iteration)
})
}

casper.test.begin('Test for record',1,function suite(test){
casper.start()
loop.call(this, 0, 20);
casper.run(function(){
    test.done();
})
})

If you need to leverage calls because of high traffic just use setTimeout.

Upvotes: 1

Related Questions