Skip to content

Commit b3c87ea

Browse files
committed
Merge pull request #451 from subtleGradient/subtlegradient/browser-testing
Browser testing.
2 parents ee8bb07 + 241d57a commit b3c87ea

22 files changed

Lines changed: 562 additions & 277 deletions

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ docs/js/live_editor.js
2222
docs/js/examples
2323
docs/downloads
2424
examples/shared/*.js
25-
25+
test/the-files-to-test.generated.js

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ env:
1313
- secure: qPvsJ46XzGrdIuPA70b55xQNGF8jcK7N1LN5CCQYYocXLa+fBrl+fTE77QvehOPhqwJXcj6kOxI+sY0KrVwV7gmq2XY2HZGWUSCxTN0SZlNIzqPA80Y7G/yOjA4PUt8LKgP+8tptyhTAY56qf+hgW8BoLiKOdztYF2p+3zXOLuA=
1414
# SECRET_TOKEN
1515
- secure: dkpPW+VnoqC/okhRdV90m36NcyBFhcwEKL3bNFExAwi0dXnFao8RoFlvnwiPlA23h2faROkMIetXlti6Aju08BgUFV+f9aL6vLyU7gUent4Nd3413zf2fwDtXIWIETg6uLnOpSykGKgCAT/hY3Q2oPLqOoY0OxfgnbqwxkxljrE=
16+
- secure: eHMg4yQWJ+5zFxNcjVwpdpcbZCQMaSByqJy0wwQ2XOOMItdCvSfO/fa+mYoVND/9Orsi5kvXC9tx4sYHyth5hLrvO+bghYjiayrbxYYrxso6G/aU8Pttc7s/qk3I4zadLlua1kGy2TauByxS6nt9nUDe0FDaJKA9k1QXVlpq5Ac=
17+
- secure: v65jUPPNBvJg1xhj4cY7wMKinCESUseHsW1d2gp68SluFHHyjWZOvXYXZdABqB/1uNmQxjbUkwkdSNAOn5k7ppjRcRnlAH2uu1xDqXrGp6cW4MfNPchrAe+FcUMtse/3THGJj8SZ9IQWIl+BHv4XASQ7FwqCR8cwlOfhsqdfch8=

Gruntfile.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ var exec = require('child_process').exec;
44
var jsxTask = require('./grunt/tasks/jsx');
55
var browserifyTask = require('./grunt/tasks/browserify');
66
var populistTask = require('./grunt/tasks/populist');
7-
var phantomTask = require('./grunt/tasks/phantom');
7+
var webdriverPhantomJSTask = require('./grunt/tasks/webdriver-phantomjs');
8+
var webdriverJasmineTasks = require('./grunt/tasks/webdriver-jasmine');
89
var npmTask = require('./grunt/tasks/npm');
910
var releaseTasks = require('./grunt/tasks/release');
1011

@@ -16,7 +17,8 @@ module.exports = function(grunt) {
1617
jsx: require('./grunt/config/jsx/jsx'),
1718
browserify: require('./grunt/config/browserify'),
1819
populist: require('./grunt/config/populist'),
19-
phantom: require('./grunt/config/phantom'),
20+
connect: require('./grunt/config/server')(grunt),
21+
"webdriver-jasmine": require('./grunt/config/webdriver-jasmine.js'),
2022
npm: require('./grunt/config/npm'),
2123
clean: ['./build', './*.gem', './docs/_site', './examples/shared/*.js'],
2224
jshint: require('./grunt/config/jshint'),
@@ -25,11 +27,12 @@ module.exports = function(grunt) {
2527

2628
grunt.config.set('compress', require('./grunt/config/compress'));
2729

28-
grunt.loadNpmTasks('grunt-contrib-jshint');
29-
grunt.loadNpmTasks('grunt-contrib-copy');
30-
grunt.loadNpmTasks('grunt-contrib-clean');
31-
grunt.loadNpmTasks('grunt-compare-size');
32-
grunt.loadNpmTasks('grunt-contrib-compress');
30+
Object.keys(grunt.file.readJSON('package.json').devDependencies)
31+
.filter(function(npmTaskName){ return npmTaskName.indexOf('grunt-') === 0;})
32+
.forEach(function(npmTaskName){
33+
grunt.loadNpmTasks(npmTaskName);
34+
})
35+
;
3336

3437
// Alias 'jshint' to 'lint' to better match the workflow we know
3538
grunt.registerTask('lint', ['jshint']);
@@ -42,7 +45,7 @@ module.exports = function(grunt) {
4245

4346
grunt.registerMultiTask('populist', populistTask);
4447

45-
grunt.registerMultiTask('phantom', phantomTask);
48+
grunt.registerMultiTask('webdriver-jasmine', webdriverJasmineTasks);
4649

4750
grunt.registerMultiTask('npm', npmTask);
4851

@@ -70,11 +73,17 @@ module.exports = function(grunt) {
7073
'jsx:jasmine',
7174
'jsx:test',
7275
'version-check',
73-
'populist:jasmine',
7476
'populist:test'
7577
]);
7678

77-
grunt.registerTask('test', ['build:test', 'build:basic', 'phantom:run']);
79+
grunt.registerTask('webdriver-phantomjs', webdriverPhantomJSTask);
80+
81+
grunt.registerTask('test:webdriver:phantomjs', [
82+
'connect',
83+
'webdriver-phantomjs',
84+
'webdriver-jasmine:local'
85+
]);
86+
grunt.registerTask('test', ['build:test', 'build:basic', 'test:webdriver:phantomjs']);
7887
grunt.registerTask('npm:test', ['build', 'npm:pack']);
7988

8089
// Optimized build task that does all of our builds. The subtasks will be run

grunt/config/phantom.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

grunt/config/server.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
var pxlgif = Buffer('R0lGODlhAQABAIAAAP///wAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==', 'base64');
2+
3+
module.exports = function(grunt){
4+
5+
function printMiddleware(req, res, next) {
6+
if (req._parsedUrl.pathname != '/print') return next();
7+
if (req.query.message.indexOf('ok') === 0){
8+
grunt.log.ok(req.query.message);
9+
} else if (req.query.message.indexOf('not ok') === 0){
10+
grunt.log.error(req.query.message);
11+
} else {
12+
grunt[req.query.type || 'log'].writeln('[%s][%s]', req.headers['user-agent'], Date.now(), req.query.message);
13+
}
14+
res.end(pxlgif);
15+
}
16+
function testResultLoggerMiddleware(req, res, next) {
17+
if (!(req.body && req.body.data)) return next();
18+
grunt.log.writeln('[%s][%s]', req.headers['user-agent'], Date.now(), req.body.data);
19+
res.end('Got it, thanks!');
20+
}
21+
22+
return {
23+
server: {
24+
options: {
25+
base: '.',
26+
hostname: '*',
27+
port: 9999,
28+
middleware: function(connect, options) {
29+
connect.logger.token('user-agent', function(req, res){ return req.headers['user-agent']; });
30+
connect.logger.token('timestamp', function(req, res){ return Date.now(); });
31+
32+
return [
33+
connect.query(),
34+
printMiddleware,
35+
36+
connect.logger({format:'[:user-agent][:timestamp] :method :url', stream:grunt.verbose}),
37+
connect.bodyParser(),
38+
testResultLoggerMiddleware,
39+
40+
connect.static(options.base),
41+
connect.directory(options.base)
42+
];
43+
},
44+
}
45+
}
46+
}
47+
}

grunt/config/webdriver-jasmine.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var grunt = require('grunt');
2+
3+
4+
exports.local = {
5+
webdriver: {
6+
remote: { protocol: 'http:', hostname: '127.0.0.1', port: 9515, path: '/' }
7+
},
8+
url: "http://127.0.0.1:9999/test/index.html",
9+
onComplete: function(report){
10+
var browser = this;
11+
if (!report.passed){
12+
grunt.fatal("tests failed");
13+
}
14+
},
15+
onError: function(error){
16+
grunt.fatal(error);
17+
}
18+
}

grunt/tasks/phantom.js

Lines changed: 0 additions & 68 deletions
This file was deleted.

grunt/tasks/populist.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
'use strict';
22

33
var grunt = require('grunt');
4+
var fs = require('fs')
45

56
module.exports = function() {
67
var config = this.data;
78
var done = this.async();
89

10+
var theFilesToTestScript = fs.createWriteStream(__dirname + '/../../test/the-files-to-test.generated.js');
11+
theFilesToTestScript.write('// Generated by ');
12+
theFilesToTestScript.write(JSON.stringify(__filename.split(/(?=grunt)/)[1]));
13+
theFilesToTestScript.write(' at ');
14+
theFilesToTestScript.write(JSON.stringify(new Date()));
15+
theFilesToTestScript.write('\n\n');
16+
917
// create the bundle we'll work with
1018
var args = config.args;
1119

@@ -15,14 +23,17 @@ module.exports = function() {
1523
nonull: true, // Keep IDs that don't expand to anything.
1624
cwd: config.rootDirectory
1725
}, requires).forEach(function(name) {
18-
args.push(name.replace(/\.js$/i, ""));
26+
name = name.replace(/\.js$/i, "");
27+
args.push(name);
28+
theFilesToTestScript.write('harness.enableTest(' + JSON.stringify(name) + ');\n');
1929
});
2030

2131
require("populist").buildP({
2232
rootDirectory: config.rootDirectory,
2333
args: args
2434
}).then(function(output) {
2535
grunt.file.write(config.outfile, output);
36+
theFilesToTestScript.end();
2637
done();
2738
});
2839
};

grunt/tasks/webdriver-jasmine.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
var grunt = require("grunt");
2+
var wd = require('wd');
3+
4+
module.exports = function(){
5+
var config = this.data;
6+
var taskSucceeded = this.async();
7+
grunt.verbose.write('webdriver remote', JSON.stringify(config.webdriver.remote));
8+
var browser = wd.promiseChainRemote(config.webdriver.remote);
9+
10+
browser.on('status', function(info) {
11+
grunt.verbose.writeln(info);
12+
});
13+
14+
browser.on('command', function(meth, path, data) {
15+
grunt.verbose.writeln(' > ' + meth, path, data || '');
16+
});
17+
18+
browser
19+
.init(config.browser || {})
20+
.get(config.url)
21+
.then(function(){return browser;})
22+
.then(getJSReport)
23+
.then(config.onComplete && config.onComplete.bind(browser), config.onError && config.onError.bind(browser))
24+
.fail(grunt.verbose.writeln.bind(grunt.verbose))
25+
.fin(function(){
26+
if (grunt.option('webdriver-keep-open')) return;
27+
grunt.verbose.writeln('Closing the browser window. To keep it open, pass the --webdriver-keep-open flag to grunt.');
28+
return browser.quit();
29+
})
30+
.done(
31+
taskSucceeded.bind(null,true),
32+
taskSucceeded.bind(null,false)
33+
)
34+
;
35+
}
36+
37+
function getJSReport(browser){
38+
return browser
39+
.waitForCondition("typeof window.jasmine != 'undefined'", 500)
40+
.waitForCondition("typeof window.jasmine.getJSReport != 'undefined'", 10e3)
41+
.waitForCondition("window.testImageURL.running <= 0", 5e3)
42+
.eval("jasmine.getJSReport()")
43+
;
44+
}

grunt/tasks/webdriver-phantomjs.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
var grunt = require('grunt');
2+
3+
module.exports = function(){
4+
var onReadyCallback = this.async();
5+
6+
var phantomjs = require("phantomjs").path;
7+
var child_process = require('child_process');
8+
var config = this.data || {};
9+
10+
var args = ["--webdriver=" + (config.port || 9515)];
11+
grunt.verbose.writeln('phantomjs START path:%s args:%s', phantomjs, args);
12+
13+
var child = child_process.spawn(phantomjs, args);
14+
process.on('exit', function() {
15+
child.kill();
16+
});
17+
18+
child.on('error', function(error) {
19+
grunt.verbose.writeln('phantomjs ERROR');
20+
grunt.fatal(error);
21+
});
22+
child.on('exit', function(code) {
23+
grunt.verbose.writeln('phantomjs END');
24+
if (code) grunt.fatal('phantomjs FAIL');
25+
});
26+
27+
function verboseWrite(chunk) {
28+
if (onReadyCallback && chunk.toString().indexOf('running on port') != -1) {
29+
grunt.verbose.writeln('phantomjs STARTED');
30+
onReadyCallback();
31+
onReadyCallback = null;
32+
}
33+
grunt.verbose.write(chunk);
34+
}
35+
child.stdout.on('data', verboseWrite);
36+
child.stderr.on('data', verboseWrite);
37+
}

0 commit comments

Comments
 (0)