Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var fs = require('fs');
- var PhantomScenario = function(_page, scenario, options) {
- this.scenario = scenario;
- this.page = _page;
- this.options = options;
- this.testcase = {};
- this.init();
- };
- PhantomScenario.prototype = {
- init: function() {
- var self = this;
- self.page.onConsoleMessage = function(msg, lineNum, sourceId) {
- console.log(sourceId + ":" + msg);
- };
- self.page.onInitialized = function() {
- self.page.evaluate(function() {
- document.addEventListener('DOMContentLoaded', function() {
- window.callPhantom('DOMContentLoaded');
- }, false);
- });
- };
- self.page.onCallback = function(event) {
- if (event === 'DOMContentLoaded') {
- console.log(self.page.no + ": " + self.page.url);
- var strDate = new Date().getTime();
- fs.write(self.options.evidencedir + fs.separator + strDate + ".html", self.page.content, 'a');
- console.log("render start.");
- self.page.render(self.options.evidencedir + fs.separator + strDate + '_capture_' + self.page.no + '.png');
- console.log("render finish.");
- self.assert();
- self.page.no++;
- self.next();
- }
- };
- self.page.onPageCreated = function(newPage) {
- self.page.onCallback = function(){};
- newPage.no = page.no;
- var newScenario = self.scenario;
- self.scenario = undefined;
- var newPhantomScenario = new PhantomScenario(newPage, newScenario, self.options);
- newPhantomScenario.testcase = self.testcase;
- self.testcase = undefined;
- };
- },
- next: function() {
- this.exec();
- },
- exec: function() {
- this.testcase = this.scenario.shift();
- if (this.testcase !== undefined) {
- if (this.testcase.name !== undefined) {
- console.log(this.testcase.name);
- }
- if (this.testcase.evaluate !== undefined) {
- this.page.evaluate(this.testcase.evaluate);
- } else {
- this.testcase.operation(this.page);
- }
- } else {
- this.page.onCallback = function(){};
- console.log("finish.");
- phantom.exit();
- }
- },
- assert: function() {
- if (this.testcase.assert !== undefined) {
- if (!this.testcase.assert(this.page)) {
- console.log('assert error. at testcase ' + this.page.no);
- phantom.exit(1);
- }
- }
- },
- wait: function(waitFor) {
- console.log("wait");
- var start = new Date().getTime();
- var current = new Date().getTime();
- while (current < start + waitFor) {
- current = new Date().getTime();
- }
- console.log("wait end");
- return;
- }
- };
- exports.run = function(page, scenario, options) {
- new PhantomScenario(page, scenario, options).next();
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement