Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added option to specify engines for executing tests, fixed casper integr... #67

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
* Licensed under the MIT license.
*/

'use strict';

module.exports = function(grunt) {

module.exports = function(grunt) {
'use strict';
// Project configuration.
grunt.initConfig({
jshint: {
Expand All @@ -18,17 +18,21 @@ module.exports = function(grunt) {
'tasks/*.js'
],
options: {
jshintrc: '.jshintrc',
},
jshintrc: '.jshintrc'
}
},

// Configuration to be run (and then tested).
casperjs: {
options: {
casperjsOptions: ["--foo=bar"]
casperjsOptions: ["--foo=bar"],
engines: {
phantomjs: true,
slimerjs: false
}
},
files: ['test/casperjs.js']
},
}

});

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
},
"dependencies": {
"phantomjs": "~1.9.1",
"casperjs": "~1.1"
"slimerjs": "~0.9.2",
"casperjs": "1.1.0-beta3",
"kew": "0.4.0"
},
"devDependencies": {
"grunt-contrib-jshint": "~0.1.1",
Expand Down
4 changes: 2 additions & 2 deletions tasks/casperjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
* Licensed under the MIT license.
*/

'use strict';

module.exports = function(grunt) {

module.exports = function(grunt) {
'use strict';
var casperjs = require('./lib/casperjs').init(grunt).casperjs;

grunt.registerMultiTask('casperjs', 'Run CasperJs tests.', function() {
Expand Down
71 changes: 60 additions & 11 deletions tasks/lib/casperjs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var path = require('path');
var fs = require('fs');
var kew = require('kew');

exports.init = function(grunt) {
var exports = {};
Expand All @@ -18,7 +19,20 @@ exports.init = function(grunt) {
}
var args = ['test'],
spawn = require('child_process').spawn,
phantomBinPath = require('phantomjs').path;
phantomBinPath = require('phantomjs').path,
slimerBinPath = require('slimerjs').path,
usePhantom = true,
useSlimer = false;

if (options.engines) {
if (options.engines.phantomjs === false) {
usePhantom = false;
}
//slimer doesn't run headless, so leave it off by default
if (options.engines.slimerjs === true) {
useSlimer = true;
}
}

if (options.casperjsOptions && options.casperjsOptions.length > 0) {
args = args.concat(options.casperjsOptions);
Expand All @@ -28,19 +42,54 @@ exports.init = function(grunt) {

grunt.log.writeln("Command: " + command);

process.env["PHANTOMJS_EXECUTABLE"] = phantomBinPath;

grunt.log.write('\nRunning tests from "' + filepath + '":\n');

grunt.util.spawn({
cmd: command,
args: args,
opts: {
// pipe stdout/stderr through
stdio: 'inherit'
var deferred = kew.defer();

if (usePhantom) {
grunt.log.writeln('Executing tests with PhantomJS');
grunt.log.writeln('PhantomJS path: ' + phantomBinPath);
process.env["PHANTOMJS_EXECUTABLE"] = phantomBinPath;
grunt.util.spawn({
cmd: command,
args: args,
opts: {
// pipe stdout/stderr through
stdio: 'inherit'
}
}, function(error, stdout, code) {
process.env["PHANTOMJS_EXECUTABLE"] = "";
deferred.resolve();
if (error) {
callback(error);
}
});
} else {
grunt.log.writeln('Skipping tests with PhantomJS');
deferred.resolve();
}

deferred.promise.then(function () {
args.push('--engine=slimerjs');
if (useSlimer) {
grunt.log.writeln('Executing tests with SlimerJS');
grunt.log.writeln('SlimerJS path: ' + slimerBinPath);
process.env["SLIMERJS_EXECUTABLE"] = slimerBinPath;
grunt.util.spawn({
cmd: command,
args: args,
opts: {
// pipe stdout/stderr through
stdio: 'inherit'
}
}, function(error, stdout, code) {
process.env["SLIMERJS_EXECUTABLE"] = "";
callback(error);
});
} else {
grunt.log.writeln('Skipping tests with SlimerJS');
callback();
}
}, function(error, stdout, code) {
callback(error);
});

};
Expand Down
66 changes: 47 additions & 19 deletions test/casperjs.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,50 @@
var casper = require('casper').create();
/*jshint strict:false*/
/*global CasperError, console, phantom, require, casper*/

casper.start('http://www.google.nl/', function() {
this.test.assertEquals(casper.cli.get('foo'), 'bar', "options were passed in successfully")
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.test.begin('default test', 4, function(test) {
casper.start('http://www.google.com/');

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.waitFor(function() {
return this.evaluate(function() {
return document.title === 'Google';
});
}, function() {
test.pass('google homepage title is the one expected');
}, function() {
test.fail('Failed finding title');
});

casper.run(function() {
this.test.renderResults(true);
});
if (casper.cli.get('foo') === 'bar') {
test.pass('options were passed in successfully');
} else {
test.fail('options were not passed in successfully');
}

casper.waitFor(function() {
return this.evaluate(function() {
return document.querySelectorAll('form[action="/search"]').length === 1;
});
}, function() {
test.pass('main form is found');
}, function() {
test.fail('Failed finding form');
});

casper.then(function () {
this.fillSelectors('form[action="/search"]', { 'input[name="q"]': 'foo' }, true);
});

casper.waitFor(function() {
return this.evaluate(function() {
return document.title === 'foo - Google Search';
});
}, function() {
test.pass('google search title is the one expected');
}, function() {
test.fail('Failed finding search title');
});

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