You are here: Chapter 3: Introduction to Test Scripts > Conversion to Java

Conversion to Java

Test scripts written in the proprietary language may be easily converted to Java using the Java converter in the T-Plan Robot Enterprise GUI. This conversion is one way only and Java test scripts can not be converted back to the proprietary scripting language.

The converter is designed to help with migration of existing test scripts to Java. It is a logical step in the automation process: people usually start with the scripting language and when it ceases to meet their needs, they convert to Java. As we are maintaining a strict language compatibility with the previous product versions, the conversion process may be applied to any test script designed with legacy VNCRobot Version 1.3 or 2.0 projects. The converter should produce a functional (though not neat) Java code even where the T-Plan Robot Enterprise scripting engine was enhanced with custom commands through the standard plug in interface.

Conversion also plays an important role in enabling the Record & Replay feature with Java test scripts. Though direct recording to Java is not supported, you may record your desktop interaction to a standard test script first and then take advantage of the converter to generate the corresponding Java code.


Be aware that the converter is not perfect and it may fail to convert certain custom expressions, especially where abundant variables are applied, and manual correction of the resulting Java code may be required. You should always review the Java code to make sure that it corresponds to the original script and run it against a test environment to verify its behaviour. The list of known converter limitations is available in the DefaultJavaTestScriptConverter class documentation.

For example, to convert the calculator script follow these steps:

  1. Copy & paste the script code from the calculator.tpr example below (or from its original topic) into the T-Plan Robot Enterprise script editor.
  2. Select File->Export to Java in the menu or press Ctrl+J in the editor.
  3. Fill in "Calculator" for class name in the Java Preferences window and hit OK.
  4. A new editor with the Java code similar to what you see below must open. You may eventually execute it to make sure it behaves the same way as the original one.

Example test script as well as a conversion video follow:

calculator.tpr (Windows version)
 
Calculator.java

# Generic procedure to start an application on Windows.
# We take advantage of the Windows+r key to open the Run box.
procedure startApp {
  Press Windows+r wait=3s
  Typeline "{1}" wait=4s
}

# Start calculator, type "5+5" followed by Enter
# and take a screen shot.
startApp calc
Typeline "5+5" wait=2s
Screenshot calculator_result.jpg
 desc="Result of 5+5"

------------------------->


Conversion to Java (video)


/**
* Generated on Sun Dec 06 11:23:28 CET 2009
* T-Plan Robot Enterprise v2.0.1 (Build No. 2.0.1-20091126.1)
* Default Java Converter version 2.0.1
*/
import com.tplan.robot.scripting.DefaultJavaTestScript;
import com.tplan.robot.scripting.JavaTestScript;
import java.io.File;
import java.io.IOException;

public class Calculator extends DefaultJavaTestScript implements JavaTestScript {
   public void test() {
      try {
         // Start calculator, type "5+5" followed by Enter and take a screenshot.
startApp("startApp", "calc");
typeLine("5+5", "2s");
screenshot(new File("calculator_result.jpg"), "Result of 5+5");
} catch (IOException ex) {
ex.printStackTrace();
}
}

// Generic procedure to start an application on Windows.
// We take advantage of the Windows+r key to open the Run box.
private int startApp(String... args) throws IOException {
press("Windows+r", "3s");
typeLine(""+args[1], "4s");
// Return exit code of the last executed command
return getContext().getExitCode();
}
}

 

12 December 2014

Copyright © T-Plan Ltd.

Version 1.0