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