我在使用 注释库 为Java选取自定义关键字时遇到问题。我面临的问题是,使用 jybot 执行时出现以下错误:
导入库“org.robotframework.javalib.library.ClassPathLibrary”不包含关键字
导入库“org.robotframework.javalib”。 library.AnnotationLibrary' 不包含关键字
未找到名称为“创建空堆栈”的关键字 使用的 Jybot 命令:jybot example.txt。下面是实际的测试用例以及为此测试用例调用的库:
*** Settings ***
Library org.robotframework.javalib.library.ClassPathLibrary org/robotframework/example/keyword/**.class
Library org.robotframework.javalib.library.AnnotationLibrary org/robotframework/example/keyword/**.class
*** Test Cases ***
Stack Example
Create an Empty Stack
Add an Element Java
Add an Element C++
Remove last Element
The last Element should be
下面是为合并关键字而编写的库,为合并 Stack 关键字而编写:
package org.robotframework.example.keyword;
import org.robotframework.javalib.annotation.RobotKeyword;
import org.robotframework.javalib.annotation.RobotKeywords;
import org.robotframework.javalib.library.AnnotationLibrary;
/**
*
*/
/**
*
*/
@RobotKeywords
public class StackKeyword extends AnnotationLibrary {
/** 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";
public StackKeyword() {
super("org/robotframework/example/keyword/**/*.class");
}
//</editor-fold>
/** The Functionality to be tested */
private java.util.Stack<String> testStack;
/**
* Keyword-method to create an empty stack.
*/
@RobotKeyword
public void createAnEmptyStack() {
testStack = new java.util.Stack<String>();
}
/**
* Keyword-method to add an element to the stack.
* @param element The element
*/
@RobotKeyword
public void addAnElement(String element) {
testStack.push(element);
}
/**
* Keyword-method to remove the last element from the stack.
*/
@RobotKeyword
public void removeLastElement() {
testStack.pop();
}
/**
* Keyword-method to search for an element position.
* @param element The element
* @param pos The expected position
*/
@RobotKeyword
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
*/
@RobotKeyword
public void theLastElementShouldBe(String result) throws Exception {
String element = testStack.pop();
if (!result.equals(element)) {
throw new Exception("Wrong element: " + element);
}
}
}
你的问题在于,你在 Robot Framework 测试文件中导入了
StackKeyword
类本身,而
AnnotationLibrary
需要导入的是包含已注释关键字的
包
。
修改你的 Robot Framework 测试文件,像这样导入库:
*** Settings ***
Library org.robotframework.javalib.library.ClassPathLibrary org/robotframework/example/keyword/**/*.class
Library org.robotframework.javalib.library.AnnotationLibrary org.robotframework.example.keyword
注意,我们在第二个
Library
导入中使用了
org.robotframework.example.keyword
,而不是
org.robotframework.example.keyword.StackKeyword
。这会告诉
AnnotationLibrary
在
org.robotframework.example.keyword
包中查找已注释的关键字,它会在
StackKeyword
类中找到它们。
进行此更改后,你的关键字应该可以被 Robot Framework 识别,并且测试应该能够运行而不会出现错误。
标签:python,java,robotframework From: 31598517