Skip to content

Commit

Permalink
added basic CI tests that spin up a phantom client to help identify c…
Browse files Browse the repository at this point in the history
…onfig issues
  • Loading branch information
Paul Cook committed Jul 24, 2014
1 parent 258b115 commit 49a4b41
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 19 deletions.
48 changes: 38 additions & 10 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,31 @@ module.exports = function(grunt) {
all: [
'Gruntfile.js',
'tasks/*.js',
'<%= nodeunit.tests %>',
'<%= nodeunit.tests %>'
],
options: {
jshintrc: '.jshintrc',
},
jshintrc: '.jshintrc'
}
},

// Before generating any new files, remove any previously-created files.
clean: {
tests: ['tmp'],
tests: ['tmp']
},

mochacli: {
options: {
colors: true,
'check-leaks': false,
ui: 'bdd',
reporter: 'spec',
timeout: 20000
},
dev: {
options: {
files: ['test/*.js']
}
}
},

selenium_start: {
Expand All @@ -41,10 +56,15 @@ module.exports = function(grunt) {
options: { }
},

// Unit tests.
nodeunit: {
tests: ['test/*_test.js']
},
connect: {
dev: {
options: {
port: 9000,
base: 'test/fixtures',
hostname: '*'
}
}
}

});

Expand All @@ -54,11 +74,19 @@ module.exports = function(grunt) {
// These plugins provide necessary tasks.
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-nodeunit');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks( 'grunt-mocha-cli' ); //testing alternative runner


// 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', ['clean', 'selenium_start' , 'selenium_stop' , 'selenium_phantom_hub', 'nodeunit' , 'selenium_stop']);
grunt.registerTask('test', ['clean',
'selenium_start' ,
'selenium_stop' ,
'selenium_phantom_hub',
'connect',
'mochacli',
'selenium_stop']);

// By default, lint and run all tests.
grunt.registerTask('default', ['jshint', 'test']);
Expand Down
19 changes: 12 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"contibutors": [
{
"name": "Paul Cook",
"email": "[email protected]",
"email": "[email protected]",
"url": "https://github.com/levexis"
}
],
Expand All @@ -24,7 +24,7 @@
}
],
"engines": {
"node": ">= 0.8.0"
"node": ">= 0.10.0"
},
"scripts": {
"test": "grunt test"
Expand All @@ -33,16 +33,21 @@
"phantomjs": "~1.9.7-3"
},
"devDependencies": {
"selenium-webdriver": "~2.40.0",
"grunt-contrib-jshint": "~0.10.0",
"chai": "^1.9.1",
"grunt": "~0.4.4",
"grunt-contrib-clean": "~0.5.0",
"grunt-contrib-connect": "^0.8.0",
"grunt-contrib-jshint": "~0.10.0",
"grunt-contrib-nodeunit": "~0.3.3",
"grunt": "~0.4.4"
"grunt-mocha-cli": "^1.9.0",
"selenium-webdriver": "^2.42.1"
},
"peerDependencies": {
"grunt": "~0.4.4"
},
"keywords": [
"gruntplugin","selenium","phantom"
"gruntplugin",
"selenium",
"phantom"
]
}
}
24 changes: 22 additions & 2 deletions tasks/selenium_webdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,39 @@ var spawn = require('child_process').spawn,
phantomLoc = __dirname,
seleniumServerProcess = null,
phantomProcess = null,
fs = require('fs' );
fs = require('fs' ),
os = require('fs' );

console.log ('Current location: [' + __dirname + ']' +
'Environment: [' + os.type() + ',' +
os.platform() + ',' +
os.arch() + ',' +
os.release() + ']');

// installed as module or locally?
if (fs.existsSync('jar')) {
console.log ('branch 1');
// mac?
selOptions.push ( 'jar/selenium-server-standalone-2.42.2.jar' );
phantomLoc += "/../node_modules/phantomjs/bin";
// this fixes a bug with ubuntu builds https://github.com/levexis/grunt-selenium-webdriver/issues/2
} else if (fs.existsSync('/../node_modules/phantomjs/bin/phantomjs')) {
console.log ('branch 2');
// ubuntu?
selOptions.push ( 'node_modules/grunt-selenium-webdriver/jar/selenium-server-standalone-2.42.2.jar' );
phantomLoc += "/../node_modules/phantomjs/bin";
} else {
} else if (fs.existsSync('/../../phantomjs/bin')) {
console.log ('branch 3');
// circle ci?
selOptions.push ( 'node_modules/grunt-selenium-webdriver/jar/selenium-server-standalone-2.42.2.jar' );
phantomLoc += "/../../phantomjs/bin";
} else {
// if adding new cases please identify environment so that changes can be maintained
throw new Error('Unable to find path to phantomjs, please run npm install and find the relative path for your system. Current location: [' + __dirname + ']' +
'Environment: [' + os.type() + ',' +
os.platform() + ',' +
os.arch() + ',' +
os.release() + ']');
}
/*
* starts phantom, called after grid has been established
Expand Down
85 changes: 85 additions & 0 deletions test/mocha-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Tests for grunt-selenium-webdriver, any problems are likely to be down to the relative install path of node modules on your system
* run grunt test --stack for more info on
*/
var chai = require('chai' ),
webdriver = require('selenium-webdriver' ),
FIXTURE = 'http://localhost:9000/index.html',
expect = chai.expect,
should = chai.should();

/**
* creates a webdriver client
* @param callBack or promise
*/
function createClient( callBack) {
// you can stick your saucelabs stuff here
var serverConfig = 'http://127.0.0.1:4445/wd/hub',
capabilities = {
silent: true, // maybe output more for tests?
browserName: 'phantomjs',
javascriptEnabled: true,
takesScreenshot: true,
databaseEnabled: false,
cssSelectorsEnabled:true,
webStorageEnabled: true
};

driver = new webdriver.Builder().
usingServer( serverConfig ).
withCapabilities( capabilities).
build();
if (typeof callBack === 'function') {
return callBack (driver);
} else if ( typeof callBack === 'object' && typeof callBack.resolve === 'function' ) {
return callBack.resolve( driver );
} else {
return driver;
}
}

/**
* gets inner html of main div element
* @param callBack or promise
*/
function getMain (driver, callBack) {
driver.get( url );
var cont = driver.findElement( webdriver.By.id("main") );
cont.getAttribute('innerHTML').then( function ( html ) {
callBack ( html );
});
}

describe ('test phantom hub', function () {
var _driver,
setDriver = function ( driver ) {
if (!driver) throw new Error ('driver not created');
_driver = driver;
}
before( function () {
createClient ( setDriver );
} );
it ('should return a page with a main div saying page loaded', function (done) {
expect ( _driver ).to.exist;
driver.get( FIXTURE );
var main = driver.findElement( webdriver.By.id("main") );
main.getAttribute('innerHTML').then( function ( conts ) {
conts.should.contain ('page loaded');
done();
});
});
it ('should change main div innerHTML to "main clicked" when clicked', function (done) {
expect ( _driver ).to.exist;
driver.get( FIXTURE );
var main = driver.findElement( webdriver.By.id("main") );
main.click()
.then( function () {
main.getAttribute('innerHTML').then( function ( conts ) {
conts.should.contain( main );
done();
});
});

done();
});
});

0 comments on commit 49a4b41

Please sign in to comment.