添加ExcelRecord类,但有报错
This commit is contained in:
parent
3709379f20
commit
7c7dc3f7d1
|
@ -1,55 +1,61 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<templet>
|
||||
<sheet name="测试用例" freeze="3">
|
||||
<column id="case_id" name="用例编号" wide="17" align="center" link="运行记录|case_id"/>
|
||||
<column id="module" name="所属模块" wide="25" align="center"/>
|
||||
<sheet name="测试用例" freeze="2">
|
||||
<column id="case_id" name="用例编号" wide="17" align="center"/>
|
||||
<column id="title" name="用例标题" wide="30" align="left"/>
|
||||
<column id="step" name="步骤" wide="45" align="left"/>
|
||||
<column id="expect" name="预期" wide="45" align="left"/>
|
||||
<column id="condition" name="前置条件" wide="30" align="left"/>
|
||||
<column id="flow" name="所属流程" wide="13" align="center"/>
|
||||
<column id="condition" name="前置条件" wide="30" align="left" index='true'/>
|
||||
</sheet>
|
||||
<sheet name="运行记录" freeze="6" >
|
||||
<sheet name="运行记录" freeze="5" >
|
||||
<column id="id" name="序号" wide="8" align="center"/>
|
||||
<column id="case_id" name="用例编号" wide="17" align="center" link="测试用例|case_id"/>
|
||||
<column id="case_id" name="用例编号" wide="17" align="center"/>
|
||||
<column id="class_name" name="类名" wide="25" align="left"/>
|
||||
<column id="method_name" name="方法名" wide="15" align="left"/>
|
||||
<column id="state" name="执行状态" wide="10" align="center" link="错误记录|id"/>
|
||||
<column id="step" name="执行步骤" wide="25" align="left"/>
|
||||
<column id="result" name="执行结果" wide="25" align="left"/>
|
||||
<column id="condition" name="前置条件" wide="25" align="left"/>
|
||||
<column id="mark" name="备注" wide="25" align="left"/>
|
||||
<column id="state" name="执行状态" wide="10" align="center"/>
|
||||
<column id="step" name="执行步骤" wide="25" align="left" index='true'/>
|
||||
<column id="result" name="执行结果" wide="25" align="left" index='true'/>
|
||||
<column id="bug_number" name="Bug数量" wide="10" align="center"/>
|
||||
<column id="screenshot_position" name="截图位置" wide="25" align="center" link="运行记录截图|id"/>
|
||||
<column id="use_time" name="执行耗时(s)" wide="15" align="center"/>
|
||||
<column id="screenshot_position" name="截图位置" wide="25" align="center"/>
|
||||
<column id="use_time" name="执行耗时" wide="15" align="center"/>
|
||||
<column id="active_person" name="执行者" wide="20" align="center"/>
|
||||
<column id="active_time" name="执行时间" wide="12" align="center"/>
|
||||
<column id="brower" name="浏览器" wide="12" align="center"/>
|
||||
<column id="version" name="版本" wide="12" align="center"/>
|
||||
<column id="system" name="操作系统" wide="12" align="center"/>
|
||||
<column id="mark" name="备注" wide="25" align="left"/>
|
||||
|
||||
<datas id='state'>
|
||||
<data name='正常' />
|
||||
<data name='异常' />
|
||||
</datas>
|
||||
</sheet>
|
||||
|
||||
<!--
|
||||
<sheet name="运行记录截图" freeze="0">
|
||||
<column id="id" name="序号" wide="8" align="center"/>
|
||||
<column id="class_name" name="类名" wide="25" align="left"/>
|
||||
<column id="method_name" name="方法名" wide="15" align="left" link="运行记录|id"/>
|
||||
<column id="screenshot" name="截图" wide="25" align="left" link="本地"/>
|
||||
</sheet>
|
||||
-->
|
||||
|
||||
<sheet name="错误记录" freeze="4">
|
||||
<column id="id" name="序号" wide="8" align="center"/>
|
||||
<column id="class_name" name="类名" wide="25" align="left"/>
|
||||
<column id="method_name" name="方法名" wide="15" align="left" link="运行记录|id"/>
|
||||
<column id="method_name" name="方法名" wide="15" align="left"/>
|
||||
<column id="error_step" name="错误步骤" wide="25" align="left"/>
|
||||
<column id="error_class" name="异常类" wide="25" align="left"/>
|
||||
<column id="error_information" name="异常信息" wide="25" align="left"/>
|
||||
<column id="screenshot_position" name="截图位置" wide="25" align="left" link="错误记录截图|id"/>
|
||||
<column id="screenshot_position" name="截图位置" wide="25" align="left"/>
|
||||
</sheet>
|
||||
|
||||
<!--
|
||||
<sheet name="错误记录截图" freeze="0">
|
||||
<column id="id" name="序号" wide="8" align="center"/>
|
||||
<column id="class_name" name="类名" wide="25" align="left"/>
|
||||
<column id="method_name" name="方法名" wide="15" align="left" link="错误记录|id"/>
|
||||
<column id="screenshot" name="截图" wide="25" align="left" link="本地"/>
|
||||
</sheet>
|
||||
-->
|
||||
</templet>
|
BIN
Result/测试用例.xlsx
BIN
Result/测试用例.xlsx
Binary file not shown.
|
@ -54,7 +54,7 @@ import pres.auxiliary.work.testcase.templet.LabelNotFoundException;
|
|||
* <b>编码时间:</b>2020年2月17日下午9:36:00
|
||||
* </p>
|
||||
* <p>
|
||||
* <b>修改时间:</b>2020年8月12日 下午14:29:37
|
||||
* <b>修改时间:</b>2020年8月22日 下午17:29:37
|
||||
* </p>
|
||||
*
|
||||
* @author 彭宇琦
|
||||
|
@ -180,14 +180,16 @@ public abstract class AbstractWriteExcel<T extends AbstractWriteExcel<T>> {
|
|||
|
||||
// 将相应的sheet标签的name属性存储至sheetName中
|
||||
this.nowSheetName = sheetName;
|
||||
writeSheetNameList.add(sheetName);
|
||||
if (!writeSheetNameList.contains(sheetName)) {
|
||||
writeSheetNameList.add(sheetName);
|
||||
}
|
||||
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 设置字段名称的常值,通过该设置,则之后该字段将直接填入设置的值,无需再次写入字段的值
|
||||
* 用于在当前指向的sheet中设置字段名称的常值,通过该设置,则之后该字段将直接填入设置的值,无需再次写入字段的值
|
||||
*
|
||||
* @param field 字段id
|
||||
* @param content 相应字段的内容
|
||||
|
@ -195,22 +197,29 @@ public abstract class AbstractWriteExcel<T extends AbstractWriteExcel<T>> {
|
|||
* @throws LabelNotFoundException 当在sheet标签中查不到相应的单元格id不存在时抛出的异常
|
||||
*/
|
||||
public void setFieldValue(String field, String content) {
|
||||
// 为保证在写用例的时候也能生效,故将值设置进入fieldMap
|
||||
//为保证内容被重复添加,故需要清空原始内容
|
||||
// clearContent(field);
|
||||
// addContent(field, content);
|
||||
|
||||
// 先将值设置入fieldMap中可以保证field字段是存在于fieldMap中,以减少此处再做判断
|
||||
// 将字段内容写入caseValueMap
|
||||
// constValueMap.put(field, content);
|
||||
setFieldValue(nowSheetName, field, content);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于在指定的sheet中设置字段名称的常值,通过该设置,则之后该字段将直接填入设置的值,无需再次写入字段的值
|
||||
*
|
||||
* @param sheetName 指定的sheet名称
|
||||
* @param field 字段id
|
||||
* @param content 相应字段的内容
|
||||
*
|
||||
* @throws LabelNotFoundException 当在sheet标签中查不到相应的单元格id不存在时抛出的异常
|
||||
*/
|
||||
public void setFieldValue(String sheetName, String field, String content) {
|
||||
//若当前sheetName中不存在常量,则先构造HashMap
|
||||
if (!constValueMap.containsKey(nowSheetName)) {
|
||||
constValueMap.put(nowSheetName, new HashMap<String, String>(16));
|
||||
if (!constValueMap.containsKey(sheetName)) {
|
||||
constValueMap.put(sheetName, new HashMap<String, String>(16));
|
||||
}
|
||||
|
||||
//添加常量
|
||||
constValueMap.get(nowSheetName).put(field, content);
|
||||
constValueMap.get(sheetName).put(field, content);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 用于设置需要被替换的词语。添加词语时无需添加特殊字符
|
||||
|
@ -393,6 +402,9 @@ public abstract class AbstractWriteExcel<T extends AbstractWriteExcel<T>> {
|
|||
sheetElement = contentXml.getRootElement().addElement("sheet").addAttribute("name", sheetName);
|
||||
}
|
||||
*/
|
||||
if (writeSheetNameList.isEmpty()) {
|
||||
throw new IncorrectIndexException("当前不存在需要写入的sheet内容");
|
||||
}
|
||||
|
||||
//生成case标签的uuid
|
||||
String caseUuid = UUID.randomUUID().toString();
|
||||
|
@ -457,6 +469,8 @@ public abstract class AbstractWriteExcel<T extends AbstractWriteExcel<T>> {
|
|||
*/
|
||||
});
|
||||
|
||||
//清空sheet写入记录
|
||||
writeSheetNameList.clear();
|
||||
return new FieldMark(caseUuid);
|
||||
}
|
||||
|
||||
|
@ -558,11 +572,11 @@ public abstract class AbstractWriteExcel<T extends AbstractWriteExcel<T>> {
|
|||
field.addDataValidation(xs, index, index);
|
||||
// 向单元格中添加Comment注解
|
||||
addComment(xs, xc, fieldElement);
|
||||
//向单元格中添加超链接
|
||||
addLink(xc, fieldElement.attributeValue("link"));
|
||||
|
||||
// 将字段内容写入单元格
|
||||
writeText(xc, textList);
|
||||
//向单元格中添加超链接
|
||||
addLink(xc, fieldElement.attributeValue("link"));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -793,7 +807,11 @@ public abstract class AbstractWriteExcel<T extends AbstractWriteExcel<T>> {
|
|||
* 清空fieldMap内的存储字段信息,不清除字段
|
||||
*/
|
||||
private void clearFieldContent() {
|
||||
fieldMap.get(nowSheetName).forEach((key, value) -> value.clearContent());
|
||||
fieldMap.forEach((sheetName, sheetFieldMap) -> {
|
||||
sheetFieldMap.forEach((fieldId, field) -> {
|
||||
field.clearContent();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1102,7 +1120,7 @@ public abstract class AbstractWriteExcel<T extends AbstractWriteExcel<T>> {
|
|||
public class FieldMark {
|
||||
// 用于存储case标签元素
|
||||
// Element caseElement;
|
||||
String uuid;
|
||||
protected String uuid;
|
||||
|
||||
/**
|
||||
* 构造类,但不允许外部进行构造
|
||||
|
@ -1297,7 +1315,6 @@ public abstract class AbstractWriteExcel<T extends AbstractWriteExcel<T>> {
|
|||
public FieldMark changeTextColor(String sheetName, String field, int startIndex, int endIndex, MarkColorsType markColorsType) {
|
||||
//查找nowSheetName指向的sheet中的与uuid一致的单元格
|
||||
Element caseElement = getCaseElement(sheetName);
|
||||
|
||||
// 获取case下的name属性与所传参数相同的field标签
|
||||
((List<Element>) (caseElement.elements())).stream().filter(e -> e.attributeValue("id").equals(field))
|
||||
.forEach(fieldElement -> {
|
||||
|
@ -1404,14 +1421,16 @@ public abstract class AbstractWriteExcel<T extends AbstractWriteExcel<T>> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 用于根据指定的sheet名称获取其下相应的case标签,并返回其元素
|
||||
* 用于根据指定的sheet名称获取其下相应的case标签,并返回其元素,若查不到元素,则返回null
|
||||
* @param sheetName sheet名称
|
||||
* @return 指定sheet名称下的case标签元素
|
||||
*/
|
||||
private Element getCaseElement(String sheetName) {
|
||||
public Element getCaseElement(String sheetName) {
|
||||
/*
|
||||
if (!writeSheetNameList.contains(sheetName)) {
|
||||
throw new IncorrectIndexException("不存在的sheet名称:" + sheetName);
|
||||
return null;
|
||||
}
|
||||
*/
|
||||
|
||||
return ((Element) (contentXml.selectSingleNode("//sheet[@name='" + sheetName +"']/case[@id='" + uuid + "']")));
|
||||
}
|
||||
|
@ -1653,5 +1672,14 @@ public abstract class AbstractWriteExcel<T extends AbstractWriteExcel<T>> {
|
|||
DataValidation d = new XSSFDataValidationHelper(caseSheet).createValidation(constraint, regions);
|
||||
caseSheet.addValidationData(d);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据下标返回字段中存储的内容,下标允许传入负数,表示从后向前遍历
|
||||
* @param index 下标
|
||||
* @return 下标对应的字段内容
|
||||
*/
|
||||
public String getContent(int index) {
|
||||
return content.get(getPoiIndex(content.size(), index));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -371,6 +371,7 @@ public abstract class AbstractBrower {
|
|||
pageMap.put(popuHandle, popuPage);
|
||||
//将当前页面指向到弹窗页面上
|
||||
nowPage = popuPage;
|
||||
windowHandleSet.add(popuHandle);
|
||||
|
||||
//返回切换弹窗成功
|
||||
return true;
|
||||
|
|
|
@ -1,12 +1,16 @@
|
|||
package pres.auxiliary.work.selenium.tool;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.dom4j.Document;
|
||||
import org.dom4j.DocumentException;
|
||||
import org.dom4j.Element;
|
||||
|
||||
import pres.auxiliary.tool.date.Time;
|
||||
import pres.auxiliary.tool.file.excel.AbstractWriteExcel;
|
||||
import pres.auxiliary.work.selenium.brower.AbstractBrower;
|
||||
import pres.auxiliary.work.testcase.file.MarkColorsType;
|
||||
|
||||
/**
|
||||
* <p><b>文件名:</b>ExcelRecord.java</p>
|
||||
|
@ -21,30 +25,270 @@ import pres.auxiliary.work.selenium.brower.AbstractBrower;
|
|||
*/
|
||||
public class ExcelRecord extends AbstractWriteExcel<ExcelRecord> {
|
||||
/**
|
||||
* 存储浏览器对象
|
||||
* 用于记录当前出现BUG的结果位置
|
||||
*/
|
||||
AbstractBrower brower;
|
||||
private ArrayList<Integer> resultBugList = new ArrayList<>();
|
||||
/**
|
||||
* 用于指向当前记录的运行截图文件
|
||||
*/
|
||||
private File runScreenhotFile = null;
|
||||
/**
|
||||
* 用于指向当前记录的异常截图文件
|
||||
*/
|
||||
private File errorScreenhotFile = null;
|
||||
/**
|
||||
* 用于指向当前是否存在异常
|
||||
*/
|
||||
boolean isError = false;
|
||||
/**
|
||||
* 用于记录开始执行的时间
|
||||
*/
|
||||
private long startTime = 0L;
|
||||
|
||||
/**
|
||||
* 通过
|
||||
* @param configDocument
|
||||
* @param tempFile
|
||||
* @param brower
|
||||
* 根据xml配置文件的{@link Document}类对象进行构造
|
||||
* @param configDocument xml配置文件的{@link Document}类对象
|
||||
* @param tempFile excel文件对象
|
||||
*/
|
||||
public ExcelRecord(Document configDocument, File tempFile, AbstractBrower brower) {
|
||||
public ExcelRecord(Document configDocument, File tempFile) {
|
||||
super(configDocument, tempFile);
|
||||
this.brower = brower;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param configFile
|
||||
* @param tempFile
|
||||
* @param brower
|
||||
* @throws DocumentException
|
||||
* 根据xml配置文件对象进行构造
|
||||
* @param configFile xml配置文件对象
|
||||
* @param tempFile excel文件对象
|
||||
* @throws DocumentException 当读取xml配置文件对象失败时抛出的异常
|
||||
*/
|
||||
public ExcelRecord(File configFile, File tempFile, AbstractBrower brower) throws DocumentException {
|
||||
public ExcelRecord(File configFile, File tempFile) throws DocumentException {
|
||||
super(configFile, tempFile);
|
||||
this.brower = brower;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于添加浏览器信息。该方法不能单独调用,必须配合其他
|
||||
* 其他方法一起使用,否则在生成文件时,其写入的内容不会被填写到文件中
|
||||
* @param brower 继承自{@link AbstractBrower}类的浏览器类对象
|
||||
* @return 类本身
|
||||
*/
|
||||
public ExcelRecord setBrowerInformation(AbstractBrower brower) {
|
||||
//TODO 添加浏览器信息内容
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于添加执行者。该方法不能单独调用,必须配合的其他其他方法一起使用,
|
||||
* 否则在生成文件时,其写入的内容不会被填写到文件中
|
||||
* @param actionName 执行者
|
||||
* @return 类本身
|
||||
*/
|
||||
public ExcelRecord setActionName(String actionName) {
|
||||
switchSheet("运行记录").setFieldValue("active_person", actionName);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于添加运行类名以及运行方法名。多次调用时会清空上一次记录的内容
|
||||
* @param className 类名
|
||||
* @param methodName 方法名
|
||||
* @return 类本身
|
||||
*/
|
||||
public ExcelRecord runMethod(String className, String methodName) {
|
||||
setFieldValue("运行记录", "class_name", className);
|
||||
setFieldValue("运行记录", "method_name", methodName);
|
||||
setFieldValue("错误记录", "class_name", className);
|
||||
setFieldValue("错误记录", "method_name", methodName);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于记录执行开始时间,记录结束时,会写入相应的结束时间
|
||||
*/
|
||||
public void reckonByTime() {
|
||||
//若设定当前状态为开始记录状态,则存储当前的时间戳
|
||||
startTime = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于添加实际运行步骤,支持传入多条,每条数据将自动编号以及换行
|
||||
*
|
||||
* @param text 实际运行步骤
|
||||
* @return 类本身
|
||||
*/
|
||||
public ExcelRecord runStep(String... text) {
|
||||
return switchSheet("运行记录").addContent("step", text);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于添加执行结果,根据传入的Bug判定来记录当前执行的Bug数量。该方法可调用多次,
|
||||
* 将多个结果,结果将自动编号。
|
||||
*
|
||||
* @param text 结果
|
||||
* @param bug 标记是否为Bug
|
||||
* @return 类本身
|
||||
*/
|
||||
public ExcelRecord runResult(String text, boolean bug) {
|
||||
//若当前结果为BUG,则记录相应的编号
|
||||
if (bug) {
|
||||
//编号可根据相应sheet中字段的内容数量获取到当前出现BUG的内容位置
|
||||
resultBugList.add(fieldMap.get("运行记录").get("result").content.size());
|
||||
}
|
||||
|
||||
return switchSheet("运行记录").addContent("result", text);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于添加运行备注
|
||||
*
|
||||
* @param text 备注文本
|
||||
* @return 类本身
|
||||
*/
|
||||
public ExcelRecord runMark(String text) {
|
||||
return switchSheet("运行记录").addContent("mark", text);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于添加运行时截图
|
||||
*
|
||||
* @param screenshotFile 截图文件对象
|
||||
* @return 类本身
|
||||
*/
|
||||
public ExcelRecord runScreenshot(File screenshotFile) {
|
||||
//记录运行截图
|
||||
runScreenhotFile = screenshotFile;
|
||||
|
||||
return switchSheet("运行记录")
|
||||
.addContent("screenshot_position", screenshotFile.getPath());
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于添加异常步骤的信息,传入异常类后,将会自动记录异常步骤
|
||||
* @param exception 异常类
|
||||
* @return 类本身
|
||||
*/
|
||||
public ExcelRecord exception(Exception exception) {
|
||||
isError = true;
|
||||
return switchSheet("错误记录")
|
||||
.addContent("error_step", fieldMap.get("运行记录").get("step").content.size() == 0 ? "" : fieldMap.get("运行记录").get("step").getContent(-1))
|
||||
.addContent("error_class", exception.getClass().getName())
|
||||
.addContent("error_information", exception.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于添加异常步骤信息,并记录截图,可参见{@link #exception(Exception)}
|
||||
* @param exception 异常类
|
||||
* @param screenshotFile 截图文件对象
|
||||
* @return 类本身
|
||||
*/
|
||||
public ExcelRecord exception(Exception exception, File screenshotFile) {
|
||||
return exception(exception)
|
||||
.switchSheet("错误记录")
|
||||
.addContent("screenshot_position", screenshotFile.getPath());
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于添加测试用例的前置条件,支持传入多条,每条数据将自动编号以及换行
|
||||
*
|
||||
* @param text 前置条件
|
||||
* @return 类本身
|
||||
*/
|
||||
public ExcelRecord caseCondition(String... text) {
|
||||
return switchSheet("测试用例").addContent("condition", text);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于添加测试用例的标题,不支持传入多个标题,每次调用方法后将覆盖之前传入的内容
|
||||
* @param text 标题
|
||||
* @return 类本身
|
||||
*/
|
||||
public ExcelRecord caseTitle(String text) {
|
||||
return switchSheet("测试用例")
|
||||
.clearContent("title")
|
||||
.addContent("title", text);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于添加测试用例的步骤,支持传入多条,每条数据将自动编号以及换行
|
||||
* @param text 标题
|
||||
* @return 类本身
|
||||
*/
|
||||
public ExcelRecord caseStep(String... text) {
|
||||
return switchSheet("测试用例")
|
||||
.addContent("step", text);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于添加测试用例的预期,支持传入多条,每条数据将自动编号以及换行
|
||||
* @param text 标题
|
||||
* @return 类本身
|
||||
*/
|
||||
public ExcelRecord caseExpect(String... text) {
|
||||
return switchSheet("测试用例")
|
||||
.addContent("expect", text);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AbstractWriteExcel<ExcelRecord>.FieldMark end() {
|
||||
//记录运行状态,Bug数量,当前时间以及运行时间
|
||||
switchSheet("运行记录")
|
||||
.addContent("state", isError ? "2" : "1")
|
||||
.addContent("bug_number", String.valueOf(resultBugList.size()))
|
||||
.addContent("active_time", new Time().getFormatTime())
|
||||
.addContent("use_time", startTime == 0L ? "" : (String.valueOf((System.currentTimeMillis() - startTime) / 1000.0) + "s"));
|
||||
|
||||
//获取父类的end方法,结束当前的记录
|
||||
FieldMark fieldMark = super.end();
|
||||
|
||||
//判断是否存在BUG,并标记相应的结果文本为红色
|
||||
resultBugList.forEach(bugIndex -> {
|
||||
fieldMark.changeTextColor("result", bugIndex, MarkColorsType.RED);
|
||||
});
|
||||
|
||||
//若存在异常,则将运行状态文本标记为红色,并添加相应的超链接
|
||||
if (isError) {
|
||||
//修改文本为红色
|
||||
fieldMark.changeTextColor("运行记录", "state", 0, MarkColorsType.RED);
|
||||
|
||||
//添加运行记录与错误记录的内链关系
|
||||
fieldMark.fieldLink("运行记录", "state", getDocumentLinkPath("错误记录|id|-1"));
|
||||
fieldMark.fieldLink("错误记录", "method_name", getDocumentLinkPath("运行记录|method_name|-1"));
|
||||
//若存在异常截图文件,则添加本地文件链接
|
||||
if (errorScreenhotFile != null) {
|
||||
fieldMark.fieldLink("错误记录", "screenshot_position", getScreenshotPath(errorScreenhotFile));
|
||||
}
|
||||
}
|
||||
|
||||
//若需要进行超链接,则添加相应的超链接
|
||||
if (runScreenhotFile != null) {
|
||||
fieldMark.fieldLink("运行记录", "screenshot_position", getScreenshotPath(runScreenhotFile));
|
||||
}
|
||||
|
||||
//获取测试用例中相应的元素
|
||||
Element caseElement = fieldMark.getCaseElement("测试用例");
|
||||
Element runElement = fieldMark.getCaseElement("运行记录");
|
||||
//若能获取到元素,则添加运行记录与测试用例之间的关联
|
||||
if (caseElement != null && runElement != null) {
|
||||
fieldMark.fieldLink("测试用例", "case_id", getDocumentLinkPath("运行记录|case_id|-1"));
|
||||
fieldMark.fieldLink("运行记录", "case_id", getDocumentLinkPath("测试用例|case_id|-1"));
|
||||
}
|
||||
|
||||
|
||||
//初始化数据
|
||||
initDta();
|
||||
return fieldMark;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于初始化所有需要用作记录的数据
|
||||
*/
|
||||
private void initDta() {
|
||||
resultBugList.clear();
|
||||
runScreenhotFile = null;
|
||||
errorScreenhotFile = null;
|
||||
startTime = 0L;
|
||||
isError = false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -53,7 +297,7 @@ public class ExcelRecord extends AbstractWriteExcel<ExcelRecord> {
|
|||
* @return 转换后的在poi使用的超链接代码
|
||||
*/
|
||||
private String getDocumentLinkPath(String linkContent) {
|
||||
String[] linkContents = linkContent.split("|");
|
||||
String[] linkContents = linkContent.split("\\|");
|
||||
int length = linkContents.length;
|
||||
|
||||
//获取超链接的sheet名称
|
||||
|
@ -63,17 +307,39 @@ public class ExcelRecord extends AbstractWriteExcel<ExcelRecord> {
|
|||
|
||||
String linkRowIndex = "";
|
||||
//获取当前表格的最后一行元素个数
|
||||
int lastIndex = xw.getSheet(linkSheetName).getLastRowNum();
|
||||
|
||||
int lastIndex = ((Element) (contentXml.selectSingleNode("//sheet[@name='" + linkSheetName + "']"))).elements().size();
|
||||
//判断当前linkContents是否存在链接行数(即第三个元素)且链接的文本为数字
|
||||
//若符合规则,则将linkRowIndex设置为当前编写的内容
|
||||
//若不符合规则,则将linkRowIndex设置为当前sheet的最后一行
|
||||
//由于存在标题行,调用getPoiIndex()方法得到的数值需要向下平移一位才能得到真实的数据
|
||||
if (length > 2) {
|
||||
linkRowIndex = String.valueOf(getPoiIndex(lastIndex + 1, Integer.valueOf(linkContents[2])));
|
||||
linkRowIndex = String.valueOf(getPoiIndex(lastIndex + 1, Integer.valueOf(linkContents[2])) + 1);
|
||||
} else {
|
||||
linkRowIndex = String.valueOf(lastIndex);
|
||||
linkRowIndex = String.valueOf(lastIndex + 1);
|
||||
}
|
||||
|
||||
//返回文档链接的内容
|
||||
return "'" + nowSheetName + "'!" + linkColumnIndex + linkRowIndex;
|
||||
return "'" + linkSheetName + "'!" + linkColumnIndex + linkRowIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于返回需要链接的截图文件路径,若截图文件在tempFile下,则返回相应的相对路径;
|
||||
* 若不在其下,则返回截图文件的绝对路径
|
||||
* @param screenshotFile 截图文件对象
|
||||
* @return 截图文件路径
|
||||
*/
|
||||
private String getScreenshotPath(File screenshotFile) {
|
||||
//获取excel文件以及截图文件存储路径
|
||||
String excelFolderPath = tempFile.getParentFile().getAbsolutePath();
|
||||
String screenhotPath = screenshotFile.getAbsolutePath();
|
||||
|
||||
//判断当前截图文件的路径是否在excel文件路径下,若存在其下,则返回相应的相对路径;
|
||||
//若不存在,则返回截图文件的绝对路径
|
||||
if (screenhotPath.indexOf(excelFolderPath) == -1) {
|
||||
return screenhotPath;
|
||||
} else {
|
||||
return "." + screenhotPath.substring(excelFolderPath.length()).replaceAll("\\\\", "/");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Binary file not shown.
|
@ -0,0 +1,108 @@
|
|||
package pres.auxiliary.work.selenium.tool;
|
||||
|
||||
import java.awt.Desktop;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.dom4j.DocumentException;
|
||||
import org.testng.annotations.AfterMethod;
|
||||
import org.testng.annotations.AfterTest;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.BeforeTest;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import pres.auxiliary.tool.file.excel.CreateExcelFile;
|
||||
|
||||
public class ExcelRecordTest {
|
||||
CreateExcelFile cef;
|
||||
ExcelRecord er;
|
||||
|
||||
final File configXml = new File("ConfigurationFiles/SeleniumConfigurationFile/LogConfiguration/ExcelRecordTemplet.xml");
|
||||
final File tempFile = new File("src/test/java/pres/auxiliary/work/selenium/tool/Test.xlsx");
|
||||
final File imageFile = new File("src/test/java/pres/auxiliary/work/selenium/tool/微信图片_20200828101657.png");
|
||||
|
||||
@BeforeTest
|
||||
public void init() throws DocumentException, IOException {
|
||||
cef = new CreateExcelFile(configXml, tempFile);
|
||||
cef.setCoverFile(true);
|
||||
cef.create();
|
||||
|
||||
er = new ExcelRecord(configXml, tempFile);
|
||||
}
|
||||
|
||||
@AfterTest
|
||||
public void writeFile() throws IOException {
|
||||
er.writeFile();
|
||||
Desktop.getDesktop().open(tempFile.getParentFile());
|
||||
Desktop.getDesktop().open(er.getCaseXml());
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public void recordBaseInfo() {
|
||||
er.setActionName("yuqipeng_d");
|
||||
}
|
||||
|
||||
@BeforeMethod
|
||||
public void startRecord(Method method) {
|
||||
er.runMethod(this.getClass().getName(), method.getName());
|
||||
er.reckonByTime();
|
||||
}
|
||||
|
||||
@AfterMethod
|
||||
public void endRecord() {
|
||||
er.end();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void runStepTest() {
|
||||
er.runStep("第一步", "第二步", "第三步");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void runResultTest() {
|
||||
er.runResult("结果1,不是BUG", false);
|
||||
er.runResult("结果2,是BUG", true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void runMarkTest() {
|
||||
er.runMark("这是备注信息");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void runScreenshotTest() throws IOException {
|
||||
er.runScreenshot(imageFile);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exceptionTest_NotFile() {
|
||||
er.exception(new NullPointerException("抛出了NullPointerException异常"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exceptionTest_HasFile() {
|
||||
er.exception(new RecordStateException("抛出了RecordStateException异常"), imageFile);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void caseConditionTest() {
|
||||
er.caseCondition("测试用例前置条件1", "测试用例前置条件2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void caseTitleTest() {
|
||||
er.caseTitle("测试用例标题");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void caseStepTest() {
|
||||
er.caseStep("测试用例步骤1", "测试用例步骤2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void caseExpectTest() {
|
||||
er.caseExpect("测试用例预期1", "测试用例预期2");
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 21 KiB |
Loading…
Reference in New Issue