diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/ApiScenarioParamConfig.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/ApiScenarioParamConfig.java index 23c14f00e1..0b8ac983cf 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/ApiScenarioParamConfig.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/ApiScenarioParamConfig.java @@ -1,5 +1,6 @@ package io.metersphere.api.dto; +import io.metersphere.api.dto.scenario.ScenarioConfig; import io.metersphere.plugin.api.spi.AbstractMsTestElement; import io.metersphere.project.dto.environment.EnvironmentInfoDTO; import lombok.Data; @@ -19,6 +20,10 @@ public class ApiScenarioParamConfig extends ApiParamConfig { * value 为环境信息 */ private Map projectEnvMap; + /** + * 当前场景的场景配置 + */ + private ScenarioConfig rootScenarioConfig; /** * 是否为环境组 * 是则使用 projectEnvMap diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsCommonElementConverter.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsCommonElementConverter.java index a2d7b81edd..856139ebd1 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsCommonElementConverter.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsCommonElementConverter.java @@ -107,8 +107,8 @@ public class MsCommonElementConverter extends AbstractJmeterElementConverter commonVariables = getCommonVariables(msScenario.getScenarioConfig()); + + if (!isRootScenario(msScenario.getRefType())) { + // 不是根场景,从步骤配置中获取是否使用场景变量 + ScenarioStepConfig scenarioStepConfig = msScenario.getScenarioStepConfig(); + if (scenarioStepConfig != null) { + if (BooleanUtils.isTrue(scenarioStepConfig.getUseOriginScenarioParam())) { + if (BooleanUtils.isNotTrue(scenarioStepConfig.getUseOriginScenarioParamPreferential())) { + // 如果不是源场景变量优先,则跟根场景比较,去掉重名的变量 + Set rootVariableKeys = getCommonVariables(config.getRootScenarioConfig()) + .stream() + .filter(CommonVariables::getEnable) + .filter(variable -> variable.getEnable() && (variable.isListValid() ? variable.isListValid() : variable.isConstantValid())) + .map(variable -> variable.getParamType() + variable.getKey()) + .collect(Collectors.toSet()); + + commonVariables = commonVariables.stream().filter(variable -> { + if (rootVariableKeys.contains(variable.getParamType() + variable.getKey())) { + // 当前场景变量优先,过滤掉当前场景变量中存在的变量 + return false; + } + return true; + }).collect(Collectors.toList()); + } + } else { + // 如果没有启用源场景环境,则置空 + commonVariables = List.of(); + } + } } - ScenarioConfig scenarioConfig = msScenario.getScenarioConfig(); - ScenarioVariable scenarioVariable = scenarioConfig == null ? new ScenarioVariable() : scenarioConfig.getVariable(); - scenarioVariable = scenarioVariable == null ? new ScenarioVariable() : scenarioVariable; - List commonVariables = scenarioVariable.getCommonVariables(); List envCommonVariables = List.of(); - if (needParseEnv(msScenario) && envInfo.getConfig() != null) { + if (envInfo != null && needParseEnv(msScenario) && envInfo.getConfig() != null) { // 获取环境变量 envCommonVariables = envInfo.getConfig().getCommonVariables(); // 获取后,将环境变量置空,避免请求重复设置 @@ -112,8 +138,16 @@ public class MsScenarioConverter extends AbstractJmeterElementConverter getCommonVariables(ScenarioConfig scenarioConfig) { + ScenarioVariable scenarioVariable = scenarioConfig == null ? new ScenarioVariable() : scenarioConfig.getVariable(); + scenarioVariable = scenarioVariable == null ? new ScenarioVariable() : scenarioVariable; + List commonVariables = scenarioVariable.getCommonVariables(); + return commonVariables; + } + /** * 合并环境变量和场景变量 + * * @param scenarioVariables * @param envCommonVariables * @param filter @@ -216,6 +250,7 @@ public class MsScenarioConverter extends AbstractJmeterElementConverter projectEnvMap = msScenario.getProjectEnvMap(); - childConfig.setProjectEnvMap(projectEnvMap); + enableConfig.setProjectEnvMap(projectEnvMap); } else { // 设置环境信息 EnvironmentInfoDTO environmentInfo = msScenario.getEnvironmentInfo(); - childConfig.setEnvConfig(environmentInfo); + enableConfig.setEnvConfig(environmentInfo); } } ScenarioConfig scenarioConfig = msScenario.getScenarioConfig(); if (scenarioConfig != null) { // 设置是否使用全局cookie - childConfig.setEnableGlobalCookie(scenarioConfig.getOtherConfig().getEnableCookieShare()); + enableConfig.setEnableGlobalCookie(scenarioConfig.getOtherConfig().getEnableCookieShare()); } - return childConfig; + return enableConfig; } private CookieManager getCookieManager() { 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 71e29f4878..492ed37885 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 @@ -1542,6 +1542,7 @@ public class ApiScenarioService extends MoveNodeService { parseConfig.setTestElementClassPluginIdMap(apiPluginService.getTestElementPluginMap()); parseConfig.setTestElementClassProtocalMap(apiPluginService.getTestElementProtocolMap()); parseConfig.setGrouped(request.getGrouped()); + parseConfig.setRootScenarioConfig(request.getScenarioConfig()); if (BooleanUtils.isTrue(request.getGrouped())) { // 设置环境组 map parseConfig.setProjectEnvMap(getProjectEnvMap(scenarioParseEnvInfo, request.getEnvironmentId()));