runtests.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*global phantom:true console:true console_reporter:true */
  2. // Runs a Jasmine Suite from an html page
  3. // @page is a PhantomJs page object
  4. // @exit_func is the function to call in order to exit the script
  5. (function(phantom, WebPage){
  6. "use strict";
  7. var PhantomJasmineRunner = function(page, exit_func){
  8. this.page = page;
  9. this.exit_func = exit_func || phantom.exit;
  10. this.tries = 0;
  11. this.max_tries = 10;
  12. this.get_status = function(){
  13. return this.page.evaluate(function(){
  14. return console_reporter.status;
  15. });
  16. };
  17. this.terminate = function(){
  18. var status = this.get_status();
  19. if (status === 'success') {
  20. this.exit_func(0);
  21. } else if (status === 'fail') {
  22. this.exit_func(1);
  23. } else {
  24. this.exit_func(2);
  25. }
  26. };
  27. };
  28. // Script Begin
  29. if (phantom.args.length === 0) {
  30. console.log("Need a url as the argument");
  31. phantom.exit(1);
  32. }
  33. var page = new WebPage();
  34. var runner = new PhantomJasmineRunner(page);
  35. var address = phantom.args[0];
  36. // Don't supress console output
  37. page.onConsoleMessage = function(msg){
  38. console.log(msg);
  39. // Terminate when the reporter singals that testing is over.
  40. // We cannot use a callback function for this (because page.evaluate is sandboxed),
  41. // so we have to *observe* the website.
  42. if (msg == "ConsoleReporter finished") {
  43. runner.terminate();
  44. }
  45. };
  46. page.open(address, function(status){
  47. if (status != "success") {
  48. console.log("can't load the address!");
  49. phantom.exit(1);
  50. }
  51. // Now we wait until onConsoleMessage reads the termination signal from the log.
  52. });
  53. }(phantom, WebPage));