Permalink
Browse files

`rake test` runs tests with PhantomJS

  • Loading branch information...
1 parent 3f6172d commit 5f221a2054762e402a3465b5e30a3eae4aeaf432 @mislav mislav committed Apr 9, 2012
Showing with 129 additions and 5 deletions.
  1. +25 −0 Guardfile
  2. +17 −2 README.md
  3. +5 −0 Rakefile
  4. +6 −0 script/guard
  5. +2 −0 script/test
  6. +5 −3 test/evidence_runner.js
  7. +69 −0 test/runner.coffee
View
@@ -0,0 +1,25 @@
+require 'guard/guard'
+
+class ::Guard::PhantomJS < ::Guard::Guard
+ def run_all
+ run_on_change([])
+ end
+
+ def run_on_change(paths)
+ passed = system @options[:runner], *paths
+
+ ::Guard::Notifier.notify \
+ (passed ? 'passed' : 'failed'),
+ :title => 'Zepto test results',
+ :image => (passed ? :success : :failed)
+ end
+end
+
+guard :phantomjs, :runner => 'script/test' do
+ watch(%r{src/(.+)\.js$}) do |m|
+ "test/#{m[1]}.html"
+ end
+ watch(%r{test/(.+)\.html$}) do |m|
+ m[0] unless m[1].include? '_functional'
+ end
+end
View
@@ -31,7 +31,6 @@ $ gem install uglifier
Build Zepto by running `rake`:
~~~ sh
-# build zepto.js
$ rake
Original version: 40.423k
Minified: 18.776k
@@ -174,7 +173,22 @@ browser to run the tests. Files named with "\_functional" are not automated
tests, but require interaction. Automated tests are written using
[Evidence.js][].
-Detailed test output is logged in the JavaScript console of your browser.
+Detailed test information such as specific failures is logged to the JavaScript
+console of your browser.
+
+It's possible to run automated tests from the command-line in headless Webkit
+with [PhantomJS][]:
+
+~~~ sh
+# install on Mac OS
+$ brew install phantomjs
+
+# run all tests
+$ rake test
+
+# run individual files
+$ script/test test/selector.html
+~~~
### Code style guidelines
@@ -194,3 +208,4 @@ Detailed test output is logged in the JavaScript console of your browser.
[mkd]: http://github.github.com/github-flavored-markdown/
[evidence.js]: https://github.com/tobie/Evidence
[optional]: http://mislav.uniqpath.com/2010/05/semicolons/
+ [phantomjs]: http://code.google.com/p/phantomjs/wiki/Installation
View
@@ -83,6 +83,11 @@ end
task(:clean) { rm_rf 'dist' }
+desc "Run tests with PhantomJS"
+task :test do
+ sh 'script/test'
+end
+
desc "Strip trailing whitespace and ensure each file ends with a newline"
task :whitespace do
verbose false do
View
@@ -0,0 +1,6 @@
+#!/bin/sh
+if [ -z "$BUNDLE_GEMFILE" ]; then
+ export BUNDLE_GEMFILE=shutup
+fi
+
+exec guard --no-notify "$@"
View
@@ -0,0 +1,2 @@
+#!/bin/sh
+phantomjs test/runner.coffee "$@"
@@ -69,13 +69,15 @@
var container = $('results')
if (container) {
if (results.failureCount || results.errorCount) {
- container.innerHTML = printf("Finished in %d s. &ndash; <em>%d failures, %d errors</em> (%d assertions)",
- [seconds, results.failureCount, results.errorCount, results.assertionCount])
container.className = 'failed'
+ container.innerHTML = printf("Finished in %d s. &ndash; <em>%d failures, %d errors</em> (%d assertions)",
+ [seconds, results.failureCount, results.errorCount, results.assertionCount])
} else {
- container.innerHTML = printf("Finished in %d s. &ndash; <em>%d tests passed</em> (%d assertions)", [seconds, results.testCount, results.assertionCount])
container.className = 'passed'
+ container.innerHTML = printf("Finished in %d s. &ndash; <em>%d tests passed</em> (%d assertions)",
+ [seconds, results.testCount, results.assertionCount])
}
+ container.className += ' finished'
}
}
View
@@ -0,0 +1,69 @@
+# Test runner for PhantomJS <phantomjs.org>
+# Usage:
+# $ phantomjs test/runner.coffee [<page1>, <page2>, ...]
+#
+# When no test pages specified, runs all automated tests.
+
+system = require('system')
+fs = require('fs')
+
+if system.args.length > 1
+ # list of test pages to run
+ suites = system.args.slice(1)
+else
+ # by default, run all test/*.html pages
+ modules = 'zepto ajax data detect event form fx selector stack'.split /\s+/
+ suites = modules.map (name)-> "test/#{name}.html"
+
+page = require('webpage').create()
+
+page.onConsoleMessage = (msg) ->
+ console.log msg
+
+page.onError = (msg, trace) ->
+ console.log 'ERROR: ' + msg
+
+# used for waiting until the tests finish running
+waitFor = (testFn, onReady, timeout=3000) ->
+ start = new Date()
+ interval = setInterval ->
+ if testFn()
+ clearInterval interval
+ onReady()
+ else if new Date() - start > timeout
+ console.log "timed out."
+ phantom.exit(1)
+ , 100
+
+loadNextSuite = ->
+ if not suites.length
+ phantom.exit()
+ else
+ url = suites.shift() + "?verbosity=WARN"
+ # PhantomJS chokes on the query string on relative paths
+ url = "file://#{fs.workingDirectory}/#{url}" if not /:\/\//.test url
+
+ page.open url, (status) ->
+ if status isnt "success"
+ console.log "failed opening #{url}"
+ phantom.exit(1)
+
+ waitFor ->
+ page.evaluate ->
+ # the "#results" element needs to have the "finished" class
+ res = document.getElementById 'results'
+ /finished/.test res.className if res
+ , ->
+ passed = page.evaluate ->
+ res = document.getElementById 'results'
+ paths = location.pathname.split('/')
+ # echo test results to the console
+ console.log "#{paths[paths.length - 1]} - " + res.textContent
+ /passed/.test res.className
+
+ if passed
+ loadNextSuite()
+ else
+ phantom.exit(1)
+
+loadNextSuite()

0 comments on commit 5f221a2

Please sign in to comment.