1

So I have some code and I would like to pass a variable outside so I can evaluate another page and inject the code from the previous page.

I'm not an expert here and I'm just not grasping the concept. Can anyone help me understand what I'm doing wrong?

var scheduleArray = [];

//blah blah removed code...everything works up to this point

casper.thenEvaluate(function(scheduleArray){

    console.log("##Your schedule is " + document.querySelector('form + div table').textContent );
    var rawSchedule = document.querySelector('form + div table').textContent;
    scheduleArray = rawSchedule.match(/((Monday)|(Tuesday)|(Wednesday)|(Thursday)|(Friday)|(Saturday)|(Sunday))([0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4})((5)|(C6)|(6)|(7H)|(7F)|(715)|(8F)|(10F)|(12F)|(1F)|(2H)|(C2)|(2))/gi);
    console.log("##scheduleArray");
    console.log(scheduleArray);

    for (i=0;i<scheduleArray.length;i++){
        console.log(scheduleArray[i]);
    }


},scheduleArray);

casper.then(function(scheduleArray){
    console.log("##scheduleArray");
//This loop contains no data
    for (i=0;i<scheduleArray.length;i++){
            console.log(scheduleArray[i]);
        }
},scheduleArray);

1 Answers1

0

I write a small sample code for you to illustrate how to passing result between evaluate and the casper script:

var casper = require('casper').create({
    verbose: true,
    logLevel: 'debug'
});

var array = []

casper.start('http://www.ecma-international.org/memento/TC39.htm');

casper.then(function() {
    array = casper.evaluate(function () {
        var nodes = document.querySelectorAll('a')
        var result = Array.prototype.map.call(nodes, function (div) {
            return div.href
        })
        return result
    });
});

casper.then(function () {
    casper.echo(array.length);
    casper.echo(array.join("\n"))
})

casper.run()

Output:

22
http://www.ecma-international.org/default.htm
http://www.ecma-international.org/contact/contact.html
http://www.ecma-international.org/sitemap/ecma_sitemap.html
http://www.ecma-international.org/memento/index.html
http://www.ecma-international.org/activities/index.html
http://www.ecma-international.org/news/index.html
http://www.ecma-international.org/publications/index.html
http://www.ecma-international.org/memento/history.htm
... ignore some lines

So, come to your code:

  1. The change of the argument of evaluate doesn't take that change to your global variable. That is to say, what ever you do to scheduleArray inside evaluate, the global variable scheduleArray remains the same.

  2. Inside evaluate, you should use console.log to log, but outside the evaluate, you should use casper.echo.

Sayakiss
  • 6,878
  • 8
  • 61
  • 107
  • Thank you for your response, however scheduleArray still does not return a result :( – user6571666 Jul 11 '16 at 01:47
  • @user6571666 I edit my solution, feel free to leave a comment if there is any further question. – Sayakiss Jul 11 '16 at 05:51
  • that is a wonderfully simple solution, thank you so much for your time, I understand now that you are using prototype and returning the value or the array as evaluate. – user6571666 Jul 11 '16 at 17:23
  • @user6571666 If there is no further question, please remember to accept my answer... – Sayakiss Jul 12 '16 at 03:09