-
Notifications
You must be signed in to change notification settings - Fork 1
/
seleniumScalaFormat.js
387 lines (333 loc) · 11.3 KB
/
seleniumScalaFormat.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
/*
* Format for Selenium Remote Control Scala client.
*/
var subScriptLoader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"].getService(Components.interfaces.mozIJSSubScriptLoader);
subScriptLoader.loadSubScript('chrome://selenium-ide/content/formats/remoteControl.js', this);
this.startsWith = function (s,e) {
return (s.match("^\\s*" + e));
};
this.trim = function (s) {
return (s.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""));
};
this.name = "scala-specs2";
/**
* A class for processing commands
*/
function CommandProcessor() {
// contain the spec functions to add after spec definition
this.functions = [];
// the specification declarations
this.specs=[];
// Since there is not good way to create a function name we will
// reuse the same name with an index
this.functionIndex = 1;
// if defined then the current function being constructed
this.currentFunction = undefined;
// the type of the last command processed.
// Types are:
// undefined (initial state),
// spec
// assert
// other
this.lastCommand = undefined;
// If defined the spec comment under construction
this.specDec = undefined;
this.endFunction = function () {
if (this.currentFunction !== undefined) {
if (this.lastCommand !== 'assert') {
this.currentFunction += indents(2) + "success\n";
}
this.currentFunction += indents(1) + "}";
this.functions.push(this.currentFunction);
this.currentFunction = undefined;
}
};
this.repeat = function (c, n) {
var str = "";
for (var i = 0; i < n; i++) {
str += c;
}
return str;
};
this.handleSpecDeclaration = function (code) {
var spec,spaces;
if (this.lastCommand === 'spec') {
this.specs.push(this.specDec);
}
this.lastCommand = 'spec';
// this is a spec comment
spec = indents(2) + '"' + trim(code.substr(1)) + '"';
spaces = options.rightColumnIndent - spec.length;
this.specDec = spec + this.repeat(" ", spaces);
}
this.processCommand = function (command, code) {
var methodName;
if (this.lastCommand === undefined && !startsWith(code, "//-")) {
this.handleSpecDeclaration("- "+options.initialSpec);
}
if (this.specDec !== undefined) {
methodName = name.replace(/ |\+|-|\|/g, '_') + "_" + this.functionIndex;
this.functionIndex += 1;
this.specDec += "! " + methodName;
if (command.command.match(/^(verify|assert)/)) {
this.lastCommand = 'assert';
} else {
this.lastCommand = 'other';
}
this.currentFunction =
indents(1) + "def " + methodName + " = {\n"+
indents(2) + "import selenium._\n" +
indents(2) + code + "\n";
this.specs.push(this.specDec);
this.specDec = undefined;
} else {
this.currentFunction += indents(2) + code + "\n";
}
};
this.output = function () {
if (this.currentFunction !== undefined) {
this.endFunction();
}
var closeStep = ""
return this.specs.join("^\n") + closeStep + "\n\n" + this.functions.join("\n\n");
};
}
this.formatCommands = function (commands) {
var processor = new CommandProcessor();
commands = filterForRemoteControl(commands);
if (this.lastIndent == null) {
this.lastIndent = '';
}
for (var i = 0; i < commands.length; i++) {
var line = null;
var command = commands[i];
if (command.type == 'line') {
line = command.line;
} else if (command.type == 'command') {
line = formatCommand(command);
if (line != null) line = addIndent(line);
command.line = line;
} else if (command.type == 'comment') {
if(startsWith(command.comment, '-')) {
processor.endFunction();
processor.handleSpecDeclaration(command.comment);
} else {
line = this.formatComment(command);
if (line != null) {
line = addIndent(line);
}
}
command.line = line;
}
command.charIndex = this.commandCharIndex;
if (line != null) {
updateIndent(line);
processor.processCommand(command, line);
this.commandCharIndex += line.length;
}
}
result = processor.output();
return result;
}
function formatComment(comment) {
return comment.comment.replace(/.+/mg, function(str) {
return "// " + str;
});
}
function useSeparateEqualsForArray() {
return false;
}
function testMethodName(testName) {
return capitalize(testName).replace(/ /g,"_");
}
function assertTrue(expression) {
return expression.toString()+" must beTrue";
}
function verifyTrue(expression) {return assertTrue(expression);}
function assertFalse(expression) {
return expression.toString() + "must beFalse";
}
function verifyFalse(expression) {return assertFalse(expression);}
function assignToVariable(type, variable, expression) {
return "val " + variable + " = " + expression.toString();
}
function ifCondition(expression, callback) {
return "if (" + expression.toString() + ") {\n" + callback() + "}";
}
function joinExpression(expression) {
return expression.toString() + ".mkString(',')";
}
function waitFor(expression) {
return "doWait("+expression.toString()+")";
}
function assertOrVerifyFailure(line, isAssert) {
return "throwA[Throwable] {"+line+"}";
}
Equals.prototype.toString = function() {
return this.e1.toString() + " == " + this.e2.toString();
};
Equals.prototype.assert = function() {
return this.e1.toString() + " must_== " + this.e2.toString();
};
Equals.prototype.verify = function() {
return this.e1.toString() + " must_== " + this.e2.toString();
};
NotEquals.prototype.toString = function() {
return this.e1.toString() + " != " + this.e2.toString() ;
};
NotEquals.prototype.assert = function() {
return this.e1.toString() + " must_!= " + this.e2.toString();
};
NotEquals.prototype.verify = NotEquals.assert
RegexpMatch.prototype.toString = function() {
return this.expression + ".matches (\"\"\"" + this.pattern + "\"\"\")";
};
RegexpMatch.prototype.verify = function() {
return this.expression + " must =~ (\"\"\"" + this.pattern + "\"\"\")";
};
function pause(milliseconds) {
return "sleep(" + parseInt(milliseconds, 10) + ")";
}
function echo(message) {
return "println(" + xlateArgument(message) + ")";
}
function statement(expression) {
return expression.toString();
}
function array(value) {
var str = 'Array(';
var i = 0;
for (i = 0; i < value.length; i++) {
str += string(value[i]);
if (i < value.length - 1) {
str += ", ";
}
}
str += ')';
return str;
}
function nonBreakingSpace() {
return "\"\\u00a0\"";
}
CallSelenium.prototype.toString = function() {
var result = '';
var i = 0;
if (this.negative) {
result += '!';
}
if(this.message === 'type') {
result += "`" + this.message + "`";
} else {
result += this.message
}
result += '(';
for (i = 0; i < this.args.length; i++) {
result += this.args[i];
if (i < this.args.length - 1) {
result += ', ';
}
}
result += ')';
return result;
};
/**
* Returns a string representing the suite for this formatter language.
*
* @param testSuite the suite to format
* @param filename the file the formatted suite will be saved as
*/
function formatSuite(testSuite, filename) {
var suiteClass = /^(\w+)/.exec(filename)[1];
suiteClass = suiteClass[0].toUpperCase() + suiteClass.substring(1);
var formattedSuite = "package "+options.packageName+"\n"
+ "\n"
+ "import org.specs2._\n"
+ "\n"
+ "class " + suiteClass + " extends SpecificationsFinder {def is=\n"
+ "\n"
+ indents(1) + "specs.foldLeft(\""+testSuite.tests[i].getTitle()+"\".title) {\n"
+ indents(2) + "(res,cur) => res ^ link(cur)\n"
+ indents(1) + "}\n"
+ "\n"
+ indents(1) + "def specs = List(\n";
var i = 0;
var testClass;
for (i = 0; i < testSuite.tests.length; ++i) {
testClass = testSuite.tests[i].getTitle();
formattedSuite += indents(2) + "classOf[" + testClass + "]";
if(i+1 < testSuite.tests.length) {
formattedSuite += ",";
}
formattedSuite += "\n";
}
formattedSuite += indents(1) + ")\n"
+ "}\n"
+ "\n"
+ "object "+suiteClass+" {\n"
+ "\n"
+ indents(1) + "def main(args:Array[String]) {\n"
+ indents(2) + "specs2.html.start(\"${packageName}."+suiteClass+"\")\n"
+ indents(1) + "}\n"
+ "}\n";
return formattedSuite;
}
this.options = {
receiver: "selenium",
environment: "*chrome",
packageName: "com.example.tests",
indent: '2',
initialIndents: '0',
rightColumnIndent: 80,
seleniumDriver: "org.openqa.selenium.firefox.FirefoxDriver",
specSummary: "This specification tests",
initialSpec: "The selenium script should succeed",
timeout: "30"
};
options.header =
"package c2c.webspecs\n" +
"package geonetwork\n" +
"package geocat\n" +
"package spec\n" +
"package ${packageName}\n" +
"\n" +
"import org.specs2._\n" +
"import matcher.ThrownExpectations\n" +
"import specification.Step\n" +
"import Thread._\n" +
"import org.openqa.selenium.WebDriverBackedSelenium\n" +
"import org.junit.runner.RunWith\n" +
"import org.specs2.runner.JUnitRunner\n" +
"\n" +
"@RunWith(classOf[JUnitRunner])\n" +
"class `${className}` extends GeocatSeleniumSpecification with ThrownExpectations { \n" +
"\n"+
indents(1) + "def isImpl = \n" +
indents(1) + "\"${specSummary} ${className}\" ^ \n";
options.footer =
"\n\n"+
indents(1) + "val TIMEOUT = " + options.timeout + "\n" +
indents(1) + "private def doWait(assertion: => Boolean, waits:Int=TIMEOUT, length:Int=1000) = \n" +
indents(2) + "(1 to waits).view map {_=> sleep(length)} find { _ => assertion }\n" +
"\n}\n";
this.configForm =
'<description>Variable for Selenium instance</description>' +
'<textbox id="options_receiver" />' +
'<description>Environment</description>' +
'<textbox id="options_environment" />' +
'<description>Package</description>' +
'<textbox id="options_packageName" />'+
'<description>Specification Summary Text</description>' +
'<textbox id="options_specSummary" />'+
'<description>Default Specification Text</description>' +
'<textbox id="options_initialSpec" />'+
'<description>Selenium Driver</description>' +
'<menulist id="options_seleniumDriver" >' +
'<menupopup>' +
'<menuitem label="Firefox" value="org.openqa.selenium.firefox.FirefoxDriver"/>' +
'<menuitem label="Chrome" value="org.openqa.selenium.chrome.ChromeDriver"/>' +
'<menuitem label="IE" value="org.openqa.selenium.ie.InternetExplorerDriver"/>' +
'<menuitem label="HtmlUnit" value="org.openqa.selenium.htmlunit.HtmlUnitDriver"/>' +
'</menupopup>' +
'</menulist>' +
'<description>Timeout in seconds</description>' +
'<textbox id="options_timeout" />';