HP.
HP.

Reputation: 19896

Is there a way to pass object to casper.js' evaluate()?

I saw this thread and look like there is no way to pass complex object to evaluate() https://groups.google.com/forum/#!topic/casperjs/x7I8LDFwFJ0

So if I write an object and want to share amongst different evaluate(), how do I do that?

Let say some silly object like this where I want to use getData function again and again:

var testObj = (function() {
  var a = 1;

  function test1(b) {
    return (a+b);
  }

  return {
    getData : function(arg) {
      return (test1(3) + arg);
    }
  }
})();

Is there a possible workaround?

UPDATE 1:

I mean to pass object with functions. Like below but it doesn't work (return null):

var casper = require('casper').create();

casper.start('about:blank', function() {

    var TestObj = function() {
      var a = 1;

      function test1(b) {
        return (a+b);
      }

      return {
        getData : function(arg) {
          return (test1(3) + arg);
        }
      }
    }

    var testObj = new TestObj();

    this.echo(casper.evaluate(function(myObject ) {
        return myObject.getData(100);
    }, testObj));
});

casper.run(function() {
    this.exit();
});

Upvotes: 4

Views: 5705

Answers (2)

jeroen
jeroen

Reputation: 502

You can use the the clientScripts option to pass along scripts such as jQuery — Can i use jQuery with CasperJS. You could probably do the same for custom scripts and have nice separation of code.

var casper = require('casper').create({
    clientScripts: ["includes/jquery.min.js", "lib/my-custom-script.js"]
});
casper.start(function () {
    this.evaluate(function () {
        window.customFunction();
    });
});

lib/my-custom-scripts.js:

window.customFunction = function () { /* do stuff */ };

Hope this helps.

Upvotes: 2

Hemerson Varela
Hemerson Varela

Reputation: 25802

Unfortunately, you can't pass a complex structure to evaluate(), because whatever arg passed to evaluate() is sort of JSON.parse(JSON.stringify(arg)).

But it doesn't mean that you are not able to pass another kind of objects.

Example about how pass a JSON Object to evaluate.

var casper = require('casper').create();

casper.start('about:blank', function() {
    var JSONObject = { arg1: 'val1' , arg2: 'val2' };
    this.echo(casper.evaluate(function(myObject ) {
        return JSON.stringify(myObject);
    }, JSONObject));
});

casper.run(function() {
    this.exit();
}); 

Example about how pass a basic Object to evaluate.

var casper = require('casper').create();

casper.start('about:blank', function() {

    obj = new Object();
    obj.param1  = "value1";
    obj.param2  = "value2";  

    this.echo(casper.evaluate(function(myObject ) {
        return JSON.stringify(myObject);
    }, obj));
});

casper.run(function() {
    this.exit();
});

Example about how pass a function with parameters to evaluate.

var casper = require('casper').create();

casper.start('about:blank', function() {
    var arg1 = "value1";
    var arg2 = "value2";
    this.echo(casper.evaluate(myFunction, arg1, arg2));
});

casper.run(function() {
    this.exit();
});

function myFunction(arg1, arg2) {
    return arg1 + "-" + arg2;
}

Upvotes: 14

Related Questions