Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CasperJS simultaneous requests

Tags:

casperjs

Lets say I have an array of urls. I dont want to use thenOpen function . Since it waits for every previous url to be loaded and it decreases load time .

 casper.each(hrefs,function(self,href){
      self.thenOpen(href,function(){ });
      self.then(function(){
        //  Selectors
     });

});

What methods would u use to spend much less compared to above method ? Would it be efficient to create multiple instances store in the db and then fetch ... but this is alot of headache . And also would like u also to answer in general would I have problems when I run multiple instances of the same js file simultaneously ?

like image 599
narek Avatar asked Nov 15 '12 20:11

narek


2 Answers

If you don't care about synchronizing behavior between all the URLs that you are opening, then you should start multiple instances of casper for each URL. Here is an example:

var casperActions = {
  href1: function (casper) {
    casper.start(address, function() {...});
    // tests and what not for href1
    casper.run(function() {...});
  },
  href2: function (casper) {
    casper.start(address, function() {...});
    // tests and what not for href2
    casper.run(function() {...});
  },
  ...
};

['href1', 'href2', ...].each(function(href) {
  var casper1 = require('casper').create();
  casperActions[href](casper);
});

Each instance would run independently of each other, but it would allow you to hit many URLs simultaneously.

like image 126
matt snider Avatar answered Oct 20 '22 13:10

matt snider


If you want to wait for each operation being finished to sequence an array of steps, look at this sample shipping with casper: https://github.com/n1k0/casperjs/blob/1.0/samples/multirun.js

like image 38
NiKo Avatar answered Oct 20 '22 15:10

NiKo