Java Code Blocks

2.12 Java Code Blocks

Java code blocks allow calling Java code directly from TPR scripts. It was designed to support cases when it is desired to stick to the scripting language, but a certain custom functionality is needed.

To make Java code blocks work properly, make sure to run Robot on a JDK through the "java -classpath <libs> com.tplan.robot.ApplicationSupport" command rather than the "java -jar robot.jar" one. The latter syntax fails to populate the 'CLASSPATH' for the Java compiler on some environments which results in failures to compile and execute Java source code. See the startup chapter of the Release Notes document for more information.

The general syntax of a Java code block follows. Be aware that support of the import clauses was delivered in v2.2. Prior versions must reference classes by fully qualified name or add the imports to the test class template managed in preferences.

java {
  <import clauses>
  <Java code>
} endjava

Each such a block is internally converted to a Java script extending DefaultJavaTestScript class and the Java code inside the block is inserted into its test() method. The class template is exposed in the Java code block configuration and may be customized through the Preferences window. The following example shows an example:

Java Code Block

Resulting Java Script Class

java {
  import java.util.Date;
  System.out.println("Current date: "+new Date());
endjava

import com.tplan.robot.scripting.*;
import java.io.*;
import java.util.*;
import java.util.Date;

public class SomeDynamicName extends DefaultJavaTestScript {
public void test() {
System.out.println("Current date: "+new Date());
}
}

This mechanism has a few practical impacts:

  • As Java code blocks require access to Java compiler, T-Plan Robot must run on Java Development Kit (JDK) or it must be configured with a valid JDK installation path (v2.3.3 and newer). If the tool runs just on Java Runtime Environment (JRE) and no JDK is available the compiler will report an error. Refer to chapter 1 of the Release Notes document for instructions.
  • Avoid using too many Java code blocks in your script. Be aware that each one must be dynamically compiled in the memory before use and it may degrade performance of your script and the whole tool. Should you want to create reusable parametrized pieces of precompiled Java code, explore the run command functionality.
  • The template by default imports all classes from the com.tplan.robot.scripting, java.io and java.util packages. If you want to use a class from another package, place the import clause before the Java code. Other alternatives are to either reference the class by fully qualified name (such as for example java.awt.Point) or go to the Java code block preferences and add the import to the class template. If you choose the template way, make sure to reapply it when the script is migrated to another machine.
  • As each Java code block behaves as a standalone Java class, local variables and objects created in one block are not visible in another one. If you need to share an object across multiple Java blocks, store them into the context. It is basically a map which exists for the whole time of script compilation or execution and holds references to important automation framework objects. To store an object to the context use getContext().put(), to retrieve an object use getContext(). get(). Make sure to use a unique key so that you don't overwrite reference of an already existing framework object. For more information see the ScriptingContext interface specification.
  • Java code blocks may share global variables with the TPR script through the context's getContext().getVariable() and getContext().setVariable() methods. As the code block extends DefaultJavaTestScript class, it may also call its methods corresponding to the scripting language commands.

The following example illustrates sharing of variables. The script first sets the template path to "C:/templates". The Java code retrieves the path through the context, lists all PNG files in the directory and stores them as numbered variables called FILE<n> together with the file counter FILECNT to the context variables. When the TPR script resumes, it iterates over the listed files and performs image comparison against each PNG file.

Var _TEMPLATE_DIR="C:\templates"

# This line declares dummy values of variables populated by the Java code.
# It prevents the compiler from reporting error in the for() loop and CompareTo command.
Var FILECNT=0 FILE1=dummy.png

java {
         File files[] = getContext().getTemplateDir().listFiles();
         int i = 0;
         for (File file : files) {
             if (file.isFile() && file.getName().endsWith(".png")) {
                 i++;
                 getContext().setVariable("FILE"+i, file.getName());
             }
         }
         getContext().setVariable("FILECNT", Integer.toString(i));
} endjava

for (i=1; {i}<{FILECNT}+1; i={i}+1) {
  Compareto "{FILE{i}}" method=search
}