-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscenario.js
136 lines (113 loc) · 3.2 KB
/
scenario.js
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
define(['scenario/deferred', 'scenario/eventAggregator'], function (deferred, events) {
var scenario;
function StepFailed(error) {
this.message = error.message || error;
this.stack = error.stack;
if (error.constructor) {
this.type = error.constructor.name;
}
}
function Step(title, action) {
this.title = title;
this.action = action;
}
Step.prototype.run = function (previousResult) {
var that = this, currentResult, test = deferred();
try {
currentResult = this.action(previousResult);
} catch (error) {
return this.fail(test, error);
}
if (currentResult && currentResult.then) {
// currentResult is a promise
currentResult.fail(function (error) {
return that.fail(test, error);
}).then(function (asyncResult) {
asyncResult = asyncResult || previousResult;
test.resolve(asyncResult);
});
return test;
}
return test.resolve(currentResult || previousResult);
};
Step.prototype.fail = function (test, error) {
this.error = new StepFailed(error);
events.publish('scenario.step.fail', this.toJSON());
return test.reject(this.error);
};
Step.prototype.toJSON = function () {
var json = {
title: this.title
};
if (this.error) {
json.error = {
message: this.error.message,
stack: this.error.stack,
type: this.error.type
};
}
return json;
};
function Scenario(title, setupSteps, metadata) {
this.id = window.location.hash.substring(1);
this.metadata = metadata || {};
this.title = title;
this.steps = [];
this.testRun = deferred();
this.setupSteps = setupSteps;
}
Scenario.prototype.addStep = function (step) {
var that = this;
this.steps.push(step);
this.testPipeline = this.testPipeline.always(function () {
events.publish('scenario.step.start', step.toJSON());
}).fail(function (error) {
step.error = {
message: 'Step skipped due to previous failure'
};
events.publish('scenario.step.fail', step.toJSON());
}).then(function (result) {
return step.run(result);
}).always(function () {
events.publish('scenario.step.end', step.toJSON());
});
};
Scenario.prototype.run = function () {
var that = this;
this.testPipeline = this.testRun.then(function () {
events.publish('scenario.scenario.start', that.toJSON());
});
this.setupSteps();
this.testPipeline.always(function (result) {
if (that.testPipeline.state() === 'rejected') {
that.error = result;
}
events.publish('scenario.scenario.end', that.toJSON());
});
this.testRun.resolve();
};
Scenario.prototype.toJSON = function () {
var i, stepJSON = [];
for (i = 0; i < this.steps.length; i++) {
stepJSON.push(this.steps[i].toJSON());
};
var json = {
id: this.id,
metadata: this.metadata,
title: this.title,
steps: stepJSON
};
if (this.error) {
json.error = this.error;
}
return json;
};
String.prototype._ = function (stepAction) {
var step = new Step(this.toString(), stepAction);
scenario.addStep(step);
};
return function (title, setupSteps, metadata) {
scenario = new Scenario(title, setupSteps, metadata);
scenario.run();
}
});