使用以代码为中心的博客:Robot Framework Tutorial – Writing Keyword Libraries in Java 作为基础,其中包含一些针对 RED 而不是 RIDE 的特定步骤。本演练将允许您设置 Jython,在 Java 中创建一个简单的库并从机器人脚本运行它。
在 Eclipse 中安装 Eclipse (NEON) 和 RED Feature 后,在 Eclipse 中创建一个新的 Java 项目。完成后,继续创建一个具有以下内容的新 Java 类。
package org.robot.sample.keywords;
import java.util.Stack;
/**
* This is an example for a Keyword Library for the Robot Framework.
* @author thomas.jaspers
*/
public class SampleKeywordLibrary {
/** This means the same instance of this class is used throughout
* the lifecycle of a Robot Framework test execution.
*/
public static final String ROBOT_LIBRARY_SCOPE = "GLOBAL";
//</editor-fold>
/** The Functionality to be tested */
private Stack<String> testStack;
/**
* Keyword-method to create an empty stack.
*/
public void createAnEmptyStack() {
testStack = new Stack<String>();
}
/**
* Keyword-method to add an element to the stack.
* @param element The element
*/
public void addAnElement(String element) {
testStack.push(element);
}
/**
* Keyword-method to remove the last element from the stack.
*/
public void removeLastElement() {
testStack.pop();
}
/**
* Keyword-method to search for an element position.
* @param element The element
* @param pos The expected position
*/
public void elementShouldBeAtPosition(String element, int pos)
throws Exception {
if (testStack.search(element) != pos) {
throw new Exception("Wrong position: " + testStack.search(element));
}
}
/**
* Keyword-method to check the last element in the stack.
* @param result Expected resulting element
*/
public void theLastElementShouldBe(String result) throws Exception {
String element = testStack.pop();
if (!result.equals(element)) {
throw new Exception("Wrong element: " + element);
}
}
}
请确保您已使用 Windows Installer 安装了 Jython。在我的示例中,Jython 安装在 c:\Jython 中。与常规的 Python 解释器机器人框架一样,仍然需要安装。假设您的机器可以访问互联网,在命令行中转到c:\Jython\bin\ 并运行命令pip install robotframework。这将在 Jython 环境中安装 Robot Framework。
现在在 Eclipse 中创建一个新的 Robot Framework 项目。请确保您拥有Window > Perspective > Open Perspective > Robot 或Other > Robot。
在项目中,默认的机器人框架是基于 Python 的,我们需要配置 Jython 解释器。在 Eclipse 中转到Window > Preferences,然后从树形菜单中选择Robot Framework > Installed Frameworks。单击Add 并指向c:\Jython\bin\。点击确定。
从机器人项目中打开 Red.XML 并转到 general 选项卡。这是项目解释器设置的地方。如果它设置为 Python(如下例所示),则单击 use local settings for this project 并检查 Jython 解释器。将设置保存到文件 (CTRL-S)。
通过 Robot Framework 项目设置,是时候将 Java 类导出到 Jar 文件了。右键单击类文件并单击export。然后选择JAR file 并单击next。单击Browse 并设置JAR 文件的位置和文件名。在这种情况下,我选择了ExampleLibrary.jar 和我的机器人项目的文件夹。按Finish完成导出。
返回 Red.XML 并单击 Referenced Libraries,然后继续单击 Add Java library,选择导出的 Jar 文件 (ExampleLibrary.jar) 并按 OK。这将继续加载 jar 并从 Jar 文件中读取关键字。保存文件 (CTRL-S)。这应该导致以下参考。
现在是时候创建一个机器人文件并使用该库了。在引用的博客中,给出了使用 java 函数/关键字的以下示例脚本。
*** Settings ***
Library org.robot.sample.keywords.SampleKeywordLibrary
*** Test Cases ***
ExampleJava
Create An Empty Stack
Add An Element Java
Add An Element C++
Remove Last Element
The Last Element Should Be Java
对于已加载的库,不应出现红线,否则请右键单击该库并单击 quick-fix 并自动发现该库。
然后使用常规的 Eclipse/RED Run 菜单运行脚本。这将成功运行脚本并输出以下内容:
Command: C:\jython2.7.0\bin\jython.exe -J-Dpython.path=C:\jython2.7.0\Lib\site-packages -J-cp .;C:\Eclipse\Workspace\ExamplJava\ExampleLibrary.jar -m robot.run --listener C:\Users\User\AppData\Local\Temp\RobotTempDir8926065561484828569\TestRunnerAgent.py:57292:False -s ExamplJava.ExampleJava C:\Eclipse\Workspace\ExamplJava
Suite Executor: Robot Framework 3.0.2 (Jython 2.7.0 on java1.8.0_60)
==============================================================================
ExamplJava
==============================================================================
ExamplJava.ExampleJava
==============================================================================
ExampleJava | PASS |
------------------------------------------------------------------------------
ExamplJava.ExampleJava | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
ExamplJava | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Output: C:\Eclipse\Workspace\ExamplJava\output.xml
Log: C:\Eclipse\Workspace\ExamplJava\log.html
Report: C:\Eclipse\Workspace\ExamplJava\report.html