fix(接口测试): 修复提取环境变量未更新环境的缺陷

This commit is contained in:
wxg0103 2024-04-03 12:41:33 +08:00 committed by Craftsman
parent 05115b43ac
commit 2beebe8fbc
1 changed files with 42 additions and 2 deletions

View File

@ -1,11 +1,24 @@
package io.metersphere.api.parser.jmeter.processor; 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.ExtractPostProcessor;
import io.metersphere.project.api.processor.extract.MsExtract; import io.metersphere.project.api.processor.extract.MsExtract;
import io.metersphere.api.parser.jmeter.processor.extract.ExtractConverterFactory; import io.metersphere.project.constants.ScriptLanguageType;
import io.metersphere.plugin.api.dto.ParameterConfig; 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 org.apache.jorphan.collections.HashTree;
import java.util.List;
/** /**
* @Author: jianxing * @Author: jianxing
@ -27,5 +40,32 @@ public class ExtractPostProcessorConverter extends MsProcessorConverter<ExtractP
ExtractConverterFactory.getConverter(extract.getClass()) ExtractConverterFactory.getConverter(extract.getClass())
.parse(hashTree, extract, config); .parse(hashTree, extract, config);
}); });
//提取参数应用到环境变量
List<MsExtract> 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);
}
} }
} }