1
0

phantom.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Simple phantom.js integration script
  2. // Adapted from Modernizr
  3. function waitFor(testFx, onReady, timeOutMillis) {
  4. var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 5001 //< Default Max Timout is 5s
  5. , start = new Date().getTime()
  6. , condition = false
  7. , interval = setInterval(function () {
  8. if ((new Date().getTime() - start < maxtimeOutMillis) && !condition) {
  9. // If not time-out yet and condition not yet fulfilled
  10. condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()) //< defensive code
  11. } else {
  12. if (!condition) {
  13. // If condition still not fulfilled (timeout but condition is 'false')
  14. console.log("'waitFor()' timeout")
  15. phantom.exit(1)
  16. } else {
  17. // Condition fulfilled (timeout and/or condition is 'true')
  18. typeof(onReady) === "string" ? eval(onReady) : onReady() //< Do what it's supposed to do once the condition is fulfilled
  19. clearInterval(interval) //< Stop this interval
  20. }
  21. }
  22. }, 100) //< repeat check every 100ms
  23. }
  24. if (phantom.args.length === 0 || phantom.args.length > 2) {
  25. console.log('Usage: phantom.js URL')
  26. phantom.exit()
  27. }
  28. var page = new WebPage()
  29. // Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this")
  30. page.onConsoleMessage = function(msg) {
  31. console.log(msg)
  32. };
  33. page.open(phantom.args[0], function(status){
  34. if (status !== "success") {
  35. console.log("Unable to access network")
  36. phantom.exit()
  37. } else {
  38. waitFor(function(){
  39. return page.evaluate(function(){
  40. var el = document.getElementById('qunit-testresult')
  41. if (el && el.innerText.match('completed')) {
  42. return true
  43. }
  44. return false
  45. })
  46. }, function(){
  47. var failedNum = page.evaluate(function(){
  48. var el = document.getElementById('qunit-testresult')
  49. try {
  50. return el.getElementsByClassName('failed')[0].innerHTML
  51. } catch (e) { }
  52. return 10000
  53. });
  54. phantom.exit((parseInt(failedNum, 10) > 0) ? 1 : 0)
  55. })
  56. }
  57. })