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 b16dbc23c9
commit c1710d5d80
11 changed files with 46 additions and 5 deletions

View File

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

View File

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

View File

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

View File

@ -1,5 +1,5 @@
package io.metersphere.api.dto.automation; package io.metersphere.api.dto.automation;
public enum ExecuteType { 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 runMode;
private String uiRunMode;
private boolean isDebug; private boolean isDebug;
private boolean saved; private boolean saved;

View File

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

View File

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

View File

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

View File

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

View File

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