最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is there a way to pass object to casper.js' evaluate()? - Stack Overflow

programmeradmin3浏览0评论

I saw this thread and look like there is no way to pass plex object to evaluate() !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();
});

I saw this thread and look like there is no way to pass plex object to evaluate() https://groups.google./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();
});
Share Improve this question edited May 30, 2013 at 21:43 HP. asked May 30, 2013 at 8:08 HP.HP. 19.9k56 gold badges159 silver badges258 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 14

Unfortunately, you can't pass a plex 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;
}

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.

发布评论

评论列表(0)

  1. 暂无评论