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.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  /**
37   * <a href="SeleneseToJavaBuilder.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
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              // I would have preferred to use XlateHtmlSeleneseToJava, but it
66              // is horribly out of sync with Selenium IDE and generates incorrect
67              // code.
68  
69              /*File file = new File(basedir + "/" + files[i]);
70  
71              String input = StringUtil.replace(file.toString(), "\\", "/");
72  
73              XlateHtmlSeleneseToJava.main(
74                  new String[] {
75                      "test", "-silent", input
76                  }
77              );*/
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("captureEntirePageScreenshot")) {
214                 sb.append("selenium.captureEntirePageScreenshot(\"");
215                 sb.append(param2);
216                 sb.append("\", \"\");");
217             }
218             else if (param1.equals("click") || param1.equals("mouseDown") ||
219                      param1.equals("mouseUp") || param1.equals("open") ||
220                      param1.equals("selectFrame") ||
221                      param1.equals("selectWindow")) {
222 
223                 sb.append("selenium.");
224                 sb.append(param1);
225                 sb.append("(\"");
226                 sb.append(param2);
227                 sb.append("\");");
228             }
229             else if (param1.equals("clickAndWait")) {
230                 sb.append("selenium.click(\"");
231                 sb.append(param2);
232                 sb.append("\");");
233                 sb.append("selenium.waitForPageToLoad(\"30000\");");
234             }
235             else if (param1.equals("close")) {
236                 sb.append("selenium.");
237                 sb.append(param1);
238                 sb.append("();");
239             }
240             else if (param1.equals("pause")) {
241                 sb.append("Thread.sleep(");
242                 sb.append(param2);
243                 sb.append(");");
244             }
245             else if (param1.equals("addSelection") || param1.equals("select") ||
246                      param1.equals("type") || param1.equals("typeKeys") ||
247                      param1.equals("waitForPopUp")) {
248 
249                 sb.append("selenium.");
250                 sb.append(param1);
251                 sb.append("(\"");
252                 sb.append(param2);
253                 sb.append("\", RuntimeVariables.replace(\"");
254                 sb.append(param3);
255                 sb.append("\"));");
256             }
257             else if (param1.equals("selectAndWait")) {
258                 sb.append("selenium.select(\"");
259                 sb.append(param2);
260                 sb.append("\", \"");
261                 sb.append(param3);
262                 sb.append("\");");
263                 sb.append("selenium.waitForPageToLoad(\"30000\");");
264             }
265             else if (param1.equals("storeText")) {
266                 sb.append("String ");
267                 sb.append(param3);
268                 sb.append(" = selenium.getText(\"");
269                 sb.append(param2);
270                 sb.append("\");");
271 
272                 sb.append("RuntimeVariables.setValue(\"");
273                 sb.append(param3);
274                 sb.append("\", ");
275                 sb.append(param3);
276                 sb.append(");");
277             }
278             else if (param1.equals("verifyElementPresent") ||
279                      param1.equals("verifyElementNotPresent")) {
280 
281                 if (param1.equals("verifyElementPresent")) {
282                     sb.append("verifyTrue");
283                 }
284                 else if (param1.equals("verifyElementNotPresent")) {
285                     sb.append("verifyFalse");
286                 }
287 
288                 sb.append("(selenium.isElementPresent(\"");
289                 sb.append(param2);
290                 sb.append("\"));");
291             }
292             else if (param1.equals("verifyTextPresent") ||
293                      param1.equals("verifyTextNotPresent")) {
294 
295                 if (param1.equals("verifyTextPresent")) {
296                     sb.append("verifyTrue");
297                 }
298                 else if (param1.equals("verifyTextNotPresent")) {
299                     sb.append("verifyFalse");
300                 }
301 
302                 sb.append("(selenium.isTextPresent(\"");
303                 sb.append(param2);
304                 sb.append("\"));");
305             }
306             else if (param1.equals("verifyTitle")) {
307                 sb.append("verifyEquals(\"");
308                 sb.append(param2);
309                 sb.append("\", selenium.getTitle());");
310             }
311             else if (param1.equals("waitForElementNotPresent") ||
312                      param1.equals("waitForElementPresent") ||
313                      param1.equals("waitForTextNotPresent") ||
314                      param1.equals("waitForTextPresent")) {
315 
316                 sb.append("for (int second = 0;; second++) {");
317                 sb.append("if (second >= 60) {");
318                 sb.append("fail(\"timeout\");");
319                 sb.append("}");
320 
321                 sb.append("try {");
322                 sb.append("if (");
323 
324                 if (param1.equals("waitForElementNotPresent") ||
325                     param1.equals("waitForTextNotPresent")) {
326 
327                     sb.append("!");
328                 }
329 
330                 sb.append("selenium.");
331 
332                 if (param1.equals("waitForElementNotPresent") ||
333                     param1.equals("waitForElementPresent")) {
334 
335                     sb.append("isElementPresent");
336                 }
337                 else if (param1.equals("waitForTextNotPresent") ||
338                          param1.equals("waitForTextPresent")) {
339 
340                     sb.append("isTextPresent");
341                 }
342 
343                 sb.append("(\"");
344                 sb.append(param2);
345                 sb.append("\")) {");
346                 sb.append("break;");
347                 sb.append("}");
348                 sb.append("}");
349                 sb.append("catch (Exception e) {");
350                 sb.append("}");
351 
352                 sb.append("Thread.sleep(1000);");
353                 sb.append("}");
354             }
355             else if (param1.equals("waitForTable")) {
356                 sb.append("for (int second = 0;; second++) {");
357                 sb.append("if (second >= 60) {");
358                 sb.append("fail(\"timeout\");");
359                 sb.append("}");
360 
361                 sb.append("try {");
362                 sb.append("if (StringPool.BLANK.equals(selenium.getTable(\"");
363                 sb.append(param2);
364                 sb.append("\"))) {");
365                 sb.append("break;");
366                 sb.append("}");
367                 sb.append("}");
368                 sb.append("catch (Exception e) {");
369                 sb.append("}");
370 
371                 sb.append("Thread.sleep(1000);");
372                 sb.append("}");
373             }
374             else {
375                 System.out.println(param1 + " was not translated");
376             }
377         }
378 
379         sb.append("}");
380         sb.append("}");
381 
382         String content = sb.toString();
383 
384         ServiceBuilder.writeFile(new File(testFileName), content);
385     }
386 
387     private static final String[] _FIX_PARAM_OLD_SUBS = new String[] {
388         "\\\\n", "<br />"
389     };
390 
391     private static final String[] _FIX_PARAM_NEW_SUBS = new String[] {
392         "\\n", "\\n"
393     };
394 
395     private static FileImpl _fileUtil = FileImpl.getInstance();
396 
397 }