/**
 * Run "casperjs _run.js" to run all the tests in this folder.
 * Note: install CasperJS v1.9.2.
 */

var fs = require('fs');
var casper = require('casper').create({
    logLevel: 'debug'
});

// Set the timeout.
var TIMEOUT = 60000;

// Handle JS errors.
casper.once('page.error', function onError(msg, trace) {
    // Workaround for GUI.endGame() causing some errors here.
    if (!this.exists('#gameEnded')) {
        this.die('Test error: ' + msg + '\n\n' + trace);
    }
});

// Show log messages in the console.
casper.on('remote.message', function(message) {
    this.echo('[LOG]: ' + message);
});

// Find all the .html files in the tests directory.
var files = fs.list(fs.workingDirectory);
var tests = [];
files.forEach(function(file) {
    if (file.indexOf('.html', file.length - '.html'.length) !== -1) {
        tests.push(file);
    }
});

// Run all the tests!
casper.echo('Running ' + tests.length + ' tests with a timeout of '
    + TIMEOUT / 1000 + ' s.\n');

var runHtmlTest = function() {
    var path = this.getCurrentUrl();
    var filename = path.substring(path.lastIndexOf('/') + 1);
    this.echo('Running ' + filename + '...');
};

var onTimeout = function() {
    this.die('Test timed out.');
};

var testIndex = 0;
for (var i = 0; i < tests.length; i++) {
    if (i == 0) {
        casper.start(tests[i], runHtmlTest);
        casper.waitForSelector('#gameEnded', null, onTimeout, TIMEOUT);
    } else {
        casper.thenOpen(tests[i], runHtmlTest);
        casper.waitForSelector('#gameEnded', null, onTimeout, TIMEOUT);
    }
}

casper.run();
@@ -18,9 +18,13 @@
 rjs 
-_
+test 
 run.
@@ -131,400 +131,181 @@
 ');%0A
-var casper = require('casper').create(%7B%0A    logLevel: 'debug'%0A%7D);%0A%0A// Set the timeout.%0Avar TIMEOUT = 60000;%0A%0A// Handle JS errors.%0Acasper.once('page.error', function onError(msg, trace) %7B%0A    // Workaround for GUI.endGame() causing some errors here.%0A    if (!this.exists('#gameEnded')) %7B%0A        this.die('Test error: ' + msg + '%5Cn%5Cn' + trace);%0A    %7D%0A%7D);%0A%0A// Show log messages in the console.%0A
+%0A// Config.%0Avar TIMEOUT = 60000;%0Avar SHOW_CONSOLE_LOG = false;%0Avar DONE_SELECTOR = '#gameEnded';%0A%0A// Show log messages in the console if needed.%0Aif (SHOW_CONSOLE_LOG) %7B%0A    
 casp
@@ -344,24 +344,28 @@
 (message) %7B%0A
+    
     this.ech
@@ -388,19 +388,25 @@
 ssage);%0A
+    
 %7D);
+%0A%7D
 %0A%0A// Fin
@@ -715,17 +715,58 @@
 + ' test
-s
+' + (tests.length != 1 ? 's' : '') +%0A    '
  with a 
@@ -777,20 +777,16 @@
 out of '
-%0A   
  + TIMEO
@@ -799,16 +799,22 @@
 000 + ' 
+second
 s.%5Cn');%0A
@@ -822,514 +822,812 @@
 var 
-runHtmlTest = function() %7B%0A    var path = this.getCurrentUrl();%0A
+testIndex = 0;%0Afor (var i = 0; i %3C tests.length; i++) %7B%0A    casper.test.begin(tests%5Bi%5D, 3, function suite(test) %7B%0A  
     
+  
 var 
-filename = path.substring(path.lastIndexOf('/') + 1);%0A    this.echo('Running ' + filename + '...');%0A%7D;%0A%0Avar onTimeout = function() %7B%0A    this.die('Test timed out.');%0A%7D;%0A%0Avar testIndex = 0;%0Afor (var i = 0; i %3C tests.length; i++) %7B%0A    if (i == 0) %7B%0A        casper.start(tests%5Bi%5D, runHtmlTest);%0A        casper.waitForSelector('#gameEnded', null, onTimeout, TIMEOUT);%0A    %7D else %7B%0A        casper.thenOpen(tests%5Bi%5D, runHtmlTest);%0A
+errors = %5B%5D;%0A        casper.on('page.error', function onError(msg, trace) %7B%0A            errors.push(msg + ': ' + trace);%0A        %7D);%0A%0A        casper.start(tests%5Bi%5D);%0A%0A        // Wait for either the game to end or an error to appear.%0A        var timedOut = false;%0A        casper.waitFor(%0A            function check() %7B%0A                return errors.length != 0 %7C%7C this.exists(DONE_SELECTOR);%0A            %7D,%0A            function gameEnded() %7B%0A                test.assertEquals(%5B%5D%5D, %5B%5D);%0A                test.assertExists('#gameEnded');%0A            %7D,%0A            function timedOut() %7B%0A                timedOut = true;%0A                test.assertEquals(errors, %5B%5D);%0A       
         
 casp
@@ -1626,90 +1626,228 @@
     
-casper.waitForSelector('#gameEnded', null, onTimeout, TIMEOUT);%0A    %7D%0A%7D%0A%0Acasper.run();
+ test.assertExists('#gameEnded');%0A            %7D, TIMEOUT);%0A%0A        casper.then(function() %7B%0A            test.assertFalsy(timedOut);%0A        %7D);%0A%0A        casper.run(function() %7B%0A            test.done();%0A        %7D);%0A    %7D);%0A%7D

Rewrite run.js as a test