我们在用testlink进行用例管理的时候,发现用例不能用excel进入导入,只支持xml文件格式的导入,这对我们写用例来说是不太方便的。
我自己开发了一个testlink的excel用例转xml文件的小工具,便于在excel中写用例,然后导入到testlink中。
工具开发思路:
采用Javabean的方式来映射用例里的项。
读取excel中的内容,将内容映射到javabean中,放置到内存当中。
从内存中读取javabean,将其写入到xml文件中。
具体代码如下:
封装用例的javabean:
@Getter
@Setter
@ToString
public class CaseBean {
/**
* 用例编号
* 该要素不是必须,是为了在写用例的时候好统计,导入后用例编号是系统自动生成的。
*/
public String caseNo;
/**
* 用例名称
*
*/
public String caseName;
/**
* 摘要
*/
private String summary;
/**
* 前提
*/
private String preconditions;
/**
* 执行方式
*/
private String execution_type;
/**
* 重要性
*/
private String importance;
/**
* 步骤动作
*/
public String actions;
/**
* 期望的结果
*/
public String expectedresults;
}
从excel中读取case封装到javabean中:
public class ExcelReaderUtil {
/**
* @methodName: readExcel 读取excel工具类
* @param: [is, tClass] 传入的实体类,成员变量类型只能是基本类型和字符串
* @return: java.util.List<T>
* @auther:
* @date: 2022/5/20 11:24
* @Description: 读取excel文件,将其转换为javabean
*/
public static <T> List<T> readExcel2Bean(InputStream is, Class<T> tClass)
throws IOException, IllegalAccessException, InstantiationException,NullPointerException {
List<List<String>> list = ExcelReaderUtil.readExcel(is);
//-----------------------遍历数据到实体集合开始-----------------------------------
List<T> listBean = new ArrayList<T>();
Field[] fields = tClass.getDeclaredFields();
T uBean = null;
// i=1是因为第一行不要
for (int i = 1; i < list.size(); i++) {
System.out.println("读取第"+i+"行数据");
uBean = (T) tClass.newInstance();
List<String> listStr = list.get(i);
for (int j = 0; j < listStr.size(); j++) {
if (j>=fields.length){
break;
}
Field field = fields[j];
String datastring = listStr.get(j);
field.setAccessible(true);
if (datastring.length()>0&&datastring!=null) {
Class<?> type = field.getType();
// 只支持8中基本类型和String类型 如有其他类型 请自行添加
if (type==String.class){
field.set(uBean,datastring);
}else if(type==Integer.class||type==int.class){
field.set(uBean,Integer.parseInt(datastring));
}else if(type==Double.class||type==double.class){
field.set(uBean,Double.parseDouble(datastring));
} else if(type==Float.class||type==float.class){
field.set(uBean,Float.parseFloat(datastring));
} else if(type==Long.class||type==long.class){
field.set(uBean,Long.parseLong(datastring));
}else if (type==Boolean.class||type==boolean.class){
field.set(uBean,Boolean.parseBoolean(datastring));
}else if (type==Short.class||type==short.class){
field.set(uBean,Short.parseShort(datastring));
}else if (type==Byte.class||type==byte.class){
field.set(uBean,Byte.parseByte(datastring));
}else if (type==Character.class ||type==char.class){
field.set(uBean,datastring.charAt(0));
}
}
}
System.out.println("第"+i+"行数据为:");
System.out.println(uBean);
listBean.add(uBean);
}
return listBean;
}
/**
* Excel读取 操作,返回内容
*/
private static List<List<String>> readExcel(InputStream is)
throws IOException {
Workbook wb = null;
try {
wb = WorkbookFactory.create(is);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
int numberOfSheets = wb.getNumberOfSheets();
System.out.println("表单数量为:"+numberOfSheets);
for (int i = 0; i < numberOfSheets ; i++) {
/** 得到第一个sheet */
Sheet sheet = wb.getSheetAt(i);
/** 得到Excel的行数 */
int totalRows = sheet.getPhysicalNumberOfRows();
/** 得到Excel的列数 */
int totalCells = 0;
if (totalRows >= 1 && sheet.getRow(0) != null) {
totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
}
List<List<String>> dataLst = new ArrayList<List<String>>();
/** 循环Excel的行 */
for (int r = 0; r < totalRows; r++) {
Row row = sheet.getRow(r);
if (row == null)
{
continue;
}
List<String> rowLst = new ArrayList<String>();
/** 循环Excel的列 */
for (int c = 0; c < totalCells; c++) {
Cell cell = row.getCell(c);
String cellValue = "";
if (null != cell) {
HSSFDataFormatter hSSFDataFormatter = new HSSFDataFormatter();
cellValue = hSSFDataFormatter.formatCellValue(cell);
}
rowLst.add(cellValue);
}
/** 保存第r行的第c列 */
dataLst.add(rowLst);
}
return dataLst;
}
return null;
}
}
将javabean写入到xml文件中:
public static void testLinkCreateXml(List<CaseBean> list,String oldfilename){
long time = System.currentTimeMillis();
String newfilename = getXmlName(oldfilename,time);
System.out.println("newfilename:"+newfilename);
System.out.println("converting,please wait...");
try {
// 1、创建document对象
Document document = DocumentHelper.createDocument();
// 2、创建根节点testsuite
Element testsuite = document.addElement("testsuite");
for (int i=0;i<list.size();i++){
System.out.println(list.get(i));
Element testcase = testsuite.addElement("testcase");
testcase.addAttribute("internalized", String.valueOf(internalized));
internalized++;
testcase.addAttribute("name",list.get(i).getCaseName());
Element summary = testcase.addElement("summary");
summary.setText("<![CDATA["+list.get(i).getSummary()+"]]>");
Element preconditions = testcase.addElement("preconditions");
preconditions.setText("<![CDATA["+replaceCellAngleBrackets(list.get(i).getPreconditions())+"]]>");
Element execution_type = testcase.addElement("execution_type");
execution_type.setText("<![CDATA["+list.get(i).getExecution_type()+"]]>");
Element importance = testcase.addElement("importance");
importance.setText("<![CDATA["+list.get(i).getImportance()+"]]>");
Element steps = testcase.addElement("steps");
Element step = steps.addElement("step");
Element step_number = step.addElement("step_number");
step_number.setText("<![CDATA["+1+"]]>");
Element actions = step.addElement("actions");
actions.setText("<![CDATA["+replaceCellAngleBrackets(list.get(i).getActions())+"]]>");
Element expectedresults = step.addElement("expectedresults");
expectedresults.setText("<![CDATA["+replaceCellAngleBrackets(list.get(i).getExpectedresults())+"]]>");
int num = 2;
for (int j = 1;j<list.size();j++){
if (i >= list.size()-1){
num=2;
break;
}
if ((Objects.equals(list.get(i+1).getCaseName(),null) || Objects.equals(list.get(i+1).getCaseName(),""))
&& (Objects.equals(list.get(i+1).getCaseNo(),null) == false || Objects.equals(list.get(i+1).getCaseNo(),"") == false)){
Element step1 = steps.addElement("step");
Element step_number1 = step1.addElement("step_number");
step_number1.setText("<![CDATA["+num+"]]>");
Element actions1 = step1.addElement("actions");
actions1.setText("<![CDATA["+replaceCellAngleBrackets(list.get(i+1).getActions())+"]]>");
Element expectedresults1 = step1.addElement("expectedresults");
expectedresults1.setText("<![CDATA["+replaceCellAngleBrackets(list.get(i+1).getExpectedresults())+"]]>");
num++;
i++;
if (i >= list.size()-1){
num=2;
break;
}
}else if (Objects.equals(list.get(i+1).getCaseName(),list.get(i).getCaseName())){
Element step1 = steps.addElement("step");
Element step_number1 = step1.addElement("step_number");
step_number1.setText("<![CDATA["+num+"]]>");
Element actions1 = step1.addElement("actions");
actions1.setText("<![CDATA["+replaceCellAngleBrackets(list.get(i+1).getActions())+"]]>");
Element expectedresults1 = step1.addElement("expectedresults");
expectedresults1.setText("<![CDATA["+replaceCellAngleBrackets(list.get(i+1).getExpectedresults())+"]]>");
num++;
i++;
if (i >= list.size()-1){
num=2;
break;
}
}else {
num=2;
break;
}
// if (list.get(j).getCaseName().equals(list.get(j-1).getCaseName()) == false ){
// num=2;
// break;
// }
//
// Element step1 = steps.addElement("step");
// Element step_number1 = step1.addElement("step_number");
// step_number1.setText("<![CDATA["+num+"]]>");
//
// Element actions1 = step1.addElement("actions");
// actions1.setText("<![CDATA["+replaceCellAngleBrackets(list.get(j).getActions())+"]]>");
//
// Element expectedresults1 = step1.addElement("expectedresults");
// expectedresults1.setText("<![CDATA["+replaceCellAngleBrackets(list.get(j).getExpectedresults())+"]]>");
//
// num++;
}
}
// 5、设置生成xml的格式
OutputFormat format = OutputFormat.createPrettyPrint();
// 设置编码格式
format.setEncoding("UTF-8");
// 6、生成xml文件
// File file = new File("testlink.xml");
File file = new File(newfilename);
XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
// 设置是否转义,默认使用转义字符
writer.setEscapeText(false);
writer.write(document);
writer.close();
System.out.println("生成testlink.xml成功");
} catch (Exception e) {
e.printStackTrace();
System.out.println("生成testlink.xml失败");
}
}
以上就是这个工具的思路,如果想获取该工具,请在评论中写上邮箱号,我私发出来,同时会把用例模板一起发出来。
想了解更多测试开发方面的知识,请关注以下公众号:
