You are here: Chapter 3: Introduction to Test Scripts > Interoperability of Test Scripts and Java Code

Interoperability of Test Scripts and Java Code

This topic deals with T-Plan Robot Enterprise features and it is not applicable to the legacy VNCRobot Version 1.3 or 2.0 projects.

As the scripting language is fully supported by the GUI but doesn't have the flexibility of the Java programming language, it often makes sense to use ordinary script for simpler tasks or high level actions and employ Java only where the ordinary language doesn't suffice or is inefficient. To support this approach T-Plan Robot Enterprise provides two mechanisms allowing to call Java code:

Both methods support transfer of parameters between the ordinary script and Java code through named variables stored in the shared context. This mechanism is demonstrated on the example below. Java test classes may also declare the parameters and their types which make the Robot's GUI recognize them. For details see the DefaultJavaTestScript class documentation and pictures in the v2.2 change log.

Let's consider a simple example verifying whether a particular file exists or not. If the file doesn't exist, the script should exit with the exit code of 1. The following examples show how to implement this test using both mechanisms. To demonstrate the parameter transfer the file name will be passed as parameter and the result ("true" or "false") will be exposed to the ordinary script.


1. Example with the Run command:

filetest.tpr (Alternative #1)
 
 
 
FileTest.java
#Alternative #1 - run the .java source code file.
# This requires Robot to run on JDK. Since the tool
# has to compile the code before execution,
# performance can be slower than the other mothods.
Run C:\testsuite\FileTest.java file=
C:\testsuite\data.txt

# If the "exists" variable is not "true", exit the script.
if ("{exists}" != "true") {
  Exit 1
}



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

/**
* Test class verifying existence of a file.
* The class relies on the "file" named variable stored in the context
* and exports the result as either "true" or "false" to the variable
* called "exists".
*
* (C) T-Plan Ltd, http://www.t-plan.com
*/
public class FileTest extends DefaultJavaTestScript {
    public void test() {
       try {
String file = getContext().getVariableAsString("file");
File f = new File(file);
getContext().setVariable("exists", f.exists());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
filetest.tpr (Alternative #2)


# Alternative #2 - instantiate the class by name.
# This example presumes that the test class was compiled
# into C:\testsuite\FileTest.jar and this path is imported
# using the Include command. Another alternative is to
# skip the Include command and
put the JAR file
# onto the Robot's Java class path.
# This way is very fast and allows to create
# libraries of Java routines.
Include C:\testsuite\FileTest.jar
Run FileTest file=C:\testsuite\data.txt

# If the "exists" variable is not "true", exit the script.
if ("{exists}" != "true") {
  Exit 1
}




2. Example with embedded Java code block:

filetest.tpr
# The file to test existence of.
Var file=C:\testsuite\data.txt

# The Java code block calls Java code directly.
# The block is in fact internally converted to a Java test
# class which is compiled and executed.

# This requires Robot to run on JDK. Since the tool
# has to compile the code before execution,
# this way is very slow. Note that the "import" clause
# is supported only by v2.2 or higher and
# v2.1 needs to use fully qualified class name instead.
java {
   import java.io.File;

  String file = getContext().getVariableAsString("file");
  File f = newFile(file);
  getContext().setVariable("exists", f.exists());
} endjava

# If the "exists" variable is not "true", exit the script.
if ("{exists}" != "true") {
  Exit 1
}

 

12 December 2014

Copyright © T-Plan Ltd.

Version 1.0