1
22
23 package com.liferay.portal.tools;
24
25 import com.liferay.portal.kernel.util.CharPool;
26 import com.liferay.portal.kernel.util.StringPool;
27 import com.liferay.portal.kernel.util.StringUtil;
28 import com.liferay.portal.kernel.util.UnicodeFormatter;
29 import com.liferay.portal.tools.servicebuilder.ServiceBuilder;
30 import com.liferay.portal.util.FileImpl;
31
32 import java.io.File;
33
34 import org.apache.tools.ant.DirectoryScanner;
35
36
42 public class SeleneseToJavaBuilder {
43
44 public static void main(String[] args) throws Exception {
45 if (args.length == 1) {
46 new SeleneseToJavaBuilder(args[0]);
47 }
48 else {
49 throw new IllegalArgumentException();
50 }
51 }
52
53 public SeleneseToJavaBuilder(String basedir) throws Exception {
54 DirectoryScanner ds = new DirectoryScanner();
55
56 ds.setBasedir(basedir);
57 ds.setIncludes(new String[] {"**\\*.html"});
58
59 ds.scan();
60
61 String[] files = ds.getIncludedFiles();
62
63 for (int i = 0; i < files.length; i++) {
64
65
69
78
79 translate(basedir, files[i]);
80 }
81 }
82
83 protected String fixParam(String param) {
84 StringBuilder sb = new StringBuilder();
85
86 char[] array = param.toCharArray();
87
88 for (int i = 0; i < array.length; ++i) {
89 char c = array[i];
90
91 if (c == CharPool.BACK_SLASH) {
92 sb.append("\\\\");
93 }
94 else if (c == CharPool.QUOTE) {
95 sb.append("\\\"");
96 }
97 else if (Character.isWhitespace(c)) {
98 sb.append(c);
99 }
100 else if ((c < 0x0020) || (c > 0x007e)) {
101 sb.append("\\u");
102 sb.append(UnicodeFormatter.charToHex(c));
103 }
104 else {
105 sb.append(c);
106 }
107 }
108
109 return StringUtil.replace(
110 sb.toString(), _FIX_PARAM_OLD_SUBS, _FIX_PARAM_NEW_SUBS);
111 }
112
113 protected String[] getParams(String step) throws Exception {
114 String[] params = new String[3];
115
116 int x = 0;
117 int y = 0;
118
119 for (int i = 0; i < 3; i++) {
120 x = step.indexOf("<td>", x) + 4;
121 y = step.indexOf("\n", x);
122 y = step.lastIndexOf("</td>", y);
123
124 params[i] = step.substring(x, y);
125 }
126
127 return params;
128 }
129
130 protected void translate(String basedir, String file) throws Exception {
131 file = StringUtil.replace(
132 file, StringPool.BACK_SLASH, StringPool.SLASH);
133
134 int x = file.lastIndexOf(StringPool.SLASH);
135 int y = file.indexOf(StringPool.PERIOD);
136
137 String testPackagePath = StringUtil.replace(
138 file.substring(0, x), StringPool.SLASH, StringPool.PERIOD);
139 String testName = file.substring(x + 1, y);
140 String testMethodName =
141 "test" + testName.substring(0, testName.length() - 4);
142 String testFileName = basedir + "/" + file.substring(0, y) + ".java";
143
144 StringBuilder sb = new StringBuilder();
145
146 sb.append("package " + testPackagePath + ";\n\n");
147
148 sb.append("import com.liferay.portal.kernel.util.StringPool;\n");
149 sb.append("import com.liferay.portalweb.portal.BaseTestCase;\n\n");
150 sb.append(
151 "import com.liferay.portalweb.portal.util.RuntimeVariables;\n\n");
152
153 sb.append("public class " + testName + " extends BaseTestCase {");
154
155 sb.append("public void " + testMethodName + "() throws Exception {");
156
157 String xml = _fileUtil.read(basedir + "/" + file);
158
159 if ((xml.indexOf("<title>" + testName + "</title>") == -1) ||
160 (xml.indexOf("colspan=\"3\">" + testName + "</td>") == -1)) {
161
162 System.out.println(testName + " has an invalid test name");
163 }
164
165 x = xml.indexOf("<tbody>");
166 y = xml.indexOf("</tbody>");
167
168 xml = xml.substring(x, y + 8);
169
170 x = 0;
171 y = 0;
172
173 while (true) {
174 x = xml.indexOf("<tr>", x);
175 y = xml.indexOf("\n</tr>", x);
176
177 if ((x == -1) || (y == -1)) {
178 break;
179 }
180
181 x += 6;
182 y++;
183
184 String step = xml.substring(x, y);
185
186 String[] params = getParams(step);
187
188 String param1 = params[0];
189 String param2 = fixParam(params[1]);
190 String param3 = fixParam(params[2]);
191
192 if (param1.equals("assertConfirmation")) {
193 param2 = StringUtil.replace(param2, "?", "[\\\\s\\\\S]");
194
195 sb.append("assertTrue(selenium.getConfirmation().matches(\"^");
196 sb.append(param2);
197 sb.append("$\"));");
198 }
199 else if (param1.equals("assertElementPresent") ||
200 param1.equals("assertElementNotPresent")) {
201
202 if (param1.equals("assertElementPresent")) {
203 sb.append("assertTrue");
204 }
205 else if (param1.equals("assertElementNotPresent")) {
206 sb.append("assertFalse");
207 }
208
209 sb.append("(selenium.isElementPresent(\"");
210 sb.append(param2);
211 sb.append("\"));");
212 }
213 else if (param1.equals("click") || param1.equals("mouseDown") ||
214 param1.equals("mouseUp") || param1.equals("open") ||
215 param1.equals("selectFrame") ||
216 param1.equals("selectWindow")) {
217
218 sb.append("selenium.");
219 sb.append(param1);
220 sb.append("(\"");
221 sb.append(param2);
222 sb.append("\");");
223 }
224 else if (param1.equals("clickAndWait")) {
225 sb.append("selenium.click(\"");
226 sb.append(param2);
227 sb.append("\");");
228 sb.append("selenium.waitForPageToLoad(\"30000\");");
229 }
230 else if (param1.equals("close")) {
231 sb.append("selenium.");
232 sb.append(param1);
233 sb.append("();");
234 }
235 else if (param1.equals("pause")) {
236 sb.append("Thread.sleep(");
237 sb.append(param2);
238 sb.append(");");
239 }
240 else if (param1.equals("addSelection") || param1.equals("select") ||
241 param1.equals("type") || param1.equals("typeKeys") ||
242 param1.equals("waitForPopUp")) {
243
244 sb.append("selenium.");
245 sb.append(param1);
246 sb.append("(\"");
247 sb.append(param2);
248 sb.append("\", RuntimeVariables.replace(\"");
249 sb.append(param3);
250 sb.append("\"));");
251 }
252 else if (param1.equals("selectAndWait")) {
253 sb.append("selenium.select(\"");
254 sb.append(param2);
255 sb.append("\", \"");
256 sb.append(param3);
257 sb.append("\");");
258 sb.append("selenium.waitForPageToLoad(\"30000\");");
259 }
260 else if (param1.equals("storeText")) {
261 sb.append("String ");
262 sb.append(param3);
263 sb.append(" = selenium.getText(\"");
264 sb.append(param2);
265 sb.append("\");");
266
267 sb.append("RuntimeVariables.setValue(\"");
268 sb.append(param3);
269 sb.append("\", ");
270 sb.append(param3);
271 sb.append(");");
272 }
273 else if (param1.equals("verifyElementPresent") ||
274 param1.equals("verifyElementNotPresent")) {
275
276 if (param1.equals("verifyElementPresent")) {
277 sb.append("verifyTrue");
278 }
279 else if (param1.equals("verifyElementNotPresent")) {
280 sb.append("verifyFalse");
281 }
282
283 sb.append("(selenium.isElementPresent(\"");
284 sb.append(param2);
285 sb.append("\"));");
286 }
287 else if (param1.equals("verifyTextPresent") ||
288 param1.equals("verifyTextNotPresent")) {
289
290 if (param1.equals("verifyTextPresent")) {
291 sb.append("verifyTrue");
292 }
293 else if (param1.equals("verifyTextNotPresent")) {
294 sb.append("verifyFalse");
295 }
296
297 sb.append("(selenium.isTextPresent(\"");
298 sb.append(param2);
299 sb.append("\"));");
300 }
301 else if (param1.equals("verifyTitle")) {
302 sb.append("verifyEquals(\"");
303 sb.append(param2);
304 sb.append("\", selenium.getTitle());");
305 }
306 else if (param1.equals("waitForElementNotPresent") ||
307 param1.equals("waitForElementPresent") ||
308 param1.equals("waitForTextNotPresent") ||
309 param1.equals("waitForTextPresent")) {
310
311 sb.append("for (int second = 0;; second++) {");
312 sb.append("if (second >= 60) {");
313 sb.append("fail(\"timeout\");");
314 sb.append("}");
315
316 sb.append("try {");
317 sb.append("if (");
318
319 if (param1.equals("waitForElementNotPresent") ||
320 param1.equals("waitForTextNotPresent")) {
321
322 sb.append("!");
323 }
324
325 sb.append("selenium.");
326
327 if (param1.equals("waitForElementNotPresent") ||
328 param1.equals("waitForElementPresent")) {
329
330 sb.append("isElementPresent");
331 }
332 else if (param1.equals("waitForTextNotPresent") ||
333 param1.equals("waitForTextPresent")) {
334
335 sb.append("isTextPresent");
336 }
337
338 sb.append("(\"");
339 sb.append(param2);
340 sb.append("\")) {");
341 sb.append("break;");
342 sb.append("}");
343 sb.append("}");
344 sb.append("catch (Exception e) {");
345 sb.append("}");
346
347 sb.append("Thread.sleep(1000);");
348 sb.append("}");
349 }
350 else if (param1.equals("waitForTable")) {
351 sb.append("for (int second = 0;; second++) {");
352 sb.append("if (second >= 60) {");
353 sb.append("fail(\"timeout\");");
354 sb.append("}");
355
356 sb.append("try {");
357 sb.append("if (StringPool.BLANK.equals(selenium.getTable(\"");
358 sb.append(param2);
359 sb.append("\"))) {");
360 sb.append("break;");
361 sb.append("}");
362 sb.append("}");
363 sb.append("catch (Exception e) {");
364 sb.append("}");
365
366 sb.append("Thread.sleep(1000);");
367 sb.append("}");
368 }
369 else {
370 System.out.println(param1 + " was not translated");
371 }
372 }
373
374 sb.append("}");
375 sb.append("}");
376
377 String content = sb.toString();
378
379 ServiceBuilder.writeFile(new File(testFileName), content);
380 }
381
382 private static final String[] _FIX_PARAM_OLD_SUBS = new String[] {
383 "\\\\n", "<br />"
384 };
385
386 private static final String[] _FIX_PARAM_NEW_SUBS = new String[] {
387 "\\n", "\\n"
388 };
389
390 private static FileImpl _fileUtil = new FileImpl();
391
392 }