diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/ExtractPostProcessorConverter.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/ExtractPostProcessorConverter.java index 6ab4524852..bdde1f4354 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/ExtractPostProcessorConverter.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/processor/ExtractPostProcessorConverter.java @@ -1,11 +1,24 @@ package io.metersphere.api.parser.jmeter.processor; +import io.metersphere.api.dto.ApiParamConfig; +import io.metersphere.api.parser.jmeter.constants.JmeterAlias; +import io.metersphere.api.parser.jmeter.constants.JmeterProperty; +import io.metersphere.api.parser.jmeter.processor.extract.ExtractConverterFactory; +import io.metersphere.plugin.api.constants.ElementProperty; +import io.metersphere.plugin.api.dto.ParameterConfig; import io.metersphere.project.api.processor.ExtractPostProcessor; import io.metersphere.project.api.processor.extract.MsExtract; -import io.metersphere.api.parser.jmeter.processor.extract.ExtractConverterFactory; -import io.metersphere.plugin.api.dto.ParameterConfig; +import io.metersphere.project.constants.ScriptLanguageType; +import io.metersphere.project.dto.environment.EnvironmentInfoDTO; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.jmeter.extractor.JSR223PostProcessor; +import org.apache.jmeter.save.SaveService; +import org.apache.jmeter.testelement.TestElement; import org.apache.jorphan.collections.HashTree; +import java.util.List; + /** * @Author: jianxing @@ -27,5 +40,32 @@ public class ExtractPostProcessorConverter extends MsProcessorConverter list = processor.getExtractors().stream() + .filter(extract -> StringUtils.equals(extract.getVariableType(), MsExtract.MsExtractType.ENVIRONMENT.name()) + && extract.isValid() && extract.getEnable()).toList(); + if (CollectionUtils.isNotEmpty(list)) { + //需要生成一个后置脚本 + ApiParamConfig apiParamConfig = (ApiParamConfig) config; + EnvironmentInfoDTO envConfig = apiParamConfig.getEnvConfig(processor.getProjectId()); + String envId = envConfig.getId(); + JSR223PostProcessor jsr223PostProcessor = new JSR223PostProcessor(); + jsr223PostProcessor.setName("Set Environment Variable"); + jsr223PostProcessor.setProperty(TestElement.TEST_CLASS, jsr223PostProcessor.getClass().getSimpleName()); + jsr223PostProcessor.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass(JmeterAlias.TEST_BEAN_GUI)); + jsr223PostProcessor.setProperty(ElementProperty.PROJECT_ID.name(), processor.getProjectId()); + jsr223PostProcessor.setProperty(JmeterProperty.SCRIPT_LANGUAGE, ScriptLanguageType.BEANSHELL.name().toLowerCase()); + StringBuilder scriptBuilder = new StringBuilder(); + list.forEach(extract -> { + String script = """ + vars.put("MS.ENV.%s.%s","${%s}"); + vars.put("%s","${%s}"); + """; + scriptBuilder.append(String.format(script, envId, extract.getVariableName(), extract.getVariableName(), extract.getVariableName(), extract.getVariableName())); + }); + jsr223PostProcessor.setProperty(JmeterProperty.SCRIPT, scriptBuilder.toString()); + hashTree.add(jsr223PostProcessor); + } } }