Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

ghostreef commented Apr 9, 2013

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

@stkrzysiak

This comment has been minimized.

Copy link

stkrzysiak commented May 10, 2013

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

This comment has been minimized.

Copy link

colintoh commented Jun 16, 2013

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

This comment has been minimized.

Copy link

colintoh commented Jun 16, 2013

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

This comment has been minimized.

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

This comment has been minimized.

Copy link

andrewfaria commented Jul 10, 2013

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

This comment has been minimized.

Copy link

wsams commented Jul 22, 2013

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

@inalgnu

This comment has been minimized.

Copy link

inalgnu commented Jul 26, 2013

i have the same problem it runs only the first script

@fxmatthews

This comment has been minimized.

Copy link

fxmatthews commented Sep 23, 2013

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

@jarndt

This comment has been minimized.

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

This comment has been minimized.

Copy link

craigdallimore commented Nov 3, 2014

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

This comment has been minimized.

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

This comment has been minimized.

Copy link

vergissberlin commented Mar 3, 2016

You may change line one to:

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

This comment has been minimized.

Copy link

erichonkanen 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

This comment has been minimized.

Copy link

flancer64 commented Dec 23, 2016

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

This comment has been minimized.

Copy link

jfkhoury commented Mar 30, 2017

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

This comment has been minimized.

Copy link

PramodDutta commented Jun 21, 2017

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
You can’t perform that action at this time.