Permalink
Browse files

Rewritten the plugin based on the new grunt plugin template and remov…

…ed support for versions of grunt lower then 0.4.0
  • Loading branch information...
ronaldlokers committed Mar 2, 2013
1 parent 0108212 commit 7903a3237f0002938840a919e41bb67903e0503e
Showing with 177 additions and 51 deletions.
  1. +45 −0 Gruntfile.js
  2. +1 −1 LICENSE-MIT
  3. +54 −11 README.md
  4. +0 −2 bin/grunt-casperjs
  5. +17 −11 package.json
  6. +31 −25 tasks/casperjs.js
  7. +8 −1 tasks/lib/casperjs.js
  8. +21 −0 test/casperjs.js
View
@@ -0,0 +1,45 @@
+/*
+ * grunt-casperjs
+ * https://github.com/ronaldlokers/grunt-casperjs
+ *
+ * Copyright (c) 2013 Ronald Lokers
+ * Licensed under the MIT license.
+ */
+
+'use strict';
+
+module.exports = function(grunt) {
+
+ // Project configuration.
+ grunt.initConfig({
+ jshint: {
+ all: [
+ 'Gruntfile.js',
+ 'tasks/*.js'
+ ],
+ options: {
+ jshintrc: '.jshintrc',
+ },
+ },
+
+ // Configuration to be run (and then tested).
+ casperjs: {
+ files: ['test/casperjs.js']
+ },
+
+ });
+
+ // Actually load this plugin's task(s).
+ grunt.loadTasks('tasks');
+
+ // These plugins provide necessary tasks.
+ grunt.loadNpmTasks('grunt-contrib-jshint');
+
+ // Whenever the "test" task is run, first clean the "tmp" dir, then run this
+ // plugin's task(s), then test the result.
+ grunt.registerTask('test', ['casperjs']);
+
+ // By default, lint and run all tests.
+ grunt.registerTask('default', ['jshint', 'test']);
+
+};
View
@@ -1,4 +1,4 @@
-Copyright (c) 2012 Ronald Lokers
+Copyright (c) 2013 Ronald Lokers
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
View
@@ -1,14 +1,57 @@
-# grunt.js task for CasperJS
+# grunt-casperjs
-With this grunt.js task you can run tests with CasperJS.
+> With this grunt.js task you can run tests with CasperJS.
-1. [Install CasperJS](http://casperjs.org/installation.html)
-2. Call `grunt.loadNpmTasks('grunt-casperjs')` in your gruntfile.
-3. Add the following to your configuration file:
+## Getting Started
- ```javascript
- casperjs: {
- files: ['tests/casperjs/**/*.js']
- },
- ```
-4. Run `grunt` or `grunt casperjs` and your CasperJS test are running in grunt.js :)
+First [Install CasperJS](http://casperjs.org/installation.html).
+
+This plugin requires Grunt `~0.4.0`
+
+If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
+
+```shell
+npm install grunt-casperjs --save-dev
+```
+
+One the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
+
+```js
+grunt.loadNpmTasks('grunt-casperjs');
+```
+
+## The "casperjs" task
+
+### Overview
+In your project's Gruntfile, add a section named `casperjs` to the data object passed into `grunt.initConfig()`.
+
+```js
+grunt.initConfig({
+ casperjs: {
+ options: {
+ // Task-specific options go here.
+ },
+ your_target: {
+ // Target-specific file lists and/or options go here.
+ },
+ },
+})
+```
+
+### Usage Examples
+
+#### Default Options
+
+```js
+grunt.initConfig({
+ casperjs: {
+ options: {},
+ files: {
+ ['tests/casperjs/**/*.js']
+ },
+ },
+})
+```
+
+## Contributing
+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](http://gruntjs.com/).
View
@@ -1,2 +0,0 @@
-#!/usr/bin/env node
-require('grunt').npmTasks('grunt-casperjs').cli();
View
@@ -1,12 +1,12 @@
{
"name": "grunt-casperjs",
"description": "With this grunt.js task you can run tests with CasperJS.",
- "version": "1.0.4",
+ "version": "1.0.5",
"homepage": "https://github.com/ronaldlokers/grunt-casperjs",
"author": {
"name": "Ronald Lokers",
"email": "ronaldlokers@me.com",
- "url": "http://ronaldlokers.nl"
+ "url": "http://www.ronaldlokers.nl"
},
"repository": {
"type": "git",
@@ -21,18 +21,24 @@
"url": "https://github.com/ronaldlokers/grunt-casperjs/blob/master/LICENSE-MIT"
}
],
- "main": "tasks/casperjs.js",
- "bin": "bin/grunt-casperjs",
+ "main": "Gruntfile.js",
"engines": {
- "node": "*"
+ "node": ">= 0.8.0"
},
- "dependencies": {
- "grunt": ">=0.3.7",
- "grunt-contrib-lib": "~0.3.0"
+ "scripts": {
+ "test": "grunt test"
},
"devDependencies": {
- "grunt": ">=0.3.7",
- "grunt-contrib-lib": "~0.3.0"
+ "grunt-contrib-jshint": "~0.1.1",
+ "grunt-contrib-clean": "~0.4.0",
+ "grunt-contrib-nodeunit": "~0.1.2",
+ "grunt": "~0.4.0"
},
- "keywords": ["grunt.js", "gruntplugin", "casperjs"]
+ "peerDependencies": {
+ "grunt": "~0.4.0"
+ },
+ "keywords": [
+ "gruntplugin",
+ "casperjs"
+ ]
}
View
@@ -1,35 +1,39 @@
+/*
+ * grunt-casperjs
+ * https://github.com/ronaldlokers/grunt-casperjs
+ *
+ * Copyright (c) 2013 Ronald Lokers
+ * Licensed under the MIT license.
+ */
+
+'use strict';
+
module.exports = function(grunt) {
- 'use strict';
-
+
var casperjs = require('./lib/casperjs').init(grunt).casperjs;
- // Create a new multi task.
- grunt.registerMultiTask('casperjs', 'This triggers casperjs.', function() {
- // Tell grunt this task is asynchronous.
+ grunt.registerMultiTask('casperjs', 'Run CasperJs tests.', function() {
+ // Merge task-specific and/or target-specific options with these defaults.
var done = this.async(),
- filepaths = [],
- helpers = require('grunt-contrib-lib').init(grunt),
- options = helpers.options(this);
-
- grunt.verbose.writeflags(options, 'Options');
+ filepaths = [],
+ options = this.options();
- // grunt 0.3.x
- if (this.file) {
- grunt.file.expandFiles(this.file.src).forEach(function(filepath) {
- filepaths.push(filepath);
+ // Iterate over all specified file groups.
+ this.files.forEach(function(file) {
+ // Concat specified files.
+ file.src.filter(function(filepath) {
+ // Warn on and remove invalid source files (if nonull was set).
+ if (!grunt.file.exists(filepath)) {
+ grunt.log.warn('Source file "' + filepath + '" not found.');
+ return false;
+ } else {
+ filepaths.push(filepath);
+ return true;
+ }
});
+ });
- // grunt 0.4.x
- } else {
- this.files.forEach(function(file) {
- filepaths.push(file.src);
- });
- }
-
- // grunt.utils changed to grunt.util in 0.4.x
- var async = (grunt.util || grunt.utils).async;
-
- async.forEachSeries(
+ grunt.util.async.forEachSeries(
filepaths, function(filepath, callback) {
casperjs(filepath, options, function(err) {
if (err) {
@@ -39,5 +43,7 @@ module.exports = function(grunt) {
});
},
done);
+
});
+
};
View
@@ -2,6 +2,7 @@ exports.init = function(grunt) {
var exports = {};
exports.casperjs = function(filepath, options, callback) {
+
var command = 'casperjs test',
exec = require('child_process').exec;
@@ -10,18 +11,23 @@ exports.init = function(grunt) {
if (options.xunit) {
command += ' --xunit=' + options.xunit;
}
+
if (options.direct) {
command += ' --direct';
}
+
if (options.includes) {
command += ' --includes=' + options.includes.join(',');
}
+
if (options.logLevel) {
command += ' --log-level=' + options.logLevel;
}
+
if (options.pre) {
command += ' --pre=' + options.pre.join(',');
}
+
if (options.post) {
command += ' --post=' + options.post.join(',');
}
@@ -33,15 +39,16 @@ exports.init = function(grunt) {
function puts(error, stdout, stderr) {
grunt.log.write('\nRunning tests from "' + filepath + '":\n');
grunt.log.write(stdout);
- //grunt.log.error( stderr );
if ( error !== null ) {
callback(error);
} else {
callback();
}
}
+
exec(command, puts);
+
};
return exports;
View
@@ -0,0 +1,21 @@
+var casper = require('casper').create();
+
+casper.start('http://www.google.nl/', function() {
+ this.test.assertTitle('Google', 'google homepage title is the one expected');
+ this.test.assertExists('form[action="/search"]', 'main form is found');
+ this.fill('form[action="/search"]', {
+ q: 'foo'
+ }, true);
+});
+
+casper.then(function() {
+ this.test.assertTitle('foo - Google zoeken', 'google title is ok');
+ this.test.assertUrlMatch(/q=foo/, 'search term has been submitted');
+ this.test.assertEval(function() {
+ return __utils__.findAll('h3.r').length >= 10;
+ }, 'google search for "foo" retrieves 10 or more results');
+});
+
+casper.run(function() {
+ this.test.renderResults(true);
+});

0 comments on commit 7903a32

Please sign in to comment.