RCPTT是一款基于Eclipse的自动化测试工具,其自身已经包含了很多的脚本命令,但基本都是与UI(基于swt图形库的UI)操作相关的命令,对于一些特定复杂的业务逻辑就显得有些力所不及,所幸的是RCPTT提供了扩展脚本命令的包,这样我们就可以实现自己想要的脚本命令了。点击下载本例子的源码
第一步:下载RCPTT的源码包http://git.eclipse.org/c/rcptt/org.eclipse.rcptt.git/,下载完毕后解压找到org.eclipse.rcptt.ecl.core这个插件,你也可以点我只下载这个插件工程,因为自定义命令只依赖这个插件,在eclipse中导入这个插件工程

第二步:新建一个插件工程com.test.rcptt.ecl(当然名字你可以随便取啦)

新建一个model文件夹,然后创建一个Ecore model文件,取名test吧,(图中标红线只是提示读者,无其他意义)

在test.core界面点击右键然后Load resource,把org.eclipse.rcptt.ecl.core中的ecl.ecore加载进来。然后新建一个包EclTestPackage,在该包下创建一个TestCmd类(ESuper Type=Command)和Student类(ESuper Type=EObject),保存test.ecore

右键test.ecore新建EMF Generator Model

打开test.genmodel,编辑EclTestPackage的Base Package为com.test.rcptt.ecl.model,右键单击EclTestPackage,生成Model code,如果生成的java代码中有错误,把错误的代码注释掉就行

第三步:在plugin.xml中扩展org.eclipse.rcptt.ecl.core.scriptlet,并且添加一个ecl命令,其中namespace必须要和.ecore中的Ns URI一样,这样RCPTT的脚本解释器就可以找到前面自定义的TestCmd这个命令类。新建命令服务类com.test.rcptt.ecl.service.TestCommandService,该类必须实现ICommandService接口,当运行test-cmd这个命令时,该类中的service方法就会被调用

第四步:在service方法中实现自己的逻辑
package com.test.rcptt.ecl.service;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.rcptt.ecl.core.Command;
import org.eclipse.rcptt.ecl.runtime.ICommandService;
import org.eclipse.rcptt.ecl.runtime.IProcess;
import com.test.rcptt.ecl.model.EclTestPackage.Student;
import com.test.rcptt.ecl.model.EclTestPackage.TestCmd;
import com.test.rcptt.ecl.model.EclTestPackage.impl.EclTestPackageFactoryImpl;
public class TestCommandService implements ICommandService {
@Override
public IStatus service(Command command, IProcess context) throws InterruptedException, CoreException {
TestCmd cmd = (TestCmd) command;
String info = cmd.getArgv(); // 获取命令的参数
String infos[] = info.split(",");
if (infos.length != 2) {
return Status.CANCEL_STATUS;
} else {
Student s = EclTestPackageFactoryImpl.init().createStudent();
s.setName(infos[0]);
s.setAge(Integer.parseInt(infos[1]));
context.getOutput().write(s); // 返回一个student对象
return Status.OK_STATUS;
}
}
}
第五步:导出com.test.rcptt.ecl插件,可以导成可安装的插件格式,也可以直接导出jar包格式(这里以jar包格式为例),将导出的jar包放到被测试的eclipse或RCP的plugin目录下,然后就可以使用了

PS:这就是一个简单的自定义命令的实现过程,如果文中有错误或不妥之处,还请联系博主
本文介绍如何通过扩展RCPTT的EclTestPackage来自定义命令,实现特定业务逻辑的自动化测试。
2745

被折叠的 条评论
为什么被折叠?



