diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/ApiScenarioParseTmpParam.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/ApiScenarioParseTmpParam.java index 6bc589193a..6b37c05d09 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/ApiScenarioParseTmpParam.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/ApiScenarioParseTmpParam.java @@ -1,6 +1,7 @@ package io.metersphere.api.dto; import io.metersphere.api.dto.request.MsCommonElement; +import io.metersphere.api.dto.request.controller.MsScriptElement; import io.metersphere.api.dto.request.http.MsHTTPElement; import io.metersphere.api.dto.scenario.ApiScenarioStepCommonDTO; import lombok.Data; @@ -44,6 +45,10 @@ public class ApiScenarioParseTmpParam { * 场景中所有的 MsCommonElement 列表 */ private List commonElements = new ArrayList<>(); + /** + * 场景中所有的脚本步骤列表 + */ + private List scriptElements = new ArrayList<>(); /** * 执行的资源ID列表 * 场景执行时,为关联的所有用例和场景列表 diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiCommonService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiCommonService.java index 6b2fe26e7c..44127a70ff 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiCommonService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiCommonService.java @@ -6,6 +6,7 @@ import io.metersphere.api.dto.ApiFile; import io.metersphere.api.dto.definition.ResponseBinaryBody; import io.metersphere.api.dto.definition.ResponseBody; import io.metersphere.api.dto.request.MsCommonElement; +import io.metersphere.api.dto.request.controller.MsScriptElement; import io.metersphere.api.dto.request.http.MsHTTPElement; import io.metersphere.api.dto.request.http.body.BinaryBody; import io.metersphere.api.dto.request.http.body.Body; @@ -13,6 +14,7 @@ import io.metersphere.api.dto.request.http.body.FormDataBody; import io.metersphere.api.dto.request.http.body.FormDataKV; import io.metersphere.plugin.api.spi.AbstractMsTestElement; import io.metersphere.project.api.KeyValueParam; +import io.metersphere.project.api.assertion.MsScriptAssertion; import io.metersphere.project.api.processor.MsProcessor; import io.metersphere.project.api.processor.ScriptProcessor; import io.metersphere.project.domain.CustomFunction; @@ -27,6 +29,7 @@ import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.JSON; import jakarta.annotation.Resource; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -180,11 +183,38 @@ public class ApiCommonService { /** * 设置使用脚本前后置的公共脚本信息 + * * @param msTestElement */ public void setEnableCommonScriptProcessorInfo(AbstractMsTestElement msTestElement) { MsCommonElement msCommonElement = getMsCommonElement(msTestElement); - Optional.ofNullable(msCommonElement).ifPresent(item -> setEnableCommonScriptProcessorInfo(List.of(item))); + Optional.ofNullable(msCommonElement).ifPresent(item -> setCommonElementEnableCommonScriptInfo(List.of(item))); + } + + /** + * 设置使用脚本步骤的公共脚本信息 + * + * @param msScriptElement + */ + public void setEnableCommonScriptProcessorInfo(MsScriptElement msScriptElement) { + CommonScriptInfo commonScriptInfo = msScriptElement.getCommonScriptInfo(); + if (BooleanUtils.isTrue(msScriptElement.getEnableCommonScript()) && commonScriptInfo != null) { + setEnableCommonScriptInfo(List.of(commonScriptInfo)); + } + } + + /** + * 设置使用脚本步骤的公共脚本信息 + * + * @param msScriptElements + */ + public void setScriptElementEnableCommonScriptInfo(List msScriptElements) { + List commonScriptInfos = msScriptElements.stream() + .filter(msScriptElement -> BooleanUtils.isTrue(msScriptElement.getEnableCommonScript()) && msScriptElement.getEnableCommonScript() != null) + .map(MsScriptElement::getCommonScriptInfo) + .toList(); + + setEnableCommonScriptInfo(commonScriptInfos); } /** @@ -192,11 +222,24 @@ public class ApiCommonService { * * @param commonElements */ - public void setEnableCommonScriptProcessorInfo(List commonElements) { + public void setCommonElementEnableCommonScriptInfo(List commonElements) { List scriptsProcessors = getEnableCommonScriptProcessors(commonElements); + List scriptAssertions = getEnableCommonScriptAssertion(commonElements); - List commonScriptIds = scriptsProcessors.stream() - .map(processor -> processor.getCommonScriptInfo().getId()) + List commonScriptInfos = scriptsProcessors.stream() + .map(ScriptProcessor::getCommonScriptInfo).collect(Collectors.toList()); + + List assertionsCommonScriptInfos = scriptAssertions.stream() + .map(MsScriptAssertion::getCommonScriptInfo).toList(); + + commonScriptInfos.addAll(assertionsCommonScriptInfos); + + setEnableCommonScriptInfo(commonScriptInfos); + } + + private void setEnableCommonScriptInfo(List commonScriptInfos) { + List commonScriptIds = commonScriptInfos.stream() + .map(CommonScriptInfo::getId) .toList(); Map customFunctionBlobMap = customFunctionService.getBlobByIds(commonScriptIds).stream() @@ -205,8 +248,7 @@ public class ApiCommonService { Map customFunctionMap = customFunctionService.getByIds(commonScriptIds).stream() .collect(Collectors.toMap(CustomFunction::getId, Function.identity())); - for (ScriptProcessor processor : scriptsProcessors) { - CommonScriptInfo commonScriptInfo = processor.getCommonScriptInfo(); + for (CommonScriptInfo commonScriptInfo : commonScriptInfos) { CustomFunctionBlob customFunctionBlob = customFunctionBlobMap.get(commonScriptInfo.getId()); CustomFunction customFunction = customFunctionMap.get(commonScriptInfo.getId()); @@ -214,9 +256,9 @@ public class ApiCommonService { if (customFunction == null || customFunctionBlob == null) { if (customFunction == null) { // 公共脚本被删除,就改成非公共脚本 - processor.getCommonScriptInfo().setDeleted(true); + commonScriptInfo.setDeleted(true); } - continue; + return; } // 设置公共脚本信息 @@ -225,10 +267,10 @@ public class ApiCommonService { // 替换用户输入值 commonParams.forEach(commonParam -> Optional.ofNullable(commonScriptInfo.getParams()).ifPresent(params -> - params.stream() - .filter(param -> StringUtils.equals(commonParam.getKey(), param.getKey())) - .findFirst() - .ifPresent(param -> commonParam.setValue(param.getValue())) + params.stream() + .filter(param -> StringUtils.equals(commonParam.getKey(), param.getKey())) + .findFirst() + .ifPresent(param -> commonParam.setValue(param.getValue())) ) ); commonScriptInfo.setParams(commonParams); @@ -251,7 +293,7 @@ public class ApiCommonService { for (MsCommonElement commonElement : commonElements) { if (commonElement.getPreProcessorConfig() == null) { - continue; + continue; } processors.addAll(commonElement.getPreProcessorConfig().getProcessors()); processors.addAll(commonElement.getPostProcessorConfig().getProcessors()); @@ -261,13 +303,37 @@ public class ApiCommonService { List scriptsProcessors = processors.stream() .filter(processor -> processor instanceof ScriptProcessor) .map(processor -> (ScriptProcessor) processor) - .filter(ScriptProcessor::getEnable) .filter(ScriptProcessor::isEnableCommonScript) .filter(ScriptProcessor::isValid) .collect(Collectors.toList()); return scriptsProcessors; } + /** + * 获取使用公共脚本的前后置 + * + * @param commonElements + * @return + */ + private List getEnableCommonScriptAssertion(List commonElements) { + List assertions = new ArrayList<>(); + + for (MsCommonElement commonElement : commonElements) { + if (commonElement.getAssertionConfig() == null) { + continue; + } + List scriptAssertions = commonElement.getAssertionConfig().getAssertions() + .stream() + .filter(assertion -> assertion instanceof MsScriptAssertion) + .map(msAssertion -> (MsScriptAssertion) msAssertion) + .filter(MsScriptAssertion::isEnableCommonScript) + .filter(MsScriptAssertion::isValid) + .toList(); + assertions.addAll(scriptAssertions); + } + return assertions; + } + public MsCommonElement getMsCommonElement(AbstractMsTestElement msTestElement) { if (CollectionUtils.isNotEmpty(msTestElement.getChildren())) { for (AbstractMsTestElement child : msTestElement.getChildren()) { @@ -281,6 +347,7 @@ public class ApiCommonService { /** * 获取资源 ID 和接口定义信息 的 Map + * * @param getDefinitionInfoFunc * @param resourceIds * @return @@ -294,6 +361,7 @@ public class ApiCommonService { /** * 设置 MsHTTPElement 中的 method 等信息 + * * @param msTestElement * @param definitionExecuteInfo */ @@ -307,6 +375,7 @@ public class ApiCommonService { /** * 给 httpElement 设置接口定义参数 + * * @param apiDefinition * @param msTestElement */ diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java index 1d825f6bd6..9c441f5129 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java @@ -11,6 +11,7 @@ import io.metersphere.api.dto.definition.ExecutePageRequest; import io.metersphere.api.dto.definition.ExecuteReportDTO; import io.metersphere.api.dto.request.ApiTransferRequest; import io.metersphere.api.dto.request.MsScenario; +import io.metersphere.api.dto.request.controller.MsScriptElement; import io.metersphere.api.dto.request.http.MsHTTPElement; import io.metersphere.api.dto.response.ApiScenarioBatchOperationResponse; import io.metersphere.api.dto.scenario.*; @@ -1597,7 +1598,8 @@ public class ApiScenarioService extends MoveNodeService { setApiDefinitionExecuteInfo(tmpParam.getUniqueIdStepMap(), tmpParam.getStepTypeHttpElementMap()); // 设置使用脚本前后置的公共脚本信息 - apiCommonService.setEnableCommonScriptProcessorInfo(tmpParam.getCommonElements()); + apiCommonService.setCommonElementEnableCommonScriptInfo(tmpParam.getCommonElements()); + apiCommonService.setScriptElementEnableCommonScriptInfo(tmpParam.getScriptElements()); return tmpParam; } @@ -1790,6 +1792,8 @@ public class ApiScenarioService extends MoveNodeService { // 暂存http类型的步骤 stepTypeHttpElementMap.putIfAbsent(step.getStepType(), new LinkedList<>()); stepTypeHttpElementMap.get(step.getStepType()).add(msHTTPElement); + } else if (msTestElement instanceof MsScriptElement msScriptElement) { + parseParam.getScriptElements().add(msScriptElement); } msTestElement.setProjectId(step.getProjectId()); msTestElement.setResourceId(step.getResourceId()); @@ -2354,6 +2358,8 @@ public class ApiScenarioService extends MoveNodeService { apiCommonService.setLinkFileInfo(step.getScenarioId(), msTestElement); } apiCommonService.setEnableCommonScriptProcessorInfo(msTestElement); + } if (stepDetail instanceof MsScriptElement msScriptElement) { + apiCommonService.setEnableCommonScriptProcessorInfo(msScriptElement); } return JSON.parseObject(JSON.toJSONString(stepDetail)); }