fix(UI 自动化): 有失败的步骤,场景结果显示了成功

--bug=1015116 --user=张大海 【测试跟踪】测试计划详情里,批量执行ui场景,有失败的步骤,场景结果显示了成功 https://www.tapd.cn/55049933/s/1206813
This commit is contained in:
zhangdahai112 2022-07-21 21:43:14 +08:00 committed by f2c-ci-robot[bot]
parent 5d1291e4ca
commit ab7e276779
2 changed files with 52 additions and 24 deletions

View File

@ -1,6 +1,7 @@
package io.metersphere.api.service; package io.metersphere.api.service;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature; import com.alibaba.fastjson.parser.Feature;
import io.metersphere.api.dto.*; import io.metersphere.api.dto.*;
@ -36,6 +37,7 @@ 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;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.Nullable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -485,7 +487,35 @@ public class ApiScenarioReportService {
if (scenario == null) { if (scenario == null) {
scenario = uiScenarioMapper.selectByPrimaryKey(report.getScenarioId()); scenario = uiScenarioMapper.selectByPrimaryKey(report.getScenarioId());
} }
//场景模式只更新场景
if (scenario != null) { if (scenario != null) {
ApiScenarioReport report1 = updateUiScenario(requestResults, dto, errorSize, status, report, scenario);
if (report1 != null) return report1;
}
//测试计划模式 更新玩测试计划最新结果再更新场景
TestPlanUiScenario testPlanUiScenario = testPlanUiScenarioMapper.selectByPrimaryKey(dto.getTestId());
if (testPlanUiScenario != null) {
report.setScenarioId(testPlanUiScenario.getUiScenarioId());
report.setEndTime(System.currentTimeMillis());
testPlanUiScenario.setLastResult(status);
long successSize = requestResults.stream().filter(requestResult -> StringUtils.equalsIgnoreCase(requestResult.getStatus(), ScenarioStatus.Success.name())).count();
String passRate = new DecimalFormat("0%").format((float) successSize / requestResults.size());
testPlanUiScenario.setPassRate(passRate);
testPlanUiScenario.setReportId(report.getId());
report.setEndTime(System.currentTimeMillis());
testPlanUiScenario.setUpdateTime(System.currentTimeMillis());
testPlanUiScenarioMapper.updateByPrimaryKeySelective(testPlanUiScenario);
if(scenario == null){
scenario = uiScenarioMapper.selectByPrimaryKey(testPlanUiScenario.getUiScenarioId());
}
updateUiScenario(requestResults, dto, errorSize, status, report, scenario);
}
return report;
}
@Nullable
private ApiScenarioReport updateUiScenario(List<ApiScenarioReportResult> requestResults, ResultDTO dto, long errorSize, String status, ApiScenarioReport report, UiScenarioWithBLOBs scenario) {
if (StringUtils.equalsAnyIgnoreCase(status, ExecuteResult.ERROR_REPORT_RESULT.toString())) { if (StringUtils.equalsAnyIgnoreCase(status, ExecuteResult.ERROR_REPORT_RESULT.toString())) {
scenario.setLastResult(status); scenario.setLastResult(status);
} else { } else {
@ -506,23 +536,7 @@ public class ApiScenarioReportService {
return report; return report;
} }
uiScenarioMapper.updateByPrimaryKey(scenario); uiScenarioMapper.updateByPrimaryKey(scenario);
} return null;
TestPlanUiScenario testPlanUiScenario = testPlanUiScenarioMapper.selectByPrimaryKey(dto.getTestId());
if (testPlanUiScenario != null) {
report.setScenarioId(testPlanUiScenario.getUiScenarioId());
report.setEndTime(System.currentTimeMillis());
testPlanUiScenario.setLastResult(report.getStatus());
long successSize = requestResults.stream().filter(requestResult -> StringUtils.equalsIgnoreCase(requestResult.getStatus(), ScenarioStatus.Success.name())).count();
String passRate = new DecimalFormat("0%").format((float) successSize / requestResults.size());
testPlanUiScenario.setPassRate(passRate);
testPlanUiScenario.setReportId(report.getId());
report.setEndTime(System.currentTimeMillis());
testPlanUiScenario.setUpdateTime(System.currentTimeMillis());
testPlanUiScenarioMapper.updateByPrimaryKeySelective(testPlanUiScenario);
}
return report;
} }
public String getEnvironment(ApiScenarioWithBLOBs apiScenario) { public String getEnvironment(ApiScenarioWithBLOBs apiScenario) {
@ -897,6 +911,12 @@ public class ApiScenarioReportService {
return report; return report;
} }
/**
* 返回正确的报告状态
*
* @param dto jmeter返回
* @return
*/
private String getStatus(ResultDTO dto) { private String getStatus(ResultDTO dto) {
if (MapUtils.isNotEmpty(dto.getArbitraryData()) && dto.getArbitraryData().containsKey("REPORT_STATUS")) { if (MapUtils.isNotEmpty(dto.getArbitraryData()) && dto.getArbitraryData().containsKey("REPORT_STATUS")) {
// 资源池执行整体传输失败单条传输内容获取资源池执行统计的状态 // 资源池执行整体传输失败单条传输内容获取资源池执行统计的状态
@ -907,7 +927,15 @@ public class ApiScenarioReportService {
long errorReportResultSize = dto.getRequestResults().stream().filter(requestResult -> long errorReportResultSize = dto.getRequestResults().stream().filter(requestResult ->
StringUtils.equalsIgnoreCase(requestResult.getStatus(), ExecuteResult.ERROR_REPORT_RESULT.toString())).count(); StringUtils.equalsIgnoreCase(requestResult.getStatus(), ExecuteResult.ERROR_REPORT_RESULT.toString())).count();
//类型为ui时的统计
if (StringUtils.isNotEmpty(dto.getRunMode()) && dto.getRunMode().startsWith("UI")) {
try {
errorSize = dto.getRequestResults().stream().filter(requestResult ->
StringUtils.isNotEmpty(requestResult.getResponseResult().getHeaders()) && JSONArray.parseArray(requestResult.getResponseResult().getHeaders()).stream().filter(r -> ((JSONObject) r).containsKey("success") && !((JSONObject) r).getBoolean("success")).count() > 0).count();
} catch (Exception e) {
errorSize = 1;
}
}
String status = dto.getRequestResults().isEmpty() ? ExecuteResult.UN_EXECUTE.toString() : ScenarioStatus.Success.name(); String status = dto.getRequestResults().isEmpty() ? ExecuteResult.UN_EXECUTE.toString() : ScenarioStatus.Success.name();
if (errorSize > 0) { if (errorSize > 0) {
status = ScenarioStatus.Error.name(); status = ScenarioStatus.Error.name();

View File

@ -205,7 +205,7 @@ public class TestPlanUiScenarioCaseService {
request.setIds(scenarioIds); request.setIds(scenarioIds);
request.setReportId(testPlanScenarioRequest.getId()); request.setReportId(testPlanScenarioRequest.getId());
request.setScenarioTestPlanIdMap(scenarioPlanIdMap); request.setScenarioTestPlanIdMap(scenarioPlanIdMap);
request.setRunMode(ApiRunMode.UI_SCENARIO.name()); request.setRunMode(ApiRunMode.UI_SCENARIO_PLAN.name());
request.setId(testPlanScenarioRequest.getId()); request.setId(testPlanScenarioRequest.getId());
request.setExecuteType(ExecuteType.Saved.name()); request.setExecuteType(ExecuteType.Saved.name());
request.setTriggerMode(testPlanScenarioRequest.getTriggerMode()); request.setTriggerMode(testPlanScenarioRequest.getTriggerMode());