runner.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. (function() {
  2. var fs = require('fs')
  3. var system = require('system')
  4. var args = system.args.slice(1)
  5. var prefix = args.shift() || ("file://" + fs.workingDirectory + "/")
  6. var suites
  7. if (args.length > 0) {
  8. suites = args
  9. } else {
  10. var modules = 'zepto ajax callbacks data deferred ajax_deferred detect touch event form fx selector stack'.split(/\s+/)
  11. suites = modules.map(function(name) {
  12. return "test/" + name + ".html"
  13. })
  14. }
  15. var page = require('webpage').create()
  16. page.onConsoleMessage = function(msg) {
  17. console.log(msg)
  18. }
  19. page.onError = function(msg, trace) {
  20. console.log('ERROR: ' + msg)
  21. }
  22. function waitFor(testFn, onReady, timeout) {
  23. if (timeout == null) {
  24. timeout = 30000
  25. }
  26. var start = new Date()
  27. var interval = setInterval(function() {
  28. if (testFn()) {
  29. clearInterval(interval)
  30. onReady()
  31. } else if (new Date() - start > timeout) {
  32. console.log("timed out.")
  33. phantom.exit(1)
  34. }
  35. }, 100)
  36. }
  37. function loadNextSuite() {
  38. if (!suites.length) {
  39. phantom.exit()
  40. } else {
  41. var url = suites.shift() + "?verbosity=WARN"
  42. if (!/:\/\//.test(url)) {
  43. url = prefix + url
  44. }
  45. page.open(url, function(status) {
  46. if (status !== "success") {
  47. console.log("failed opening " + url)
  48. return phantom.exit(1)
  49. }
  50. waitFor(function() {
  51. return page.evaluate(function() {
  52. var res = document.getElementById('results')
  53. if (res) {
  54. return /finished/.test(res.className)
  55. }
  56. })
  57. }, function() {
  58. var passed = page.evaluate(function() {
  59. var res = document.getElementById('results')
  60. var paths = location.pathname.split('/')
  61. console.log((paths[paths.length - 1] + " - ") + res.textContent)
  62. return /passed/.test(res.className)
  63. })
  64. if (passed) {
  65. loadNextSuite()
  66. } else {
  67. phantom.exit(1)
  68. }
  69. })
  70. })
  71. }
  72. }
  73. loadNextSuite()
  74. }).call(this)