1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.tools;
24  
25  import com.liferay.portal.kernel.util.CharPool;
26  import com.liferay.portal.kernel.util.FileUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.kernel.util.UnicodeFormatter;
30  import com.liferay.portal.tools.servicebuilder.ServiceBuilder;
31  import com.liferay.portal.util.InitUtil;
32  
33  import java.io.File;
34  
35  import java.util.HashMap;
36  import java.util.Map;
37  
38  import org.apache.tools.ant.DirectoryScanner;
39  
40  /**
41   * <a href="SeleneseToJavaBuilder.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class SeleneseToJavaBuilder {
47  
48      public static void main(String[] args) throws Exception {
49          InitUtil.initWithSpring();
50  
51          if (args.length == 1) {
52              new SeleneseToJavaBuilder(args[0]);
53          }
54          else {
55              throw new IllegalArgumentException();
56          }
57      }
58  
59      public SeleneseToJavaBuilder(String basedir) throws Exception {
60          DirectoryScanner ds = new DirectoryScanner();
61  
62          ds.setBasedir(basedir);
63          ds.setIncludes(new String[] {"**\\*.html"});
64  
65          ds.scan();
66  
67          String[] files = ds.getIncludedFiles();
68  
69          for (int i = 0; i < files.length; i++) {
70  
71              // I would have preferred to use XlateHtmlSeleneseToJava, but it
72              // is horribly out of sync with Selenium IDE and generates incorrect
73              // code.
74  
75              /*File file = new File(basedir + "/" + files[i]);
76  
77              String input = StringUtil.replace(file.toString(), "\\", "/");
78  
79              XlateHtmlSeleneseToJava.main(
80                  new String[] {
81                      "test", "-silent", input
82                  }
83              );*/
84  
85              translate(basedir, files[i]);
86          }
87      }
88  
89      protected String fixParam(String param) {
90          StringBuilder sb = new StringBuilder();
91  
92          char[] array = param.toCharArray();
93  
94          for (int i = 0; i < array.length; ++i) {
95              char c = array[i];
96  
97              if (c == CharPool.BACK_SLASH) {
98                  sb.append("\\\\");
99              }
100             else if (c == CharPool.QUOTE) {
101                 sb.append("\\\"");
102             }
103             else if (Character.isWhitespace(c)) {
104                 sb.append(c);
105             }
106             else if ((c < 0x0020) || (c > 0x007e)) {
107                 sb.append("\\u");
108                 sb.append(UnicodeFormatter.charToHex(c));
109             }
110             else {
111                 sb.append(c);
112             }
113         }
114 
115         return StringUtil.replace(
116             sb.toString(), _FIX_PARAM_OLD_SUBS, _FIX_PARAM_NEW_SUBS);
117     }
118 
119     protected String[] getParams(String step) throws Exception {
120         String[] params = new String[3];
121 
122         int x = 0;
123         int y = 0;
124 
125         for (int i = 0; i < 3; i++) {
126             x = step.indexOf("<td>", x) + 4;
127             y = step.indexOf("\n", x);
128             y = step.lastIndexOf("</td>", y);
129 
130             params[i] = step.substring(x, y);
131         }
132 
133         return params;
134     }
135 
136     protected void translate(String basedir, String file) throws Exception {
137         file = StringUtil.replace(
138             file, StringPool.BACK_SLASH, StringPool.SLASH);
139 
140         int x = file.lastIndexOf(StringPool.SLASH);
141         int y = file.indexOf(StringPool.PERIOD);
142 
143         String testPackagePath = StringUtil.replace(
144             file.substring(0, x), StringPool.SLASH, StringPool.PERIOD);
145         String testName = file.substring(x + 1, y);
146         String testMethodName =
147             "test" + testName.substring(0, testName.length() - 4);
148         String testFileName = basedir + "/" + file.substring(0, y) + ".java";
149 
150         StringBuilder sb = new StringBuilder();
151 
152         sb.append("package " + testPackagePath + ";\n\n");
153 
154         sb.append("import com.liferay.portal.kernel.util.FileUtil;\n");
155         sb.append("import com.liferay.portal.kernel.util.StringPool;\n");
156         sb.append("import com.liferay.portalweb.portal.BaseTestCase;\n\n");
157         sb.append(
158             "import com.liferay.portalweb.portal.util.RuntimeVariables;\n\n");
159 
160         sb.append("public class " + testName + " extends BaseTestCase {");
161 
162         sb.append("public void " + testMethodName + "() throws Exception {");
163 
164         String xml = FileUtil.read(basedir + "/" + file);
165 
166         if ((xml.indexOf("<title>" + testName + "</title>") == -1) ||
167             (xml.indexOf("colspan=\"3\">" + testName + "</td>") == -1)) {
168 
169             System.out.println(testName + " has an invalid test name");
170         }
171 
172         if (xml.indexOf("&quot;") != -1) {
173             xml = StringUtil.replace(xml, "&quot;", "\"");
174 
175             FileUtil.write(basedir + "/" + file, xml);
176         }
177 
178         x = xml.indexOf("<tbody>");
179         y = xml.indexOf("</tbody>");
180 
181         xml = xml.substring(x, y + 8);
182 
183         Map<String, String> labels = new HashMap<String, String>();
184 
185         int labelCount = 1;
186 
187         x = 0;
188         y = 0;
189 
190         while (true) {
191             x = xml.indexOf("<tr>", x);
192             y = xml.indexOf("\n</tr>", x);
193 
194             if ((x == -1) || (y == -1)) {
195                 break;
196             }
197 
198             x += 6;
199             y++;
200 
201             String step = xml.substring(x, y);
202 
203             String[] params = getParams(step);
204 
205             String param1 = params[0];
206             String param2 = fixParam(params[1]);
207             String param3 = fixParam(params[2]);
208 
209             if (param1.equals("label")) {
210                 String label = labels.get(param2);
211 
212                 if (label == null) {
213                     labelCount++;
214 
215                     label = labels.put(param2, String.valueOf(labelCount));
216                 }
217             }
218         }
219 
220         if (labels.size() > 0) {
221             sb.append("int label = 1;");
222 
223             sb.append("while (label >= 1) {");
224             sb.append("switch (label) {");
225             sb.append("case 1:");
226         }
227 
228         x = 0;
229         y = 0;
230 
231         while (true) {
232             x = xml.indexOf("<tr>", x);
233             y = xml.indexOf("\n</tr>", x);
234 
235             if ((x == -1) || (y == -1)) {
236                 break;
237             }
238 
239             x += 6;
240             y++;
241 
242             String step = xml.substring(x, y);
243 
244             String[] params = getParams(step);
245 
246             String param1 = params[0];
247             String param2 = fixParam(params[1]);
248             String param3 = fixParam(params[2]);
249 
250             if (param1.equals("addSelection") || param1.equals("select") ||
251                 param1.equals("type") || param1.equals("typeKeys") ||
252                 param1.equals("waitForPopUp")) {
253 
254                 sb.append("selenium.");
255                 sb.append(param1);
256                 sb.append("(\"");
257                 sb.append(param2);
258                 sb.append("\", RuntimeVariables.replace(\"");
259                 sb.append(param3);
260                 sb.append("\"));");
261             }
262             else if (param1.equals("assertChecked")) {
263                 sb.append("assertTrue(selenium.isChecked(");
264                 sb.append(param2);
265                 sb.append("));");
266             }
267             else if (param1.equals("assertConfirmation")) {
268                 param2 = StringUtil.replace(param2, "?", "[\\\\s\\\\S]");
269 
270                 sb.append("assertTrue(selenium.getConfirmation().matches(\"^");
271                 sb.append(param2);
272                 sb.append("$\"));");
273             }
274             else if (param1.equals("assertElementPresent") ||
275                      param1.equals("assertElementNotPresent")) {
276 
277                 if (param1.equals("assertElementPresent")) {
278                     sb.append("assertTrue");
279                 }
280                 else if (param1.equals("assertElementNotPresent")) {
281                     sb.append("assertFalse");
282                 }
283 
284                 sb.append("(selenium.isElementPresent(\"");
285                 sb.append(param2);
286                 sb.append("\"));");
287             }
288             else if (param1.equals("assertSelectOptions")) {
289                 String[] expectedArray = StringUtil.split(param3);
290 
291                 sb.append("String[] actualArray = ");
292                 sb.append("selenium.getSelectOptions(\"");
293                 sb.append(param2);
294                 sb.append("\");");
295 
296                 sb.append("assertEquals(");
297                 sb.append(expectedArray.length);
298                 sb.append(", actualArray.length);");
299 
300                 for (int i = 0; i < expectedArray.length; i++) {
301                     sb.append("assertEquals(\"");
302                     sb.append(expectedArray[i]);
303                     sb.append("\", actualArray[");
304                     sb.append(i);
305                     sb.append("]);");
306                 }
307             }
308             else if (param1.equals("assertTextPresent") ||
309                      param1.equals("assertTextNotPresent")) {
310 
311                 if (param1.equals("assertTextPresent")) {
312                     sb.append("assertTrue");
313                 }
314                 else if (param1.equals("assertTextNotPresent")) {
315                     sb.append("assertFalse");
316                 }
317 
318                 sb.append("(selenium.isTextPresent(\"");
319                 sb.append(param2);
320                 sb.append("\"));");
321             }
322             else if (param1.equals("captureEntirePageScreenshot")) {
323                 int pos = param2.lastIndexOf("\\");
324 
325                 String dirName = param2.substring(0, pos + 1);
326 
327                 sb.append("FileUtil.mkdirs(RuntimeVariables.replace(\"");
328                 sb.append(dirName);
329                 sb.append("\"));");
330                 sb.append("selenium.captureEntirePageScreenshot(");
331                 sb.append("RuntimeVariables.replace(\"");
332                 sb.append(param2);
333                 sb.append("\"), \"\");");
334             }
335             else if (param1.equals("click") || param1.equals("mouseDown") ||
336                      param1.equals("mouseUp") || param1.equals("open") ||
337                      param1.equals("selectFrame") ||
338                      param1.equals("selectWindow")) {
339 
340                 sb.append("selenium.");
341                 sb.append(param1);
342                 sb.append("(\"");
343                 sb.append(param2);
344                 sb.append("\");");
345             }
346             else if (param1.equals("clickAndWait")) {
347                 sb.append("selenium.click(RuntimeVariables.replace(\"");
348                 sb.append(param2);
349                 sb.append("\"));");
350                 sb.append("selenium.waitForPageToLoad(\"30000\");");
351             }
352             else if (param1.equals("close")) {
353                 sb.append("selenium.");
354                 sb.append(param1);
355                 sb.append("();");
356             }
357             else if (param1.equals("gotoIf")) {
358                 String conditional = StringUtil.replace(
359                     param2, new String[] {"${", "}"}, new String[] {"", ""});
360 
361                 sb.append("if (");
362                 sb.append(conditional);
363                 sb.append(") {");
364                 sb.append("label =");
365                 sb.append(labels.get(param3));
366                 sb.append(";");
367                 sb.append("continue;");
368                 sb.append("}");
369             }
370             else if (param1.equals("label")) {
371                 String label = labels.get(param2);
372 
373                 sb.append("case ");
374                 sb.append(label);
375                 sb.append(":");
376             }
377             else if (param1.equals("pause")) {
378                 sb.append("Thread.sleep(");
379                 sb.append(param2);
380                 sb.append(");");
381             }
382             else if (param1.equals("selectAndWait")) {
383                 sb.append("selenium.select(\"");
384                 sb.append(param2);
385                 sb.append("\", \"");
386                 sb.append(param3);
387                 sb.append("\");");
388                 sb.append("selenium.waitForPageToLoad(\"30000\");");
389             }
390             else if (param1.equals("store")) {
391                 sb.append("boolean ");
392                 sb.append(param3);
393                 sb.append(" = ");
394 
395                 if (param2.startsWith("eval(")) {
396                     String eval = param2.substring(5, param2.length() - 1);
397 
398                     eval = StringUtil.replace(eval, "'", "\"");
399 
400                     sb.append(eval);
401                 }
402 
403                 sb.append(";");
404             }
405             else if (param1.equals("storeText")) {
406                 sb.append("String ");
407                 sb.append(param3);
408                 sb.append(" = selenium.getText(\"");
409                 sb.append(param2);
410                 sb.append("\");");
411 
412                 sb.append("RuntimeVariables.setValue(\"");
413                 sb.append(param3);
414                 sb.append("\", ");
415                 sb.append(param3);
416                 sb.append(");");
417             }
418             else if (param1.equals("verifyElementPresent") ||
419                      param1.equals("verifyElementNotPresent")) {
420 
421                 if (param1.equals("verifyElementPresent")) {
422                     sb.append("verifyTrue");
423                 }
424                 else if (param1.equals("verifyElementNotPresent")) {
425                     sb.append("verifyFalse");
426                 }
427 
428                 sb.append("(selenium.isElementPresent(\"");
429                 sb.append(param2);
430                 sb.append("\"));");
431             }
432             else if (param1.equals("verifyTextPresent") ||
433                      param1.equals("verifyTextNotPresent")) {
434 
435                 if (param1.equals("verifyTextPresent")) {
436                     sb.append("verifyTrue");
437                 }
438                 else if (param1.equals("verifyTextNotPresent")) {
439                     sb.append("verifyFalse");
440                 }
441 
442                 sb.append("(selenium.isTextPresent(\"");
443                 sb.append(param2);
444                 sb.append("\"));");
445             }
446             else if (param1.equals("verifyTitle")) {
447                 sb.append("verifyEquals(\"");
448                 sb.append(param2);
449                 sb.append("\", selenium.getTitle());");
450             }
451             else if (param1.equals("waitForElementNotPresent") ||
452                      param1.equals("waitForElementPresent") ||
453                      param1.equals("waitForTextNotPresent") ||
454                      param1.equals("waitForTextPresent")) {
455 
456                 sb.append("for (int second = 0;; second++) {");
457                 sb.append("if (second >= 60) {");
458                 sb.append("fail(\"timeout\");");
459                 sb.append("}");
460 
461                 sb.append("try {");
462                 sb.append("if (");
463 
464                 if (param1.equals("waitForElementNotPresent") ||
465                     param1.equals("waitForTextNotPresent")) {
466 
467                     sb.append("!");
468                 }
469 
470                 sb.append("selenium.");
471 
472                 if (param1.equals("waitForElementNotPresent") ||
473                     param1.equals("waitForElementPresent")) {
474 
475                     sb.append("isElementPresent");
476                 }
477                 else if (param1.equals("waitForTextNotPresent") ||
478                          param1.equals("waitForTextPresent")) {
479 
480                     sb.append("isTextPresent");
481                 }
482 
483                 sb.append("(\"");
484                 sb.append(param2);
485                 sb.append("\")) {");
486                 sb.append("break;");
487                 sb.append("}");
488                 sb.append("}");
489                 sb.append("catch (Exception e) {");
490                 sb.append("}");
491 
492                 sb.append("Thread.sleep(1000);");
493                 sb.append("}");
494             }
495             else if (param1.equals("waitForTable")) {
496                 sb.append("for (int second = 0;; second++) {");
497                 sb.append("if (second >= 60) {");
498                 sb.append("fail(\"timeout\");");
499                 sb.append("}");
500 
501                 sb.append("try {");
502                 sb.append("if (StringPool.BLANK.equals(selenium.getTable(\"");
503                 sb.append(param2);
504                 sb.append("\"))) {");
505                 sb.append("break;");
506                 sb.append("}");
507                 sb.append("}");
508                 sb.append("catch (Exception e) {");
509                 sb.append("}");
510 
511                 sb.append("Thread.sleep(1000);");
512                 sb.append("}");
513             }
514             else {
515                 System.out.println(param1 + " was not translated");
516             }
517         }
518 
519         if (labels.size() > 0) {
520             sb.append("case 100:");
521             sb.append("label = -1;");
522             sb.append("}");
523             sb.append("}");
524         }
525 
526         sb.append("}");
527         sb.append("}");
528 
529         String content = sb.toString();
530 
531         ServiceBuilder.writeFile(new File(testFileName), content);
532     }
533 
534     private static final String[] _FIX_PARAM_OLD_SUBS = new String[] {
535         "\\\\n", "<br />"
536     };
537 
538     private static final String[] _FIX_PARAM_NEW_SUBS = new String[] {
539         "\\n", "\\n"
540     };
541 
542 }