-
Notifications
You must be signed in to change notification settings - Fork 7
/
testCase.js
520 lines (466 loc) · 14.7 KB
/
testCase.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/*
* Copyright 2005 Shinya Kasatani
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
function Command(command, target, value) {
this.command = command != null ? command : '';
if (target != null && target instanceof Array) {
if (target[0]) {
this.target = target[0][0];
this.targetCandidates = target;
} else {
this.target = "LOCATOR_DETECTION_FAILED";
}
} else {
this.target = target != null ? target : '';
}
this.value = value != null ? value : '';
}
Command.prototype.createCopy = function() {
var copy = new Command();
for (prop in this) {
copy[prop] = this[prop];
}
return copy;
};
Command.prototype.getRealValue = function() {
if (this.value) {
return this.value;
} else {
return this.target;
}
}
Command.prototype.getRealTarget = function() {
if (this.value) {
return this.target;
} else {
return null;
}
}
Command.innerHTML = function(element) {
var html = "";
var nodes = element.childNodes;
for (var i = 0; i < nodes.length; i++) {
var node = nodes.item(i);
switch (node.nodeType) {
case 1: // ELEMENT_NODE
html += "<" + node.nodeName + ">";
html += this.innerHTML(node);
html += "</" + node.nodeName + ">";
break;
case 3: // TEXT_NODE
html += node.data;
break;
}
}
return html;
}
Command.loadAPI = function() {
if (!this.functions) {
var document;
var documents = this.apiDocuments;
var functions = {};
// document.length will be 1 by default, but will grow with plugins
for (var d = 0; d < documents.length; d++) {
// set the current document. again, by default this is the iedoc-core.xml
document = documents[d];
// <function name="someName">
// <param name="targetName">description</param>
// <param name="valueName">description</param> -- optional
// <return type="string">description</return> -- optional
// <comment>description for ide here</comment>
// </function>
var functionElements = document.apidoc.function;
for (var i = 0; i < functionElements.length; i++) {
var element = functionElements[i];
var def = new CommandDefinition(String(element.$.name));
var returns = element.return;
if (returns && returns.length > 0) {
var returnType = new String(returns[0].$.type);
returnType = returnType.replace(/string/, "String");
returnType = returnType.replace(/number/, "Number");
def.returnType = returnType;
def.returnDescription = returns[0]._;
}
var comments = element.comment;
if (comments && comments.length > 0) {
def.comment = comments[0];
}
var alternatives = element.alternatives;
if (alternatives && alternatives.length > 0) {
def.alternatives = alternatives[0];
}
var deprecated = element.deprecated;
if (deprecated && deprecated.length > 0) {
def.deprecated = deprecated[0];
if (def.deprecated.length == 0 && def.alternatives) {
def.deprecated = "Use the ${alternatives} command instead.";
}
}
var params = element.param;
for (var j = 0; j < (params && params.length); j++) {
var paramElement = params[j];
var param = {};
param.name = String(paramElement.$.name);
param.description = paramElement._;
def.params.push(param);
}
functions[def.name] = def;
// generate negative accessors
if (def.name.match(/^(is|get)/)) {
def.isAccessor = true;
functions["!" + def.name] = def.negativeAccessor();
}
if (def.name.match(/^assert/)) { // only assertSelected should match
var verifyDef = new CommandDefinition(def.name);
verifyDef.params = def.params;
functions["verify" + def.name.substring(6)] = verifyDef;
}
}
}
functions['assertFailureOnNext'] = new CommandDefinition('assertFailureOnNext');
functions['verifyFailureOnNext'] = new CommandDefinition('verifyFailureOnNext');
functions['assertErrorOnNext'] = new CommandDefinition('assertErrorOnNext');
functions['verifyErrorOnNext'] = new CommandDefinition('verifyErrorOnNext');
this.functions = functions;
}
return this.functions;
};
function CommandDefinition(name) {
this.name = name;
this.params = [];
}
CommandDefinition.prototype.getReferenceFor = function(command) {
var paramNames = [];
for (var i = 0; i < this.params.length; i++) {
paramNames.push(this.params[i].name);
}
var originalParamNames = paramNames.join(", ");
if (this.name.match(/^is|get/)) { // accessor
if (command.command) {
if (command.command.match(/^store/)) {
paramNames.push("variableName");
} else if (command.command.match(/^(assert|verify|waitFor)/)) {
if (this.name.match(/^get/)) {
paramNames.push("pattern");
}
}
}
}
var note = "";
if (command.command && command.command != this.name) {
note = "<dt>Generated from <strong>" + this.name + "(" +
originalParamNames + ")</strong></dt>";
}
var params = "";
if (this.params.length > 0) {
params += "<div>Arguments:</div><ul>";
for (var i = 0; i < this.params.length; i++) {
params += "<li>" + this.params[i].name + " - " + this.params[i].description + "</li>";
}
params += "</ul>";
}
var returns = "";
if (this.returnDescription) {
returns += "<dl><dt>Returns:</dt><dd>" + this.returnDescription + "</dd></dl>";
}
var deprecated = "";
if (this.deprecated) {
deprecated += '<div class="deprecated">This command is deprecated. ' + this.deprecated + "</div>";
if (this.alternatives) {
deprecated = deprecated.replace("${alternatives}", "<strong>" + CommandDefinition.getAlternative(command.command, this.alternatives) + "</strong>");
}
}
return "<dl><dt><strong>" + (command.command || this.name) + "(" +
paramNames.join(", ") + ")</strong></dt>" +
deprecated + note +
'<dd style="margin:5px;">' +
params + returns +
this.comment + "</dd></dl>";
};
CommandDefinition.prototype.negativeAccessor = function() {
var def = new CommandDefinition(this.name);
for (var name in this) {
def[name] = this[name];
}
def.isAccessor = true;
def.negative = true;
return def;
}
/**
* Figure out the correct alternative command to use based on the current command flavour
* @param command - e.g. "waitForTextNotPresent"
* @param alternative - e.g. "Text
* @returns {string}
*/
CommandDefinition.getAlternative = function(command, alternative) {
if (command == null) return '';
var alt = alternative;
var r = /^(.*?)(AndWait)?$/.exec(command);
var commandName = r[1];
var prefix = '';
var suffix = r[2] ? r[2] : '';
var negate = false;
r = /^(assert|verify|store|waitFor)(.*)$/.exec(commandName);
if (r) {
prefix = r[1];
var commandName = r[2];
if ((r = /^(.*)NotPresent$/.exec(commandName)) != null) {
negate = true;
} else if ((r = /^Not(.*)$/.exec(commandName)) != null) {
negate = true;
}
if (negate) {
if (alt.match(/Present$/)) {
alt = alt.replace(/Present$/, 'NotPresent');
} else {
prefix += 'Not';
}
}
}
return prefix + (prefix.length > 0 ? alt.charAt(0).toUpperCase() : alt.charAt(0).toLowerCase()) + alt.substr(1) + suffix;
};
Command.prototype.getDefinition = function() {
if (this.command == null) return null;
var commandName = this.command.replace(/AndWait$/, '');
var api = Command.loadAPI();
var r = /^(assert|verify|store|waitFor)(.*)$/.exec(commandName);
if (r) {
var suffix = r[2];
var prefix = "";
if ((r = /^(.*)NotPresent$/.exec(suffix)) != null) {
suffix = r[1] + "Present";
prefix = "!";
} else if ((r = /^Not(.*)$/.exec(suffix)) != null) {
suffix = r[1];
prefix = "!";
}
var booleanAccessor = api[prefix + "is" + suffix];
if (booleanAccessor) {
return booleanAccessor;
}
var accessor = api[prefix + "get" + suffix];
if (accessor) {
return accessor;
}
}
return api[commandName];
}
Command.prototype.getParameterAt = function(index) {
switch (index) {
case 0:
return this.target;
case 1:
return this.value;
default:
return null;
}
}
// Command.prototype.getAPI = function() {
// return window.editor.seleniumAPI;
// }
Command.prototype.type = 'command';
/**
* The string representation of a command is the command, target, and value
* delimited by padded pipes.
*/
Command.prototype.toString = function()
{
var s = this.command
if (this.target) {
s += ' | ' + this.target;
if (this.value) {
s += ' | ' + this.value;
}
}
return s;
}
Command.prototype.isRollup = function()
{
return /^rollup(?:AndWait)?$/.test(this.command);
}
function Comment(comment) {
this.comment = comment != null ? comment : '';
}
Comment.prototype.type = 'comment';
function Line(line) {
this.line = line;
}
Line.prototype.type = 'line';
Comment.prototype.createCopy = function() {
var copy = new Comment();
for (prop in this) {
copy[prop] = this[prop];
}
return copy;
};
function TestCase(tempTitle) {
if (!tempTitle) tempTitle = "Untitled";
this.log = console;//new Log("TestCase");
this.tempTitle = tempTitle;
this.formatLocalMap = {};
this.commands = [];
this.recordModifiedInCommands();
this.baseURL = "";
var testCase = this;
this.debugContext = {
reset: function() {
this.failed = false;
this.started = false;
this.debugIndex = -1;
},
nextCommand: function() {
if (!this.started) {
this.started = true;
this.debugIndex = testCase.startPoint ? testCase.commands.indexOf(testCase.startPoint) : 0
} else {
this.debugIndex++;
}
for (; this.debugIndex < testCase.commands.length; this.debugIndex++) {
var command = testCase.commands[this.debugIndex];
if (command.type == 'command') {
return command;
}
}
return null;
},
currentCommand: function() {
var command = testCase.commands[this.debugIndex];
if (!command) {
testCase.log.warn("currentCommand() not found: commands.length=" + testCase.commands.length + ", debugIndex=" + this.debugIndex);
}
return command;
}
}
}
// Create a shallow copy of testcase
TestCase.prototype.createCopy = function() {
var copy = new TestCase();
for (prop in this) {
copy[prop] = this[prop];
}
return copy;
};
// Store variables specific to each format in this hash.
TestCase.prototype.formatLocal = function(formatName) {
var scope = this.formatLocalMap[formatName];
if (!scope) {
scope = {};
this.formatLocalMap[formatName] = scope;
}
return scope;
}
// For backwards compatibility
TestCase.prototype.setCommands = function(commands) {
this.commands = commands;
this.recordModifiedInCommands();
}
TestCase.prototype.recordModifiedInCommands = function() {
if (this.commands.recordModified) {
return;
}
this.commands.recordModified = true;
var self = this;
var commands = this.commands;
var _push = commands.push;
commands.push = function(command) {
_push.call(commands, command);
self.setModified();
}
var _splice = commands.splice;
commands.splice = function(index, removeCount, command) {
var removed = null;
if (command !== undefined && command != null) {
removed = _splice.call(commands, index, removeCount, command);
} else {
removed = _splice.call(commands, index, removeCount);
}
self.setModified();
return removed;
}
var _pop = commands.pop;
commands.pop = function() {
var command = commands[commands.length - 1];
commands.splice(commands.length - 1, 1);
self.setModified();
return command;
}
}
TestCase.prototype.clear = function() {
var length = this.commands.length;
this.commands.splice(0, this.commands.length);
this.setModified();
};
TestCase.prototype.setModified = function() {
this.modified = true;
this.notify("modifiedStateUpdated");
}
TestCase.prototype.clearModified = function() {
this.modified = false;
this.notify("modifiedStateUpdated");
}
TestCase.prototype.checkTimestamp = function() {
if (this.file) {
if (this.lastModifiedTime < this.file.lastModifiedTime) {
this.lastModifiedTime = this.file.lastModifiedTime;
return true;
}
}
return false;
}
TestCase.prototype.getCommandIndexByTextIndex = function(text, index, formatter) {
this.log.debug("getCommandIndexByTextIndex: index=" + index);
var lineno = text.substring(0, index).split(/\n/).length - 1;
var header = this.formatLocal(formatter.name).header;
this.log.debug("lineno=" + lineno + ", header=" + header);
if (header) {
lineno -= header.split(/\n/).length - 1;
}
this.log.debug("this.commands.length=" + this.commands.length);
for (var i = 0; i < this.commands.length; i++) {
this.log.debug("lineno=" + lineno + ", i=" + i);
if (lineno <= 0) {
return i;
}
var command = this.commands[i];
if (command.line != null) {
lineno -= command.line.split(/\n/).length;
}
}
return this.commands.length;
}
TestCase.prototype.getTitle = function() {
if (this.title) {
return this.title;
} else if (this.file && this.file.leafName) {
return this.file.leafName.replace(/\.\w+$/,'');
} else if (this.tempTitle) {
return this.tempTitle;
} else {
return null;
}
}
TestCase.prototype.setBaseURL = function(baseURL) {
this.baseURL = baseURL;
}
TestCase.prototype.getBaseURL = function() {
if (!this.baseURL || this.baseURL == "") {
return "http://change-this-to-the-site-you-are-testing/";
} else {
return this.baseURL;
}
}
exports.TestCase = TestCase;
exports.Command = Command;
exports.Comment = Comment;