fix(接口测试): 插件无法读取场景变量

This commit is contained in:
AgAngle 2024-07-04 11:30:49 +08:00 committed by fit2-zhao
parent 0b42f930de
commit 566f4f4939
2 changed files with 41 additions and 25 deletions

View File

@ -1,16 +1,23 @@
package io.metersphere.api.parser.jmeter; package io.metersphere.api.parser.jmeter;
import io.metersphere.api.dto.ApiParamConfig;
import io.metersphere.api.dto.request.MsScenario; import io.metersphere.api.dto.request.MsScenario;
import io.metersphere.api.dto.scenario.ScenarioOtherConfig; import io.metersphere.api.dto.scenario.ScenarioOtherConfig;
import io.metersphere.api.parser.TestElementParser; import io.metersphere.api.parser.TestElementParser;
import io.metersphere.api.utils.JmeterElementConverterRegister; import io.metersphere.api.utils.JmeterElementConverterRegister;
import io.metersphere.plugin.api.dto.ParameterConfig; import io.metersphere.plugin.api.dto.ParameterConfig;
import io.metersphere.plugin.api.spi.AbstractMsProtocolTestElement;
import io.metersphere.plugin.api.spi.AbstractMsTestElement; import io.metersphere.plugin.api.spi.AbstractMsTestElement;
import io.metersphere.project.dto.environment.EnvironmentInfoDTO;
import io.metersphere.project.dto.environment.variables.CommonVariables;
import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.util.LogUtils; import io.metersphere.sdk.util.LogUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.config.Arguments; import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.control.LoopController; import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.modifiers.UserParameters;
import org.apache.jmeter.sampler.DebugSampler; import org.apache.jmeter.sampler.DebugSampler;
import org.apache.jmeter.save.SaveService; import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement; import org.apache.jmeter.testelement.TestElement;
@ -20,6 +27,8 @@ import org.apache.jorphan.collections.HashTree;
import org.apache.jorphan.collections.ListedHashTree; import org.apache.jorphan.collections.ListedHashTree;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Optional;
/** /**
* @Author: jianxing * @Author: jianxing
@ -48,6 +57,16 @@ public class JmeterTestElementParser implements TestElementParser {
final HashTree testPlanTree = hashTree.add(testPlan); final HashTree testPlanTree = hashTree.add(testPlan);
final HashTree groupTree = testPlanTree.add(getThreadGroup(msTestElement)); final HashTree groupTree = testPlanTree.add(getThreadGroup(msTestElement));
if (msTestElement instanceof AbstractMsProtocolTestElement) {
// 如果是单接口执行添加环境变量接口插件也需要支持访问变量
// 场景执行时场景解析器里会处理变量
ApiParamConfig apiParamConfig = (ApiParamConfig) config;
EnvironmentInfoDTO envConfig = apiParamConfig.getEnvConfig(msTestElement.getProjectId());
// 处理环境变量
UserParameters userParameters = getEnvUserParameters(msTestElement.getName(), envConfig);
Optional.ofNullable(userParameters).ifPresent(groupTree::add);
}
// 解析 msTestElement // 解析 msTestElement
JmeterElementConverterRegister.getConverter(msTestElement.getClass()).toHashTree(groupTree, msTestElement, config); JmeterElementConverterRegister.getConverter(msTestElement.getClass()).toHashTree(groupTree, msTestElement, config);
@ -131,4 +150,26 @@ public class JmeterTestElementParser implements TestElementParser {
debugSampler.setProperty("displaySystemProperties", displaySystemProperties); debugSampler.setProperty("displaySystemProperties", displaySystemProperties);
return debugSampler; return debugSampler;
} }
/**
* 添加环境变量
*
* @param name
* @param envInfo
*/
private UserParameters getEnvUserParameters(String name, EnvironmentInfoDTO envInfo) {
if (envInfo == null) {
return null;
}
List<CommonVariables> envVariables = envInfo.getConfig().getCommonVariables();
envVariables = envVariables.stream()
.filter(variable -> BooleanUtils.isTrue(variable.getEnable()) && variable.isValid())
.toList();
if (CollectionUtils.isEmpty(envVariables)) {
return null;
}
return JmeterTestElementParserHelper.getUserParameters(name, envVariables);
}
} }

View File

@ -23,13 +23,11 @@ import io.metersphere.project.dto.environment.host.Host;
import io.metersphere.project.dto.environment.http.HttpConfig; import io.metersphere.project.dto.environment.http.HttpConfig;
import io.metersphere.project.dto.environment.http.HttpConfigPathMatchRule; import io.metersphere.project.dto.environment.http.HttpConfigPathMatchRule;
import io.metersphere.project.dto.environment.http.SelectModule; import io.metersphere.project.dto.environment.http.SelectModule;
import io.metersphere.project.dto.environment.variables.CommonVariables;
import io.metersphere.sdk.util.EnumValidator; import io.metersphere.sdk.util.EnumValidator;
import io.metersphere.sdk.util.LogUtils; import io.metersphere.sdk.util.LogUtils;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.modifiers.UserParameters;
import org.apache.jmeter.protocol.http.control.AuthManager; import org.apache.jmeter.protocol.http.control.AuthManager;
import org.apache.jmeter.protocol.http.control.Authorization; import org.apache.jmeter.protocol.http.control.Authorization;
import org.apache.jmeter.protocol.http.control.DNSCacheManager; import org.apache.jmeter.protocol.http.control.DNSCacheManager;
@ -91,10 +89,6 @@ public class MsHTTPElementConverter extends AbstractJmeterElementConverter<MsHTT
HashTree httpTree = tree.add(sampler); HashTree httpTree = tree.add(sampler);
// 处理环境变量
UserParameters userParameters = getEnvUserParameters(msHTTPElement, envConfig);
Optional.ofNullable(userParameters).ifPresent(httpTree::add);
// 处理请求头 // 处理请求头
HeaderManager httpHeader = getHttpHeader(msHTTPElement, apiParamConfig, httpConfig); HeaderManager httpHeader = getHttpHeader(msHTTPElement, apiParamConfig, httpConfig);
Optional.ofNullable(httpHeader).ifPresent(httpTree::add); Optional.ofNullable(httpHeader).ifPresent(httpTree::add);
@ -188,25 +182,6 @@ public class MsHTTPElementConverter extends AbstractJmeterElementConverter<MsHTT
return authManager; return authManager;
} }
/**
* 添加场景和环境变量
*
* @param msHTTPElement
* @param envInfo
*/
private UserParameters getEnvUserParameters(MsHTTPElement msHTTPElement, EnvironmentInfoDTO envInfo) {
if (envInfo == null) {
return null;
}
List<CommonVariables> envVariables = envInfo.getConfig().getCommonVariables();
if (CollectionUtils.isEmpty(envVariables)) {
return null;
}
return JmeterTestElementParserHelper.getUserParameters(msHTTPElement.getName(), envVariables);
}
private String getPath(MsHTTPElement msHTTPElement, HttpConfig httpConfig) { private String getPath(MsHTTPElement msHTTPElement, HttpConfig httpConfig) {
String url = msHTTPElement.getPath(); String url = msHTTPElement.getPath();
if (httpConfig != null) { if (httpConfig != null) {