From 15cb45562017438728ac7d3e4828339e26fc1903 Mon Sep 17 00:00:00 2001 From: AgAngle <1323481023@qq.com> Date: Thu, 4 Jul 2024 11:30:49 +0800 Subject: [PATCH] =?UTF-8?q?fxi(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=E6=97=A0=E6=B3=95=E8=AF=BB=E5=8F=96=E5=9C=BA?= =?UTF-8?q?=E6=99=AF=E5=8F=98=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jmeter/JmeterTestElementParser.java | 41 +++++++++++++++++++ .../parser/jmeter/MsHTTPElementConverter.java | 25 ----------- 2 files changed, 41 insertions(+), 25 deletions(-) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/JmeterTestElementParser.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/JmeterTestElementParser.java index 1399985086..01bc0eb77f 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/JmeterTestElementParser.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/JmeterTestElementParser.java @@ -1,16 +1,23 @@ package io.metersphere.api.parser.jmeter; +import io.metersphere.api.dto.ApiParamConfig; import io.metersphere.api.dto.request.MsScenario; import io.metersphere.api.dto.scenario.ScenarioOtherConfig; import io.metersphere.api.parser.TestElementParser; import io.metersphere.api.utils.JmeterElementConverterRegister; import io.metersphere.plugin.api.dto.ParameterConfig; +import io.metersphere.plugin.api.spi.AbstractMsProtocolTestElement; 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.util.LogUtils; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.StringUtils; import org.apache.jmeter.config.Arguments; import org.apache.jmeter.control.LoopController; +import org.apache.jmeter.modifiers.UserParameters; import org.apache.jmeter.sampler.DebugSampler; import org.apache.jmeter.save.SaveService; import org.apache.jmeter.testelement.TestElement; @@ -20,6 +27,8 @@ import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.ListedHashTree; import java.io.ByteArrayOutputStream; +import java.util.List; +import java.util.Optional; /** * @Author: jianxing @@ -48,6 +57,16 @@ public class JmeterTestElementParser implements TestElementParser { final HashTree testPlanTree = hashTree.add(testPlan); 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 JmeterElementConverterRegister.getConverter(msTestElement.getClass()).toHashTree(groupTree, msTestElement, config); @@ -131,4 +150,26 @@ public class JmeterTestElementParser implements TestElementParser { debugSampler.setProperty("displaySystemProperties", displaySystemProperties); return debugSampler; } + + /** + * 添加环境变量 + * + * @param name + * @param envInfo + */ + private UserParameters getEnvUserParameters(String name, EnvironmentInfoDTO envInfo) { + if (envInfo == null) { + return null; + } + + List 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); + } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsHTTPElementConverter.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsHTTPElementConverter.java index b52e1d61ac..55ee65eb91 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsHTTPElementConverter.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsHTTPElementConverter.java @@ -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.HttpConfigPathMatchRule; 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.LogUtils; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.BooleanUtils; 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.Authorization; import org.apache.jmeter.protocol.http.control.DNSCacheManager; @@ -91,10 +89,6 @@ public class MsHTTPElementConverter extends AbstractJmeterElementConverter envVariables = envInfo.getConfig().getCommonVariables(); - if (CollectionUtils.isEmpty(envVariables)) { - return null; - } - - return JmeterTestElementParserHelper.getUserParameters(msHTTPElement.getName(), envVariables); - } - private String getPath(MsHTTPElement msHTTPElement, HttpConfig httpConfig) { String url = msHTTPElement.getPath(); if (httpConfig != null) {