Skip to content

Instantly share code, notes, and snippets.

@n1k0
Created October 1, 2012 17:59
Show Gist options
  • Save n1k0/3813361 to your computer and use it in GitHub Desktop.
Save n1k0/3813361 to your computer and use it in GitHub Desktop.
CasperJS test cli hooks example

Put test1.js and test2.js into a tests/ directory, then run the suite:

$ casperjs test tests/ --pre=pre.js --includes=inc.js --post=post.js
Test file: /Users/nperriault/tmp/pre-inc/pre.js                                 
Hey, I'm executed before the suite.
Test file: /Users/nperriault/tmp/pre-inc/tests/test1.js                         
# this is test 1
Hi, I've been included.
PASS Subject is strictly true
Test file: /Users/nperriault/tmp/pre-inc/tests/test2.js                         
# this is test 2
Hi, I've been included.
PASS Subject is strictly true
Test file: /Users/nperriault/tmp/pre-inc/post.js                                
Hey, I'm executed after the suite.
PASS 2 tests executed, 2 passed, 0 failed.                                      

A more colorful output:

casper.sayHi = function sayHi() {
this.echo("Hi, I've been included.");
};
casper.echo("Hey, I'm executed after the suite.");
casper.test.done();
casper.echo("Hey, I'm executed before the suite.");
casper.test.done();
casper.test.comment('this is test 1');
casper.sayHi();
casper.test.assertTrue(true);
casper.test.done();
casper.test.comment('this is test 2');
casper.sayHi();
casper.test.assertTrue(true);
casper.test.done();
@ghostreef
Copy link

How come I don't need casper.start or casper.run?

@stkrzysiak
Copy link

Yes, what is the deal with start/run? It seems if I take a bunch of scripts that have start/run in them, and drop them in a folder, casper stops after executing the first script.

@colintoh
Copy link

Just got it working. If there is no "star"t, do not use "run". If there is "star"t, you must include "run" to get the script running.

@colintoh
Copy link

Just got it working. If there is no "star"t, do not use "run". If there is "star"t, you must include "run" to get the script running.

@ccnokes
Copy link

ccnokes commented Jul 6, 2013

Maybe this is stupidly obvious but exit() will stop the tests midway too. Calling it on your last test is fine though.

@andrewfaria
Copy link

casperjs test ./tests/* --cookies-file=/tmp/cookie.txt and variations on the path do not run more than the first file. Casper v1.0.2. Is this a bug?

root@SM-DATA:/home/imedia# ls tests/
1st-ut-login.js  ut-properties.js

@wsams
Copy link

wsams commented Jul 22, 2013

@myimedia same for me - casperjs test tests only runs the first script found.

@inelgnu
Copy link

inelgnu commented Jul 26, 2013

i have the same problem it runs only the first script

@fxmatthews
Copy link

Are there any news about it ? Or maybe a bypass ?

@jarndt
Copy link

jarndt commented Oct 1, 2013

in my case i was able to run multiple files in a folder (as documented by CasperJS - i.e. test.begin, casper.start, casper.run), but only when I removed casper = require('casper').create.

@craigdallimore
Copy link

If it helps anyone else who ends up here, this is what I felt was missing on this page - an example with casper.start and casper.run in it.

pre.js (which I was using for login purposes)

var url = casper.cli.options.url;
casper.start(url, function() {
  // do things
});

// casper.then do other things

casper.run(function() {
  this.test.done();
});

somefileInTheMiddle.js

// variables do not carry over from one file to the next
var url = casper.cli.options.url;

casper.start(url, function() {
  // pretty much like pre.js
});

// casper.waitFor, etc...

casper.run(function() {
  this.test.done();
});

post.js

// nothing else to see here
casper.run();

@ddeath
Copy link

ddeath commented Apr 11, 2015

Be aware that --post will run only once after all test, all files are do. Not after the suite() function

@vergissberlin
Copy link

You may change line one to:

casperjs test --pre=pre.js --includes=inc.js --post=post.js test*.js

@hoIIer
Copy link

hoIIer commented Jun 1, 2016

if anyone else runs into this, what fixed it for me was setting this.test.done() at the end of each test instead of this.exit() as someone else pointed out above...

@flancer64
Copy link

Thanks for the sample, guys.

I try to use --includes option and it does not act as expected: "will include the foo.js and bar.js files before each test file execution". One only include is performed, before pre scripts. Here is the sample code with customized inc.js:

casper.echo("Hey, I should be included before each test file.");
casper.sayHi = function sayHi() {
    this.echo("Hi, I've been included.");
}

Echo phrase is shown only once in output:

Hey, I should be included before each test file.
Test file: /home/alex/work/github/tmp_casperjs_includes/pre.js                  
Hey, I'm executed before the suite.
Test file: /home/alex/work/github/tmp_casperjs_includes/tests/test1.js          
# this is test 1
Hi, I've been included.
PASS Subject is strictly true
Test file: /home/alex/work/github/tmp_casperjs_includes/tests/test2.js          
# this is test 2
Hi, I've been included.
PASS Subject is strictly true
Test file: /home/alex/work/github/tmp_casperjs_includes/post.js                 
Hey, I'm executed after the suite.
PASS 2 tests executed in 0.061s, 2 passed, 0 failed, 0 dubious, 0 skipped.   

@jfkhoury
Copy link

Doing this with --engine=slimejs fails to open multiple tests, it only runs one test. Any way we can run multiple tests using SlimerJS?

@PramodDutta
Copy link

Anyway we can open multiple test cases in the SlimerJS ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment