forked from 73rhodes/sideflow
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sideflow.js
212 lines (203 loc) · 7.64 KB
/
sideflow.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
var SideFlowUtils = classCreate();
objectExtend(SideFlowUtils.prototype, {
testCase: null,
banner: "SideFlow",
initialize: function() { },
setTestCase: function(testCase) {
this.testCase = testCase;
},
getTestCase: function() {
return this.testCase;
},
getCommands: function() {
var seleniumCommands;
if(this.isIDE()) {
seleniumCommands = this.getTestCase().commands;
} else {
seleniumCommands = this.getTestCase().commandRows;
}
return seleniumCommands;
},
isIDE: function() {
return (Application.prototype != undefined);
},
formatMessage: function(msg, error) {
return this.banner + ((error) ? "[" + error.lineNumber + "] " : " ") + msg;
},
debug: function(msg, error) {
LOG.debug(this.formatMessage(msg, error));
},
info: function(msg, error) {
LOG.info(this.formatMessage(msg, error));
},
warn: function(msg, error) {
LOG.warn(this.formatMessage(msg, error));
},
error: function(msg, error) {
LOG.error(this.formatMessage(msg, error));
},
throwError: function(err, error) {
throw new SeleniumError(this.formatMessage(err, error));
}
});
var SideFlow = classCreate();
objectExtend(SideFlow.prototype, {
gotoLabels: {},
whileLabels: { ends: {}, whiles: {} },
utils: null,
/*
* --- Initialize Conditional Elements --- *
* Run through the script collecting line numbers of all conditional elements
* There are three a results arrays: goto labels, while pairs and forEach pairs
*/
initialize: function(sideFlowUtils) {
this.utils = sideFlowUtils;
this.utils.debug("detected runtime = Selenium " + (this.utils.isIDE() ? "IDE" : "RC"), new Error());
if(this.utils.isIDE()) {
this.utils.setTestCase(testCase);
} else {
this.utils.setTestCase(testFrame.getCurrentTestCase());
}
try {
var commands = this.utils.getCommands(),
command_rows = [],
cycles = [],
forEachCmds = [];
for(var i = 0; i < commands.length; ++i) {
command_rows.push(commands[i]);
}
for(var i = 0; i < command_rows.length; i++) {
if (this.utils.isIDE()) {
if((command_rows[i].type == 'command')) {
switch (command_rows[i].command.toLowerCase()) {
case "label":
this.gotoLabels[ command_rows[i].target ] = i;
break;
case "while":
case "endwhile":
cycles.push([command_rows[i].command.toLowerCase(), i]);
break;
case "foreach":
case "endforeach":
forEachCmds.push([command_rows[i].command.toLowerCase(), i]);
break;
}
}
} else {
var command = command_rows[i].trElement.cells[0].innerHTML;
var target = command_rows[i].trElement.cells[1].innerHTML;
var value = command_rows[i].trElement.cells[2].innerHTML;
switch (command.toLowerCase()) {
case "label":
this.gotoLabels[ target ] = i;
break;
case "while":
case "endwhile":
cycles.push([command.toLowerCase(), i]);
break;
case "foreach":
case "endforeach":
forEachCmds.push([command.toLowerCase(), i]);
break;
}
}
}
} catch (err) {
this.utils.error(err, new Error());
this.utils.throwError(err, new Error());
}
var i = 0;
while (cycles.length) {
if(i >= cycles.length) {
this.utils.throwError("non-matching while/endWhile found", new Error());
}
switch(cycles[i][0]) {
case "while":
if(( i + 1 < cycles.length ) && ( "endwhile" == cycles[i + 1][0] )) {
// pair found
this.whileLabels.ends[ cycles[i + 1][1] ] = cycles[i][1];
this.whileLabels.whiles[ cycles[i][1] ] = cycles[i + 1][1];
cycles.splice(i, 2);
i = 0;
} else {
++i;
}
break;
case "endwhile":
++i;
break;
}
}
},
continueFromRow: function (row_num) {
if((row_num == undefined) || (row_num == null) || (row_num < 0) || isNaN(row_num)) {
this.utils.throwError("Invalid row_num specified.", new Error());
}
if(this.utils.isIDE()) {
this.utils.getTestCase().debugContext.debugIndex = row_num;
} else {
this.utils.getTestCase().nextCommandRowIndex = row_num + 1;
}
}
});
var sideFlow = null;
(function() {
// Proxy design pattern - Overwrite Selenium's reset() function and then invoke the original impl
var proxied = Selenium.prototype.reset;
Selenium.prototype.reset = function() {
sideFlow = new SideFlow(new SideFlowUtils());
proxied.apply( this, arguments );
};
})();
// do nothing. simple label
Selenium.prototype.doLabel = function() { };
Selenium.prototype.doGotoLabel = function(label) {
if(undefined == sideFlow.gotoLabels[label]) {
sideFlow.utils.throwError( "Specified label '" + label + "' is not found.", new Error() );
}
sideFlow.continueFromRow( sideFlow.gotoLabels[label] );
};
Selenium.prototype.doGoto = Selenium.prototype.doGotoLabel;
Selenium.prototype.doGotoIf = function(condition, label) {
if(eval(condition)) {
this.doGotoLabel( label );
}
};
Selenium.prototype.doWhile = function(condition) {
if(!eval(condition)) {
var last_row;
if(sideFlow.utils.isIDE()) {
last_row = sideFlow.utils.getTestCase().debugContext.debugIndex;
} else {
last_row = sideFlow.utils.getTestCase().nextCommandRowIndex - 1;
}
var end_while_row = sideFlow.whileLabels.whiles[ last_row ];
if((undefined == end_while_row) || isNaN(end_while_row)) {
sideFlow.utils.throwError( "Corresponding 'endWhile' is not found.", new Error() );
}
sideFlow.continueFromRow( end_while_row );
}
};
Selenium.prototype.doEndWhile = function() {
var last_row;
if(sideFlow.utils.isIDE()) {
last_row = sideFlow.utils.getTestCase().debugContext.debugIndex;
} else {
last_row = sideFlow.utils.getTestCase().nextCommandRowIndex - 1;
}
var while_row = sideFlow.whileLabels.ends[ last_row ] - 1;
if((undefined == while_row) || isNaN(while_row)) {
sideFlow.utils.throwError( "Corresponding 'While' is not found.", new Error() );
}
sideFlow.continueFromRow( while_row );
};
Selenium.prototype.doPush = function(value, varName) {
if(!storedVars[varName]) {
storedVars[varName] = new Array();
}
if(typeof storedVars[varName] !== 'object') {
sideFlow.utils.throwError("Cannot push value onto non-array " + varName, new Error());
} else {
storedVars[varName].push(value);
}
};