| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
 | var scenario = angular.scenario;
scenario.SuiteRunner = function(scenarios, body) {
  this.scenarios = scenarios;
  this.body = body;
};
scenario.SuiteRunner.prototype = {
  run:function(){
    this.setUpUI();
    this.runScenarios();
  },
  setUpUI:function(){
    this.body.html(
      '<div id="runner">' +
        '<div class="console"></div>' +
      '</div>' +
      '<div id="testView">' +
        '<iframe></iframe>' +
      '</div>');
    this.console = this.body.find(".console");
    this.testFrame = this.body.find("iframe");
    this.console.find(".run").live("click", function(){
      jQuery(this).parent().find('.log').toggle();
    });
  },
  runScenarios:function(){
    var runner = new scenario.Runner(this.console, this.testFrame);
    _.stepper(this.scenarios, function(next, scenarioObj, name){
        new scenario.Scenario(name, scenarioObj).run(runner, next);
      }, function(){
      }
    );
  }
};
scenario.Runner = function(console, frame){
  this.console = console;
  this.current = null;
  this.tests = [];
  this.frame = frame;
};
scenario.Runner.prototype = {
  start:function(name){
    var current = this.current = {
      name:name,
      start:new Date().getTime(),
      scenario:jQuery('<div class="scenario"></div>')
    };
    current.run = current.scenario.append(
      '<div class="run">' +
        '<span class="name">.</span>' +
        '<span class="time">.</span>' +
        '<span class="state">.</span>' +
      '</run>').find(".run");
    current.log = current.scenario.append('<div class="log"></div>').find(".log");
    current.run.find(".name").text(name);
    this.tests.push(current);
    this.console.append(current.scenario);
  },
  end:function(name){
    var current = this.current;
    var run = current.run;
    this.current = null;
    current.end = new Date().getTime();
    current.time = current.end - current.start;
    run.find(".time").text(current.time);
    run.find(".state").text(current.error ? "FAIL" : "PASS");
    run.addClass(current.error ? "fail" : "pass");
    if (current.error)
      run.find(".run").append('<span div="error"></span>').text(current.error);
    current.scenario.find(".log").hide();
  },
  log:function(level) {
    var buf = [];
    for ( var i = 1; i < arguments.length; i++) {
      var arg = arguments[i];
      buf.push(typeof arg == "string" ?arg:toJson(arg));
    }
    var log = jQuery('<div class="' + level + '"></div>');
    log.text(buf.join(" "));
    this.current.log.append(log);
    this.console.scrollTop(this.console[0].scrollHeight);
    if (level == "error")
      this.current.error = buf.join(" ");
  }
};
scenario.Scenario = function(name, scenario){
  this.name = name;
  this.scenario = scenario;
};
scenario.Scenario.prototype = {
  run:function(runner, callback) {
    var self = this;
    _.stepper(this.scenario, function(next, steps, name){
      if (name.charAt(0) == '$') {
        next();
      } else {
        runner.start(self.name + "::" + name);
        var allSteps = (self.scenario.$before||[]).concat(steps);
        _.stepper(allSteps, function(next, step){
          self.executeStep(runner, step, next);
        }, function(){
          runner.end();
          next();
        });
      }
    }, callback);
  },
  verb:function(step){
    var fn = null;
    if (!step) fn = function (){ throw "Step is null!"; };
    else if (step.Given) fn = scenario.GIVEN[step.Given];
    else if (step.When) fn = scenario.WHEN[step.When];
    else if (step.Then) fn = scenario.THEN[step.Then];
      return fn || function (){
         throw "ERROR: Need Given/When/Then got: " + toJson(step);
       };
  },
  context: function(runner) {
    var frame = runner.frame;
    var window = frame[0].contentWindow;
    var document;
    if (window.jQuery)
      document = window.jQuery(window.document);
    var context = {
        frame:frame,
        window:window,
        log:_.bind(runner.log, runner, "info"),
        document:document,
        assert:function(element, path){
          if (element.size() != 1) {
            throw "Expected to find '1' found '"+
              element.size()+"' for '"+path+"'.";
          }
          return element;
        },
        element:function(path){
          var exp = path.replace("{{","[ng-bind=").replace("}}", "]");
          var element = document.find(exp);
          return context.assert(element, path);
        }
    };
    return context;
  },
  executeStep:function(runner, step, callback) {
    if (!step) {
      callback();
      return;
    }
    runner.log("info", toJson(step));
    var fn = this.verb(step);
    var context = this.context(runner);
    _.extend(context, step);
    try {
      (fn.call(context)||function(c){c();})(callback);
    } catch (e) {
      runner.log("error", "ERROR: " + toJson(e));
    }
  }
};
 |