Grunt task for custom CasperJS test runner
CasperJS test runner built for GruntJS
This was originally a fork of ronaldlokers/grunt-casperjs but with more
features:
silent
flag of grunt-casperjs is always on)This plugin requires Grunt >=0.4.0
This plugin requires phantomjs-prebuilt ~2.1.4
. It is specified as a peer
dependency, so be sure to install it manually.
If you haven’t used Grunt before, be sure to check out the Getting Started
guide, as it explains how to create a Gruntfile as well as install and use
Grunt plugins. Once you’re familiar with that process, you may install this
plugin with this command:
npm install --save-dev grunt-wendy
# DON'T FORGET THE phantomjs-prebuilt PEER DEPENDENCY
One the plugin has been installed, it may be enabled inside your Gruntfile with
this line of JavaScript:
grunt.loadNpmTasks('grunt-wendy');
In your project’s Gruntfile, add a section named wendy
to the data object
passed into grunt.initConfig()
.
grunt.initConfig({
wendy: {
options: {
async: 'eachSeries',
spawnOpts: null,
cli: [],
runner: 'test',
formatter: formatter, // function in tasks/lib/formatter.js
formatterOptions: {
whitespace: true,
filter: null
},
fail: ['failed'],
warn: ['dubious', 'skipped']
},
files: ['tests/e2e/**/*.js']
},
})
grunt.initConfig({
wendy: {
options: {
async: 'each',
cli: [
'--no-colors', // jenkins hates color
'--log-level=error', // hide casper logging
'--web-security=false' // phantomjs option
],
formatterOptions: {
whitespace: true,
// filter out useless headers since we're running async and they'll
// be out of order
filter: /(Test file:)|(tests executed)|(^\w*#)/
},
fail: ['failed'], // fail on failed
warn: ['dubious'] // don't fail on dubious
},
files: ['tests/e2e/**/*.js']
}
});
CasperJS CLI options can also be passed directly from the command line when
running grunt
. To use this feature, prefix the args with wendy
and they’ll
be passed through from grunt to casper. E.g.
grunt wendy:myTests --wendy-somearg --wendy-server=http://my.dev --baz=z
Will pass the args to casper as if you had run the command:
casper test --somearg --server=http://my.dev myTests/
Note that the wendy
is removed, and args not beginning with wendy, like baz
were not included.
Tasks are run in series by default (one after the other). To change how tests
are run, set the async option to a node async compatible value such as:
each
- run in paralleleachSeries
- run in series
wendy: {
options: {
async: 'each'
},
inparallel: ['tests/e2e/a/*.js'],
inparallel2: ['tests/e2e/b/*.js']
}
The task spawns casperJs binary instances as child processes of node. The rawchild_process.exec
options can be modified with the spawnOpts
task option:
wendy: {
options: {
spawnOpts: {
cwd: '../',
env: {
SERVER: 'QA',
API_KEY: 'abc123'
},
}
},
inparallel: ['tests/e2e/a/*.js'],
inparallel2: ['tests/e2e/b/*.js']
}
Of note is that you can change where you’re running CasperJS from, e.g.
in case your node_modules/
folder is for some reason not at the root of your
project and you want to run CasperJS from a different directory to so you can
require those modules.
Most likely you won’t need this or will just want the env
key values.
CasperJS CLI options (including user defined ones) can be passed in using
‘cli’ in the options object
wendy: {
options: {
cli: ['--foo=bar', '--no-colors']
},
files: ['tests/e2e/**/*.js']
}
See also Reading CLI options from grunt.
The task uses casper’s included test runner by default. If you’d like to use
a custom runner, casper allows this. Specify a new test runner JS file to therunner
option and wendy will hook it up.
wendy: {
options: {
runner: 'tests/e2e-runner.js'
},
files: ['tests/e2e/**/*.js']
}
This task captures all casper output and allows formatting of that output.
The formatter can be customized by passing in a function like so:
wendy: {
options: {
formatter: function (grunt, options, data) {
grunt.log.write(data);
}
},
files: ['tests/e2e/**/*.js']
}
The data
argument is always a string, a line from casper’s stdout or stderr.
The default formatter uses the clean
option as well. See its source here for
an example: formatter.js
This task tries to clean up the casper output whitespace and outputs aggregated
test results when multiple suites (multiple files) are run in a single task.
If you change the formatter
this option may not apply.
whitespace
: boolean — true to clean up casper whitespacefilter
: regex — anything that matches this filter will not be output
wendy: {
options: {
formatterOptions: {
// don't try to clean up whitespace
whitespace: false,
// don't output lines saying 'Test file:' name and the suite summary
filter: /(Test file:)|(tests executed)/
}
},
tests: ['tests/e2e/**/*.js']
}
Instead of failing on dubious tests or passing when tests are skipped, this
task only fails when a test actually fails. You can go back to default grunt
behavior, or customize your own using the fail
and warn
options.
The options take an array with values passed
, failed
, dubious
, andskipped
.
wendy: {
options: {
fail: ['failed'], // fail the task if any tests failed
warn: ['dubious'] // grunt warning when tests dubious
},
files: ['tests/e2e/**/*.js']
}
In lieu of a formal styleguide, take care to maintain the existing coding style.
Add unit tests for any new or changed functionality. Lint and test your code
using Grunt.
Follow the standards of the included eslint and markdownlint.
--wendy
cli flags to casperclean
to formatterOptions.whitespace
filter
to formatterOptionsfail
and warn
optionsformatter
optiongrunt.util.linefeed
for better Windows outputlib/
casperjs
to wendy
casperjsOptions
option key to cli
silent
option from unpublished release of casperjsclean
option is true (default)