CasperJS: The correct way to close page instances during a loop

I have a long script that executes several different links to do its job. I came across a script when PhantomJS ran out of resources due to the fact that the page objects were not garbage collected because I reused them. The following is a simple example. I close () the page object and create a new one at the end of each cycle of the cycle, but after I do this, Casper just freezes. What is the right way to do this?

var links = ['http://www.google.com', 'http://www.yahoo.com'];

var casper = require('casper').create({
    logging: 'error',
    pageSettings: {
        webSecurityEnabled: false
    }
});

casper.start('http://www.amazon.com', function () {
    this.echo(this.getTitle());
});

casper.eachThen(links, function(item) {
    var url = item.data;
    this.open(url).then(function (){
        this.echo(this.getTitle());
    }).then(function (){
        casper.page.close();
        casper.page = require('webpage').create();
    });
});

casper.run(function (){
    this.echo('Done');
});

This will lead to the conclusion

Amazon.com: Online Shopping for Electronics, Apparel, Computers, Books, DVDs & more
Google

and then just hang. What am I missing?

+4
source share
1 answer
var casper = require('casper').create();
var urls = ['http://google.com/', 'http://yahoo.com/'];

casper.start().eachThen(urls, function(response) {
  this.thenOpen(response.data, function(response) {
      this.echo('\n'+this.getTitle());
  });
});

casper.run(function(){
    this.die('\n'+'Done');
});

. ... , casperjs , .

+1

All Articles