Skip to content

Commit

Permalink
Run browser tests against PhantomJS
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Scholtes committed Oct 11, 2013
1 parent 6b3daa1 commit 950e122
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 14 deletions.
6 changes: 4 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
var connect = require('connect');


module.exports = function(grunt) {
grunt.registerTask('testserver', 'Start a server to test clients', function(){

grunt.loadTasks('tasks')

grunt.registerTask('test:server', 'Start a server to test clients', function(){
var done = this.async();
server = require('./test/helpers/server')();
server.listen(3000)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"mocha": "*",
"chai": "*",
"sinon": "1.7.3",
"phantom-proxy": "~0.1.792",
"uglify-js": "~2"
},
"engine": "node >= 0.10",
Expand Down
45 changes: 45 additions & 0 deletions tasks/test_phantom.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
path = require('path')

module.exports = (grunt)->

grunt.registerTask 'test:phantom', 'Run browser tests in phantom', ->

done = this.async()

phantomProxy = require('phantom-proxy')
url = 'http://127.0.0.1:3000'
phantomProxy.create (proxy)->

fail = (msg)->
proxy.end ->
grunt.fail.fatal(msg)
done()

page = proxy.page
page.open url, (status)->
if status != true
fail "Could not connect to #{url}\nStart test server with `grunt test:server`"

page.on 'error', (error, trace)->
if trace && trace.length
grunt.log.error(error.red + ' at ')
trace.forEach (line)->
file = line.file.replace(/^file:/,'')
message = grunt.util._('%s:%d %s')
.sprintf(path.relative('.',file), line.line, line.function)
grunt.log.error(message.red)
else
grunt.log.error(error.red)
fail('Errors on site')

page.on 'callback', (args)->
page.emit(args...)

page.on 'console', console.log.bind(console.log)
page.on 'write', process.stdout.write.bind(process.stdout)
page.on 'finished', (failures)->
if failures > 0
fail("#{failures} test(s) failed")
else
proxy.end(done)

9 changes: 7 additions & 2 deletions test/browser/index.coffee
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
require './connection.coffee'
require './doc.coffee'
mocha.setup('bdd')
phantom = require '../helpers/phantom'
require './connection'
require './doc'

mocha.run().on 'end', ->
phantom('finished', this.failures)
14 changes: 4 additions & 10 deletions test/browser/index.html
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<title>ShareJS Client Tests</title>

<script src="/mocha.js"></script>
<script>
mocha.setup('bdd')
</script>
<script src="/tests.js"></script>
<link rel="stylesheet" href="/mocha.css">

</head>

<body>
<div id="mocha"></div>
<script>
mocha.run()
</script>
<script src="/mocha.js"></script>
<script src="/tests.js"></script>
</body>
</html>
25 changes: 25 additions & 0 deletions test/helpers/phantom.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Communicate with phantom if available
if window && window.callPhantom

Function.prototype.bind = (object)->
=> this.apply(object, arguments)


phantom = (type, args...)->
if args.length > 0
window.callPhantom [type].concat(args)
else
(args...)-> window.callPhantom [type].concat(args)

console.log = phantom('console')
console.error = phantom('console')
Mocha.process.stdout.write = phantom('write')

mocha.reporter('spec')

module.exports = phantom
module.exports.available = true

else
module.exports = ->
module.exports.available = false

0 comments on commit 950e122

Please sign in to comment.