diff --git a/Gruntfile.js b/Gruntfile.js index 428980e..c2432ab 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -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: { @@ -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: '*' + } + } + } }); @@ -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']); diff --git a/package.json b/package.json index 1081488..1e87404 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "contibutors": [ { "name": "Paul Cook", - "email": "paul@connectid.me", + "email": "levexis@googlemail.com", "url": "https://github.com/levexis" } ], @@ -24,7 +24,7 @@ } ], "engines": { - "node": ">= 0.8.0" + "node": ">= 0.10.0" }, "scripts": { "test": "grunt test" @@ -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" ] -} \ No newline at end of file +} diff --git a/tasks/selenium_webdriver.js b/tasks/selenium_webdriver.js index 4524e6c..25a6872 100644 --- a/tasks/selenium_webdriver.js +++ b/tasks/selenium_webdriver.js @@ -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 diff --git a/test/mocha-tests.js b/test/mocha-tests.js new file mode 100644 index 0000000..136e0a0 --- /dev/null +++ b/test/mocha-tests.js @@ -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(); + }); +}); \ No newline at end of file