Skip to content

Commit

Permalink
phantomjs for headless testing
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangruber committed Mar 4, 2013
1 parent 16aa092 commit 78c07bf
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 18 deletions.
28 changes: 25 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ Meanwhile have your tap output in the console and a comfy watch mode.
Usage: tapedeck [FILE/GLOB]... [OPTIONS]

Options:
-w, --watch watch mode
-h, --html html reporter

-w, --watch watch mode
-h, --html html reporter
-p, --phantom, --phantomjs test headlessly with phantomjs
```

### normal mode
Expand Down Expand Up @@ -79,6 +79,28 @@ ok 3 el changed
# ok
```

### phantomjs

BRAND NEW: use phantomjs to run you tests headlessly. This way you don't need
to open a browser, phantomjs does all that for you.

```bash
$ tapedeck test/*.js -p
TAP version 13
# editable
ok 1 input changed
ok 2 stream received data
ok 3 el changed

1..3
# tests 3
# pass 3

# ok
```

Combine with `watch` for super powers!

### html reporter

Shows the tests output directly in your browser. Use with caution, this may
Expand Down
41 changes: 31 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,26 @@
var browserify = require('browserify')
var fs = require('fs')
var html = fs.readFileSync(__dirname + '/lib/index.html')
var spawn = require('child_process').spawn
var phantomjs = require('phantomjs')

// configuration
var port = Math.round(Math.random() * 65535)
var dir = '/tmp/' + Math.random().toString(16).slice(2)

var argv = require('optimist')
.usage('Usage: $0 [FILE/GLOB]... [OPTIONS]')
.alias('w', 'watch')
.alias('h', 'html')

.describe('w', 'watch mode')
.alias('w', 'watch')

.describe('h', 'html reporter')
.alias('h', 'html')

.describe('p', 'test headlessly with phantomjs')
.alias('p', 'phantom')
.alias('p', 'phantomjs')

.demand('_')
.argv

Expand All @@ -27,7 +36,7 @@ fs.writeFileSync(dir + '/index.html', html)
fs.writeFileSync(dir + '/tapedeck.js', client)

// html reporter
if (argv.h) {
if (argv.h && !argv.phantomjs) {
var reporter = browserify().addEntry(__dirname + '/lib/reporter.js').bundle()
fs.writeFileSync(dir + '/reporter.js', reporter)
}
Expand Down Expand Up @@ -70,7 +79,15 @@ var http = require('http')
var ecstatic = require('ecstatic')
var server = http.createServer(ecstatic(dir))
server.listen(port, function () {
console.log('Open up http://localhost:' + port + '/ in your browser\n')
var addr = 'http://localhost:' + port + '/';
if (argv.phantomjs) {
var ps = spawn(phantomjs.path, [
__dirname + '/script/phantom.js', addr
])
ps.stderr.pipe(process.stderr)
} else {
console.log('Open up ' + addr + ' in your browser\n')
}
})

// socket
Expand All @@ -79,11 +96,13 @@ shoe(function (stream) {
stream.pipe(process.stdout)
if (!argv.w) {
stream.on('data', function (data) {
if (data.match('# ok')) process.exit()
if (data.match('# fail')) {
failed = true
process.exit()
}
var isOk = data.match('# ok')
var isFail = data.match('# fail')

if (!isFail && !isOk) return
if (isFail) failed = true
stream.write('quit')
process.exit()
})
return
}
Expand All @@ -107,7 +126,9 @@ function cleanup () {
cleanedUp = true
fs.unlinkSync(dir + '/index.html')
fs.unlinkSync(dir + '/tapedeck.js')
if (argv.html) fs.unlinkSync(dir + '/reporter.js')
if (argv.html && !argv.phantomjs) {
fs.unlinkSync(dir + '/reporter.js')
}
fs.unlinkSync(dir + '/tests.js')
fs.rmdirSync(dir)
process.exit(failed)
Expand Down
2 changes: 2 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ console.log = function (data) {
sock.on('data', function (data) {
if (data.match('reload')) {
window.location.href = window.location.href
} else if (data.match('quit')) {
window.finished = true
}
})
3 changes: 1 addition & 2 deletions lib/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ emitter.on('data', function (data) {
pre.innerHTML += data
})

document.addEventListener('load', function () {
window.addEventListener('load', function () {
document.body.appendChild(pre)
})

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "tapedeck",
"version": "0.0.8",
"version": "0.1.0",
"description": "Run tap(e) tests that need a browser...in your browser!",
"main": "index.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "node index.js test/*.js"
"test": "node index.js test/*.js -p"
},
"bin": {
"tapedeck": "./index.js"
Expand All @@ -31,7 +31,8 @@
"ecstatic": "~0.3.0",
"browserify": "~1.16.6",
"shoe": "0.0.7",
"glob": "~3.1.14"
"glob": "~3.1.14",
"phantomjs": "~1.8.1-3"
},
"devDependencies": {
"tape": "~0.1.5"
Expand Down
14 changes: 14 additions & 0 deletions script/phantom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var page = require('webpage').create();
var args = require('system').args;

page.open(args[1], function () {
function finished () {
return page.evaluate(function () {
return !! window.finished;
});
}

setInterval(function () {
if (finished()) phantom.exit();
}, 50);
});

0 comments on commit 78c07bf

Please sign in to comment.