スクリプト言語はGUIで完全にサポートされていますが、Javaプログラミング言語のような柔軟性はないため、簡単なタスクや高レベルのアクションには通常のスクリプトを使用し、通常の言語で十分でない場合や非効率な場合にのみJavaを使用するのが理にかなっていることがよくあります。このようなアプローチをサポートするために、T-Plan Robot EnterpriseにはJavaコードを呼び出すための2つのメカニズムが用意されています:
どちらのメソッドも、共有コンテキストに格納された名前付き変数を通じて、 通常のスクリプトとJavaコードの間でパラメーターの転送をサポートしている。この仕組みは、以下の例で示されています。Javaテストクラスは、ロボットのGUIに認識させるパラメータとその型を宣言することもできます。詳細はDefaultJavaTestScriptクラスのドキュメントを参照してください。
あるファイルが存在するかどうかを調べる簡単な例を考えてみましょう。ファイルが存在しない場合、スクリプトは終了コード 1 で終了するはずです。以下の例では、両方のメカニズムを使ってこのテストを実装する方法を示します。パラメータ転送を示すために、ファイル名をパラメータとして渡し、その結果("true "または "false")を通常のスクリプトに公開します。
1.Runコマンドを使った例:
filetest.tpr (Alternative #1)
| FileTest.java
|
---|
JS
#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
}
|
JS
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)
|
---|
JS
# 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.Java コードブロックを埋め込んだ例
filetest.tpr
|
---|
JS
# 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
}
|