7

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 ?

narek
  • 1,026
  • 2
  • 12
  • 30

2 Answers2

9

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.

matt snider
  • 4,013
  • 4
  • 24
  • 39
  • i don't get your code to work. Following error occurs: **[ TypeError: 'undefined' is not a function (evaluating '['href1','href2'].each')]** Do you have an idea why it is undefined? Thanks in advance! – Hammerfaust May 06 '13 at 14:42
  • use `['href1','href2'].forEach` instead of `['href1','href2'].each` – Artjom B. Apr 26 '14 at 12:37
1

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

NiKo
  • 11,215
  • 6
  • 46
  • 56