fix(UI 自动化): UI 测试产生的截图增加清理机制

--bug=1014128 --user=周骏弘 【UI测试】UI测试产生的截图缺少清理机制 https://www.tapd.cn/55049933/s/1187159
This commit is contained in:
junhong 2022-06-20 21:35:37 +08:00 committed by zhangdahai112
parent 6822146dd4
commit 4186f3fa88
11 changed files with 46 additions and 5 deletions

View File

@ -18,4 +18,5 @@ public class ApiScenarioReportBaseInfoDTO {
private long rspTime;
private String uiImg;
private Boolean isNotStep;
private String reportId;
}

View File

@ -15,6 +15,7 @@ import java.util.Map;
public class RequestResultExpandDTO extends RequestResult {
private String status;
private String uiImg;
private String reportId;
private long time;
private Map<String, String> attachInfoMap;
@ -32,6 +33,7 @@ public class RequestResultExpandDTO extends RequestResult {
this.setTime(dto.getRspTime());
this.setEndTime(dto.getRspTime() - dto.getReqStartTime());
this.setUiImg(dto.getUiImg());
this.setReportId(dto.getReportId());
this.setStatus(requestResult.getStatus());
ResponseResult responseResult = this.getResponseResult();
responseResult.setResponseCode(dto.getRspCode());

View File

@ -8,4 +8,5 @@ import lombok.Setter;
public class UiScenarioReportBaseInfoDTO extends ApiScenarioReportBaseInfoDTO {
private Boolean isNotStep = false;
private String uiImg;
private String reportId;
}

View File

@ -1,5 +1,5 @@
package io.metersphere.api.dto.automation;
public enum ExecuteType {
Saved, Completed, Debug, Marge
Saved, Completed, Debug, Marge, Deleted
}

View File

@ -16,6 +16,8 @@ public class RunDefinitionRequest {
private String runMode;
private String uiRunMode;
private boolean isDebug;
private boolean saved;

View File

@ -79,6 +79,11 @@ public class ParameterConfig extends MsParameter {
private String scenarioId;
/**
* 报告 ID
*/
private String reportId;
private String reportType;
private boolean runLocal;

View File

@ -31,6 +31,7 @@ import io.metersphere.service.SystemParameterService;
import io.metersphere.service.UserService;
import io.metersphere.track.dto.PlanReportCaseDTO;
import io.metersphere.utils.LoggerUtil;
import io.metersphere.xpack.ui.service.UiScenarioReportStructureService;
import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
@ -90,6 +91,8 @@ public class ApiScenarioReportService {
private UiReportServiceProxy uiReportServiceProxy;
@Resource
private ExtApiScenarioReportResultMapper extApiScenarioReportResultMapper;
@Resource
private UiScenarioReportStructureService uiScenarioReportStructureService;
public void saveResult(ResultDTO dto) {
// 报告详情内容
@ -249,6 +252,11 @@ public class ApiScenarioReportService {
if (StringUtils.isNotEmpty(report.getTriggerMode()) && report.getTriggerMode().equals("CASE")) {
report.setTriggerMode(TriggerMode.MANUAL.name());
}
// UI 调试类型报告不记录更新状态
if(report.getExecuteType().equals(ExecuteType.Debug.name()) &&
report.getReportType().equals(ReportTypeConstants.UI_INDEPENDENT.name())){
return report;
}
apiScenarioReportMapper.updateByPrimaryKeySelective(report);
return report;
}
@ -496,6 +504,11 @@ public class ApiScenarioReportService {
executeTimes = scenario.getExecuteTimes().intValue();
}
scenario.setExecuteTimes(executeTimes + 1);
// 针对 UI 调试类型的不需要更新
if(report.getExecuteType().equals(ExecuteType.Debug.name()) &&
report.getReportType().equals(ReportTypeConstants.UI_INDEPENDENT.name())){
return report;
}
uiScenarioMapper.updateByPrimaryKey(scenario);
}
@ -615,6 +628,11 @@ public class ApiScenarioReportService {
ApiDefinitionExecResultExample execResultExample = new ApiDefinitionExecResultExample();
execResultExample.createCriteria().andIntegratedReportIdEqualTo(request.getId());
definitionExecResultMapper.deleteByExample(execResultExample);
}else{
// UI 类型报告需要删除报告产生的截图
List<String> ids = new ArrayList<>();
ids.add(request.getId());
uiScenarioReportStructureService.cleanUpReport(ids);
}
// 补充逻辑如果是集成报告则把零时报告全部删除
@ -735,6 +753,11 @@ public class ApiScenarioReportService {
ids = otherIdList;
}
if(!BooleanUtils.isNotTrue(reportRequest.getIsUi())){
// UI 类型报告需要删除报告产生的截图
uiScenarioReportStructureService.cleanUpReport(ids);
}
//处理最后剩余的数据
if (!ids.isEmpty()) {
ApiScenarioReportDetailExample detailExample = new ApiScenarioReportDetailExample();

View File

@ -31,8 +31,8 @@ public class ResourceController {
}
@GetMapping(value = "/ui/get")
public ResponseEntity<FileSystemResource> getUiFile(@RequestParam ("fileName") String fileName) {
return resourceService.getUiResultImage(fileName);
public ResponseEntity<FileSystemResource> getUiFile(@RequestParam ("fileName") String fileName, @RequestParam ("reportId") String reportId) {
return resourceService.getUiResultImage(fileName, reportId);
}
/**

View File

@ -7,6 +7,7 @@ import io.metersphere.commons.utils.LogUtil;
import io.metersphere.dto.ProjectConfig;
import io.metersphere.service.ProjectApplicationService;
import io.metersphere.service.ProjectService;
import io.metersphere.xpack.ui.service.UiScenarioReportStructureService;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.quartz.JobExecutionContext;
@ -23,6 +24,7 @@ public class CleanUpReportJob extends MsScheduleJob {
private final ProjectService projectService;
private final ProjectApplicationService projectApplicationService;
private final UiScenarioReportStructureService uiScenarioReportStructureService;
private static final String UNIT_DAY = "D";
private static final String UNIT_MONTH = "M";
private static final String UNIT_YEAR = "Y";
@ -31,6 +33,7 @@ public class CleanUpReportJob extends MsScheduleJob {
public CleanUpReportJob() {
projectService = CommonBeanFactory.getBean(ProjectService.class);
projectApplicationService = CommonBeanFactory.getBean(ProjectApplicationService.class);
uiScenarioReportStructureService = CommonBeanFactory.getBean(UiScenarioReportStructureService.class);
localDate = LocalDate.now();
}
@ -52,6 +55,8 @@ public class CleanUpReportJob extends MsScheduleJob {
if (BooleanUtils.isTrue(config.getCleanLoadReport())) {
this.doCleanUp(projectService::cleanUpLoadReport, config.getCleanLoadReportExpr());
}
// 定时删除 UI 调试模式生成的截图
uiScenarioReportStructureService.cleanUpReport(null);
} catch (Exception e) {
LogUtil.error("clean up report error.");
LogUtil.error(e.getMessage(), e);

View File

@ -46,11 +46,11 @@ public class ResourceService {
return getImage(FileUtils.MD_IMAGE_DIR + "/" + name);
}
public ResponseEntity<FileSystemResource> getUiResultImage(String name) {
public ResponseEntity<FileSystemResource> getUiResultImage(String name, String reportId) {
if (name.contains("/")) {
MSException.throwException(Translator.get("invalid_parameter"));
}
return getImage(FileUtils.UI_IMAGE_DIR + "/" + name);
return getImage(FileUtils.UI_IMAGE_DIR + "/" + reportId + "/" + name);
}
public ResponseEntity<FileSystemResource> getImage(String path) {

View File

@ -14,6 +14,7 @@ export default {
environment: Map,
executeType: String,
runMode: String,
uiRunMode: String,
debug: Boolean,
reportId: String,
runData: Object,
@ -79,6 +80,7 @@ export default {
reqObj.variables = this.runData.variables;
}
reqObj.runLocal = this.runLocal;
reqObj.uiRunMode = this.uiRunMode;
this.$emit('runRefresh', {});
let url = '/api/automation/run/debug';