Skip to content

Instantly share code, notes, and snippets.

@saadtazi
Created April 19, 2014 01:18
Show Gist options
  • Save saadtazi/11070632 to your computer and use it in GitHub Desktop.
Save saadtazi/11070632 to your computer and use it in GitHub Desktop.
grunt-mocha-webdriver bootstrap
/*jshint node:true*/
//test/e2e/spec/before.js
// i put my spec files in the same folder as before.js
'use strict';
before(function(done) {
// put the browser in global so we don't need 'self=this'
// using global vars is not ideal but I think it is acceptable for testing
global.browser = this.browser;
global.chaiAsPromised.transferPromiseness = this.wd.transferPromiseness;
// load some promise Methods
require('../helpers/some_async_methods')(this.wd);
require('../helpers/other_async_methods')(this.wd);
});
after(function(done) {
this.browser.quit().then(done, done);
});
module.exports = function(grunt) {
'use strict';
// load all grunt tasks
require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);
grunt.initConfig({
// ....
mochaWebdriver: {
options: {
testName: 'my test suite',
timeout: 1000 * 60,
// only used by phantomjs
reporter: 'spec',
require: [ 'test/e2e/init.js' ],
usePromises: true
},
phantom: {
src: ['test/e2e/spec/before.js', 'test/e2e/spec/*.spec.js'],
options: {
// phantomjs
usePhantom: true
}
},
saucelabs: {
src: ['test/e2e/spec/before.js', 'test/e2e/spec/*.spec.js'],
options: {
// SAUCELABS
testName: 'my test suite - ' + Date(),
username: 'xxx',
key: 'yyy-zzzz-ttt-uuuu',
tunnelFlags: ['--direct-domains', 'www.google.com,facebook.com,api.mixpanel.com'],
browsers: [
{
browserName: 'firefox',
version: '24',
platform: 'Windows XP'
},
{
browserName: 'firefox',
version: '26',
platform: 'Windows 8.1'
}
]
}
}
// ....
}
});
};
/*jshint node:true*/
// test/e2e/init.js
// in this file, I add some global requires
require('sugar');
// Chai
var chai = require('chai');
chai.use(
global.chaiAsPromised = require('chai-as-promised')
);
chai.should();
//test/e2e/helpers/some_async_methods.js
module.exports = function(wd) {
'use strict';
function injectScript(url) {
// this assumes you have jquery in your tested app
return global.browser.execute('$.getScript("' + url + '");');
}
wd.addPromiseMethod('injectScript', injectScript);
// ... add more
};
@binarykitchen
Copy link

Wonderful, many thanks!! Very helpful for me.

Can you explain me what the line https://gist.github.com/saadtazi/11070632#file-init-js-L12 is for? Never seen chai.use() before. I am using chai's expect here like this:

var chai = require('chai');
global.expect = chai.expect;

But I do not like this somehow ...

@binarykitchen
Copy link

And shouldn't you call done() somewhere along these lines?
https://gist.github.com/saadtazi/11070632#file-before-js-L16

@binarykitchen
Copy link

I prefer expect over should. And I am getting an ReferenceError: before is not defined in my case. Any ideas what else I should call? I think before() exists only for should?

@binarykitchen
Copy link

I prefer expect over should. And I am getting an ReferenceError: before is not defined in my case. Any ideas what else I should call? I think before() exists only for should?

Fixed, it was simply a question of the interface. I am using TDD, you BDD ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment