Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. var fs = require('fs');
  2.  
  3. var PhantomScenario = function(_page, scenario, options) {
  4. this.scenario = scenario;
  5. this.page = _page;
  6. this.options = options;
  7. this.testcase = {};
  8. this.init();
  9. };
  10.  
  11. PhantomScenario.prototype = {
  12. init: function() {
  13. var self = this;
  14. self.page.onConsoleMessage = function(msg, lineNum, sourceId) {
  15. console.log(sourceId + ":" + msg);
  16. };
  17. self.page.onInitialized = function() {
  18. self.page.evaluate(function() {
  19. document.addEventListener('DOMContentLoaded', function() {
  20. window.callPhantom('DOMContentLoaded');
  21. }, false);
  22. });
  23. };
  24. self.page.onCallback = function(event) {
  25. if (event === 'DOMContentLoaded') {
  26. console.log(self.page.no + ": " + self.page.url);
  27. var strDate = new Date().getTime();
  28. fs.write(self.options.evidencedir + fs.separator + strDate + ".html", self.page.content, 'a');
  29. console.log("render start.");
  30. self.page.render(self.options.evidencedir + fs.separator + strDate + '_capture_' + self.page.no + '.png');
  31. console.log("render finish.");
  32. self.assert();
  33. self.page.no++;
  34. self.next();
  35. }
  36. };
  37. self.page.onPageCreated = function(newPage) {
  38. self.page.onCallback = function(){};
  39. newPage.no = page.no;
  40. var newScenario = self.scenario;
  41. self.scenario = undefined;
  42. var newPhantomScenario = new PhantomScenario(newPage, newScenario, self.options);
  43. newPhantomScenario.testcase = self.testcase;
  44. self.testcase = undefined;
  45. };
  46. },
  47. next: function() {
  48. this.exec();
  49. },
  50. exec: function() {
  51. this.testcase = this.scenario.shift();
  52. if (this.testcase !== undefined) {
  53. if (this.testcase.name !== undefined) {
  54. console.log(this.testcase.name);
  55. }
  56. if (this.testcase.evaluate !== undefined) {
  57. this.page.evaluate(this.testcase.evaluate);
  58. } else {
  59. this.testcase.operation(this.page);
  60. }
  61. } else {
  62. this.page.onCallback = function(){};
  63. console.log("finish.");
  64. phantom.exit();
  65. }
  66. },
  67. assert: function() {
  68. if (this.testcase.assert !== undefined) {
  69. if (!this.testcase.assert(this.page)) {
  70. console.log('assert error. at testcase ' + this.page.no);
  71. phantom.exit(1);
  72. }
  73. }
  74. },
  75. wait: function(waitFor) {
  76. console.log("wait");
  77. var start = new Date().getTime();
  78. var current = new Date().getTime();
  79. while (current < start + waitFor) {
  80. current = new Date().getTime();
  81. }
  82. console.log("wait end");
  83. return;
  84. }
  85. };
  86.  
  87.  
  88. exports.run = function(page, scenario, options) {
  89. new PhantomScenario(page, scenario, options).next();
  90. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement