examples.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. var webpage = require("webpage"),
  2. fs = require("fs");
  3. var html_path = fs.absolute("test.html");
  4. var examples = [];
  5. function run_example(example_index) {
  6. if (example_index >= examples.length) {
  7. phantom.exit(0);
  8. return;
  9. }
  10. var example = examples[example_index];
  11. var snapshot_index = 0;
  12. var page = webpage.create();
  13. page.viewportSize = { width: 500, height: 300 };
  14. page.clipRect = { width: 500, height: 300 };
  15. page.onAlert = function (msg) {
  16. var e = JSON.parse(msg);
  17. if (e.fn == "snapshot") {
  18. page.render("output/" + example.name + snapshot_index + ".png");
  19. snapshot_index += 1;
  20. } else if (e.fn == "mousemove") {
  21. page.sendEvent("mousemove", e.x, e.y);
  22. }
  23. };
  24. page.open(html_path, function (status) {
  25. if (status == "fail") {
  26. console.log("Failed to load test page: " + example.name);
  27. phantom.exit(1);
  28. } else {
  29. page.evaluate(example.runner);
  30. }
  31. page.close();
  32. run_example(example_index + 1);
  33. });
  34. }
  35. exports.def = function (name, runner) {
  36. examples.push({ name: name, runner: runner });
  37. };
  38. exports.run = function () {
  39. if (fs.isDirectory("output")) {
  40. fs.list("output").forEach(function (path) {
  41. if (path != "." && path != "..") {
  42. fs.remove("output/" + path);
  43. }
  44. });
  45. } else {
  46. fs.makeDirectory("output");
  47. }
  48. run_example(0);
  49. };