alesdario
alesdario

Reputation: 1894

Loop over all resources of a web page with CasperJS

How can I loop over all resources of a web page without Casper Default Time limit of 5 seconds ?

If i loop over Web Page' resources with

casper.waitForResource(function check(resource){

    ....
});

after 5000ms casper raise a test fail.

Upvotes: 0

Views: 246

Answers (1)

Artjom B.
Artjom B.

Reputation: 61952

casper.waitForResource is not for looping over resources. Instead, it is for waiting for a specific resource that has some property. If you use a check function, you will have access to resources that are seen up to the point when the matching resource is found.

What you want is resource.received or similar event listeners. The question is what you want to do with the resource information. Keep in mind that CasperJS and the underlying PhantomJS do not expose the resource content. You will need to download it separately with __utils__.sendAJAX inside of the page context.

If you want the resource list right in the test flow for resources after a specific action, you could do something like this:

var resources = [],
    collectResources = false;

casper.on('resource.received', function(resource) {
    if (!collectResources) { return; }
    resources.push(resource);
});

// later...
casper.then(function(){
    collectResources = true;
    this.click("#someAction");
}).wait(5000).then(function(){
    collectResources = false;
    resources.forEach(function(resource){
        // do something with it
    });
});

Upvotes: 1

Related Questions