-
Notifications
You must be signed in to change notification settings - Fork 7
/
convertHtmlToTestCase.js
407 lines (379 loc) · 13.5 KB
/
convertHtmlToTestCase.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
// Characters that should be escaped when saving.
var XhtmlEntities = require("./XhtmlEntities").XhtmlEntities;
var Command = require("./testCase").Command;
var Comment = require("./testCase").Comment;
var log = console;
var EncodeToXhtmlEntity = ["amp", "gt", "lt", "quot", "nbsp"];
var XhtmlEntityFromChars = {};
for (var i = 0; i < EncodeToXhtmlEntity.length; i++) {
var entity = EncodeToXhtmlEntity[i];
XhtmlEntityFromChars[XhtmlEntities[entity]] = entity;
}
// A regular expression that matches characters that can be converted to entities.
var XhtmlEntityChars = "[";
for (var code in XhtmlEntityFromChars) {
var c = parseInt(code).toString(16);
while (c.length < 4) {
c = "0" + c;
}
XhtmlEntityChars += "\\u" + c;
}
XhtmlEntityChars += "]";
function decodeText(text) {
if (text == null) return "";
text = text.replace(/<br\s*\/?>/gi, "\n");
text = text.replace(/&(\w+);/g, function(str, p1) {
var c = XhtmlEntities[p1];
if (c) {
return String.fromCharCode(c);
} else {
return str;
}
});
text = text.replace(/&#(\d+);/g, function(str, p1) {
return String.fromCharCode(parseInt(p1));
});
text = text.replace(/&#x([0-9a-f]+);/gi, function(str, p1) {
return String.fromCharCode(parseInt(p1, 16));
});
text = text.replace(/ +/g, " "); // truncate multiple spaces to single space
text = text.replace(/\xA0/g, " "); // treat nbsp as space
if ('true' == options.escapeDollar) {
text = text.replace(/([^\\])\$\{/g, '$1$$$${'); // replace [^\]${...} with $${...}
text = text.replace(/^\$\{/g, '$$$${'); // replace ^${...} with $${...}
text = text.replace(/\\\$\{/g, '$${'); // replace \${...} with ${...}
}
return text;
}
function encodeText(text) {
if (text == null) return "";
// & -> &
// & -> &amp;
// " -> &quot;
// \xA0 ->
text = text.replace(new RegExp(XhtmlEntityChars, "g"),
function(c) {
var entity = XhtmlEntityFromChars[c.charCodeAt(c)];
if (entity) {
return "&" + entity + ";";
} else {
throw "Failed to encode entity: " + c;
}
});
text = text.replace(/ {2,}/g, function(str) {
var result = '';
for (var i = 0; i < str.length; i++) {
result += ' ';
}
return result;
}); // convert multiple spaces to nbsp
if ('true' == options.escapeDollar) {
text = text.replace(/([^\$])\$\{/g, '$1\\${'); // replace [^$]${...} with \${...}
text = text.replace(/^\$\{/g, '\\${'); // replace ^${...} with \${...}
text = text.replace(/\$\$\{/g, '${'); // replace $${...} with ${...}
}
text = text.replace(/\n/g, "<br />");
return text;
}
function convertText(command, converter) {
var props = ['command', 'target', 'value'];
for (var i = 0; i < props.length; i++) {
var prop = props[i];
command[prop] = converter(command[prop]);
}
}
exports.setLogger = function (logger) {
log = logger;
};
/**
* Parse source and update TestCase. Throw an exception if any error occurs.
*
* @param testCase TestCase to update
* @param source The source to parse
*/
exports.parse = function parse(testCase, source) {
var commandRegexp = new RegExp(options.commandLoadPattern, 'i');
var commentRegexp = new RegExp(options.commentLoadPattern, 'i');
var commandOrCommentRegexp = new RegExp("((" + options.commandLoadPattern + ")|(" + options.commentLoadPattern + "))", 'ig');
var doc = source;
var commands = [];
var commandFound = false;
var lastIndex;
while (true) {
//log.debug("doc=" + doc + ", commandRegexp=" + commandRegexp);
lastIndex = commandOrCommentRegexp.lastIndex;
var docResult = commandOrCommentRegexp.exec(doc);
if (docResult) {
if (docResult[2]) { // command
var command = new Command();
command.skip = docResult.index - lastIndex;
command.index = docResult.index;
var result = commandRegexp.exec(doc.substring(lastIndex));
eval(options.commandLoadScript);
convertText(command, decodeText);
commands.push(command);
if (!commandFound) {
// remove comments before the first command or comment
for (var i = commands.length - 1; i >= 0; i--) {
if (commands[i].skip > 0) {
commands.splice(0, i);
break;
}
}
testCase.header = doc.substr(0, commands[0].index);
commandFound = true;
}
} else { // comment
var comment = new Comment();
comment.skip = docResult.index - lastIndex;
comment.index = docResult.index;
var result = commentRegexp.exec(doc.substring(lastIndex));
eval(options.commentLoadScript);
commands.push(comment);
}
} else {
break;
}
}
if (commands.length > 0) {
testCase.footer = doc.substring(lastIndex);
log.debug("header=" + testCase.header);
log.debug("footer=" + testCase.footer);
if (testCase.header &&
/<link\s+rel="selenium\.base"\s+href="(.*)"/.test(testCase.header)) {
testCase.baseURL = decodeURI(RegExp.$1);
}
//log.debug("commands.length=" + commands.length);
testCase.commands = commands;
}else {
//Samit: Fix: Atleast try to allow empty test cases, before screaming murder
//Note: This implementation will work with empty test cases saved with this formatter only
var templateVars = matchTemplateAndExtractVars(source, options.testTemplate);
if (templateVars) {
//Since the matching has succeeded, update the test case with found variable values
if (templateVars["baseURL"]) {
testCase.baseURL = templateVars["baseURL"][0];
}
if (templateVars["commands"]) {
testCase.header = doc.substring(0, templateVars["commands"][1]);
testCase.footer = doc.substring(templateVars["commands"][1]);
log.debug("header=" + testCase.header);
log.debug("footer=" + testCase.footer);
}
testCase.commands = commands;
}else {
throw "no command found";
}
}
}
//Samit: Enh: Utility function to match the document against a template and extract the variables marked as ${} in the template
function matchTemplateAndExtractVars(doc, template) {
var matchTextRa = template.split(/(\$\{\w+\})/g);
var templateVars = {};
var captureVar;
var matchIndex = 0;
for (var i=0; i<matchTextRa.length; i++) {
var matchedVar = matchTextRa[i].match(/\$\{(\w+)\}/i);
if (matchedVar) {
//Found variable!
if (templateVars[matchedVar[1]]) {
//already captured, treat as static text and match later
matchTextRa[i] = templateVars[matchedVar[1]][0];
}else {
//variable capture required
if (captureVar) {
//Error: Capture failed as there is no way to delimit adjacent variables without static text between them
log.error("Error: Capture failed as there is no way to delimit adjacent variables without static text between them");
return null;
}
captureVar = matchedVar[1];
continue;
}
}
//static text
if (captureVar) {
//search for static string
var index = doc.indexOf(matchTextRa[i], matchIndex);
if (index >= 0) {
//matched
templateVars[captureVar] = [doc.substring(matchIndex, index), matchIndex];
matchIndex = matchTextRa[i].length + index;
captureVar = null;
}else {
//Error: Match failed
log.error("Error: Match failed");
return null;
}
}else {
//match text
if (doc.substr(matchIndex, matchTextRa[i].length) == matchTextRa[i]) {
//matched!
matchIndex += matchTextRa[i].length;
}else {
//Error: Match failed
log.error("Error: Match failed");
return null;
}
}
}
if (captureVar) {
// capture the final variable if any
templateVars[captureVar] = [doc.substring(matchIndex), matchIndex];
}
return templateVars;
}
function getSourceForCommand(commandObj) {
var command = null;
var comment = null;
var template = '';
if (commandObj.type == 'command') {
command = commandObj;
command = command.createCopy();
convertText(command, encodeText);
template = options.commandTemplate;
} else if (commandObj.type == 'comment') {
comment = commandObj;
template = options.commentTemplate;
}
var result;
var text = template.replace(/\$\{([a-zA-Z0-9_\.]+)\}/g,
function(str, p1, offset, s) {
result = eval(p1);
return result != null ? result : '';
});
return text;
}
/**
* Format an array of commands to the snippet of source.
* Used to copy the source into the clipboard.
*
* @param The array of commands to sort.
*/
function formatCommands(commands) {
var commandsText = '';
for (i = 0; i < commands.length; i++) {
var text = getSourceForCommand(commands[i]);
commandsText = commandsText + text;
}
return commandsText;
}
/**
* Format TestCase and return the source.
* The 3rd and 4th parameters are used only in default HTML format.
*
* @param testCase TestCase to format
* @param name The name of the test case, if any. It may be used to embed title into the source.
*/
function format(testCase, name) {
var text;
var commandsText = "";
var testText;
var i;
for (i = 0; i < testCase.commands.length; i++) {
var text = getSourceForCommand(testCase.commands[i]);
commandsText = commandsText + text;
}
var testText;
if (testCase.header == null || testCase.footer == null) {
testText = options.testTemplate;
testText = testText.replace(/\$\{name\}/g, name);
var encoding = options["global.encoding"];
if (!encoding) encoding = "UTF-8";
testText = testText.replace(/\$\{encoding\}/g, encoding);
testText = testText.replace(/\$\{baseURL\}/g, encodeURI(testCase.getBaseURL()));
var commandsIndex = testText.indexOf("${commands}");
if (commandsIndex >= 0) {
var header = testText.substr(0, commandsIndex);
var footer = testText.substr(commandsIndex + "${commands}".length);
testText = header + commandsText + footer;
}
} else {
testText = testCase.header + commandsText + testCase.footer;
}
return testText;
}
function defaultExtension() {
return options.defaultExtension;
}
/*
* Optional: The customizable option that can be used in format/parse functions.
*/
var options = {
commandLoadPattern:
"<tr\s*[^>]*>" +
"\\s*(<!--[\\d\\D]*?-->)?" +
"\\s*<td\s*[^>]*>\\s*([\\w]*?)\\s*</td>" +
"\\s*<td\s*[^>]*>([\\d\\D]*?)</td>" +
"\\s*(<td\s*/>|<td\s*[^>]*>([\\d\\D]*?)</td>)" +
"\\s*</tr>\\s*",
commandLoadScript:
"command.command = result[2];\n" +
"command.target = result[3];\n" +
"command.value = result[5] || '';\n",
commentLoadPattern:
"<!--([\\d\\D]*?)-->\\s*",
commentLoadScript:
"comment.comment = result[1];\n",
testTemplate:
'<?xml version="1.0" encoding="${encoding}"?>\n' +
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n' +
'<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">\n' +
'<head profile="http://selenium-ide.openqa.org/profiles/test-case">\n' +
'<meta http-equiv="Content-Type" content="text/html; charset=${encoding}" />\n' +
'<link rel="selenium.base" href="${baseURL}" />\n' +
"<title>${name}</title>\n" +
"</head>\n" +
"<body>\n" +
'<table cellpadding="1" cellspacing="1" border="1">\n'+
'<thead>\n' +
'<tr><td rowspan="1" colspan="3">${name}</td></tr>\n' +
"</thead><tbody>\n" +
"${commands}\n" +
"</tbody></table>\n" +
"</body>\n" +
"</html>\n",
commandTemplate:
"<tr>\n" +
"\t<td>${command.command}</td>\n" +
"\t<td>${command.target}</td>\n" +
"\t<td>${command.value}</td>\n" +
"</tr>\n",
commentTemplate:
"<!--${comment.comment}-->\n",
escapeDollar:
"false",
defaultExtension: "html"
};
/*
* Optional: XUL XML String for the UI of the options dialog
*/
var configForm =
//'<tabbox flex="1"><tabs orient="horizontal"><tab label="Load"/><tab label="Save"/></tabs>' +
//'<tabpanels flex="1">' +
//'<tabpanel orient="vertical">' +
'<description>Regular expression for each command entry</description>' +
'<textbox id="options_commandLoadPattern" flex="1"/>' +
'<separator class="thin"/>' +
'<description>Script to load command from the pattern</description>' +
'<textbox id="options_commandLoadScript" multiline="true" flex="1" rows="2"/>' +
//'<separator class="thin"/>' +
//'<description>Regular expression for comments between commands</description>' +
//'<textbox id="options_commentLoadPattern" flex="1"/>' +
//'<separator class="thin"/>' +
//'<description>Script to load comment from the pattern</description>' +
//'<textbox id="options_commentLoadScript" multiline="true" flex="1" rows="2"/>' +
'<separator class="groove"/>' +
//'</vbox><vbox>' +
//'</tabpanel>' +
//'<tabpanel orient="vertical">' +
'<description>Template for new test html file</description>' +
'<textbox id="options_testTemplate" multiline="true" flex="1" rows="3"/>' +
'<separator class="thin"/>' +
'<description>Template for command entries in the test html file</description>' +
'<textbox id="options_commandTemplate" multiline="true" flex="1" rows="3"/>' +
'<separator class="groove"/>' +
'<checkbox id="options_escapeDollar" label="Escape \'$' + '{\' as \'\\$' + '{\' (useful for JSP 2.0)"/>';
//'<separator class="thin"/>' +
//'<description>Template for comment entries in the test html file</description>' +
//'<textbox id="options_commentTemplate" multiline="true" flex="1"/>' +
//'</tabpanel></tabpanels></tabbox>';