yarek
yarek

Reputation: 12064

How to use global variables in casperjs?

How can I use some global variables and them their content across .then ?

var casper = require('casper').create();
var globalVariable = "hello world";    
casper.start('http://163.172.110.7/be/get.php', function() {    
  var source = this.getPageContent();
  var json = JSON.parse(source);  
  console.log('step1:', globalVariable);

});
casper.then(function() {
    this.echo('step2', globalVariable);
    casper.exit();
});
casper.run();

Step1 : gives me "hello world"

Step2 : gives me ""

I also tried to use casper.globalVariable

Upvotes: 0

Views: 685

Answers (1)

davejagoda
davejagoda

Reputation: 2528

this.echo() is getting 2 arguments, and it only outputs the first one. console.log() concatenates all its arguments:

var casper = require('casper').create();
var globalVariable = "hello world";
casper.start('http://163.172.110.7/be/get.php', function() {
  var source = this.getPageContent();
  var json = JSON.parse(source);
  console.log('step1:', globalVariable);
});
casper.then(function() {
  this.echo('step2', globalVariable);
  console.log('step2:', globalVariable);
  casper.exit();
});
casper.run();

Upvotes: 1

Related Questions