From bfc96474980784abf8dc703adb1a49d249eddb1a Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Sat, 22 Jan 2022 14:11:36 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95)=20?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E4=B8=B4=E7=95=8C=E6=8E=A7=E5=88=B6=E5=99=A8?= =?UTF-8?q?=E9=9A=94=E7=A6=BB=E5=BC=95=E7=94=A8=E5=9C=BA=E6=99=AF=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E5=90=8C=E5=90=8D=E5=8F=98=E9=87=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1009618 --user=赵勇 【接口测试】场景引用变量错误 https://www.tapd.cn/55049933/s/1096157 --- .../dto/definition/request/ElementUtil.java | 18 ++++++++++++ .../dto/definition/request/MsScenario.java | 15 ++++++---- .../MsCriticalSectionController.java | 28 +++++++++++++++++++ .../processors/pre/MsJSR223PreProcessor.java | 22 ++++++++------- .../api/service/ApiAutomationService.java | 10 +++---- 5 files changed, 72 insertions(+), 21 deletions(-) create mode 100644 backend/src/main/java/io/metersphere/api/dto/definition/request/controller/MsCriticalSectionController.java diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java index c545953f7f..b024d60b47 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java @@ -35,6 +35,7 @@ import org.apache.jmeter.config.Arguments; import org.apache.jmeter.config.CSVDataSet; import org.apache.jmeter.config.RandomVariableConfig; import org.apache.jmeter.modifiers.CounterConfig; +import org.apache.jmeter.modifiers.JSR223PreProcessor; import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy; import org.apache.jmeter.save.SaveService; import org.apache.jmeter.testelement.AbstractTestElement; @@ -574,6 +575,23 @@ public class ElementUtil { return resourceId + "_" + ElementUtil.getFullIndexPath(parent, indexPath); } + public static JSR223PreProcessor argumentsToProcessor(Arguments arguments) { + JSR223PreProcessor processor = new JSR223PreProcessor(); + processor.setEnabled(true); + processor.setName("scene variable"); + processor.setProperty(TestElement.TEST_CLASS, JSR223PreProcessor.class.getName()); + processor.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI")); + StringBuffer script = new StringBuffer(); + if (arguments != null) { + for (int i = 0; i < arguments.getArguments().size(); ++i) { + String argValue = arguments.getArgument(i).getValue(); + script.append("vars.put(\"" + arguments.getArgument(i).getName() + "\",\"" + argValue + "\");").append("\n"); + } + processor.setProperty("script", script.toString()); + } + return processor; + } + public static void setBaseParams(AbstractTestElement sampler, MsTestElement parent, ParameterConfig config, String id, String indexPath) { sampler.setProperty("MS-ID", id); sampler.setProperty("MS-RESOURCE-ID", ElementUtil.getResourceId(id, config, parent, indexPath)); diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java index dcd4fe4b24..24d158ba16 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java @@ -8,6 +8,7 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import io.metersphere.api.dto.EnvironmentType; +import io.metersphere.api.dto.definition.request.controller.MsCriticalSectionController; import io.metersphere.api.dto.definition.request.variable.ScenarioVariable; import io.metersphere.api.dto.mockconfig.MockConfigStaticData; import io.metersphere.api.dto.scenario.KeyValue; @@ -158,14 +159,16 @@ public class MsScenario extends MsTestElement { if (CollectionUtils.isNotEmpty(this.getVariables())) { config.setVariables(this.variables); } + final HashTree scenarioTree = MsCriticalSectionController.createHashTree(tree, this.getName()); // 场景变量和环境变量 Arguments arguments = arguments(config); if (arguments != null) { - tree.add(ParameterConfig.valueSupposeMock(arguments)); + Arguments valueSupposeMock = ParameterConfig.valueSupposeMock(arguments); + scenarioTree.add(ElementUtil.argumentsToProcessor(valueSupposeMock)); } - ElementUtil.addCsvDataSet(tree, variables, config, "shareMode.group"); - ElementUtil.addCounter(tree, variables, false); - ElementUtil.addRandom(tree, variables); + ElementUtil.addCsvDataSet(scenarioTree, variables, config, "shareMode.group"); + ElementUtil.addCounter(scenarioTree, variables, false); + ElementUtil.addRandom(scenarioTree, variables); if (CollectionUtils.isNotEmpty(this.headers)) { config.setHeaders(this.headers); } @@ -205,9 +208,9 @@ public class MsScenario extends MsTestElement { el.setParent(this); el.setMockEnvironment(this.isMockEnvironment()); if (this.isEnvironmentEnable()) { - el.toHashTree(tree, el.getHashTree(), newConfig); + el.toHashTree(scenarioTree, el.getHashTree(), newConfig); } else { - el.toHashTree(tree, el.getHashTree(), config); + el.toHashTree(scenarioTree, el.getHashTree(), config); } } } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/controller/MsCriticalSectionController.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/controller/MsCriticalSectionController.java new file mode 100644 index 0000000000..92a98afc5e --- /dev/null +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/controller/MsCriticalSectionController.java @@ -0,0 +1,28 @@ +package io.metersphere.api.dto.definition.request.controller; + +import io.metersphere.plugin.core.MsParameter; +import io.metersphere.plugin.core.MsTestElement; +import org.apache.commons.lang3.StringUtils; +import org.apache.jmeter.control.CriticalSectionController; +import org.apache.jmeter.testelement.TestElement; +import org.apache.jorphan.collections.HashTree; + +import java.util.List; + +public class MsCriticalSectionController extends MsTestElement { + + @Override + public void toHashTree(HashTree tree, List hashTree, MsParameter msParameter) { + + } + + public static HashTree createHashTree(HashTree tree, String name) { + CriticalSectionController criticalSectionController = new CriticalSectionController(); + criticalSectionController.setName(StringUtils.isNotEmpty(name) ? "Csc_" + name : "Scenario Critical Section Controller"); + criticalSectionController.setLockName("global_lock"); + criticalSectionController.setEnabled(true); + criticalSectionController.setProperty(TestElement.TEST_CLASS, CriticalSectionController.class.getName()); + criticalSectionController.setProperty(TestElement.GUI_CLASS, "CriticalSectionControllerGui"); + return tree.add(criticalSectionController); + } +} diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/pre/MsJSR223PreProcessor.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/pre/MsJSR223PreProcessor.java index 484e9542f8..51951b84c6 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/pre/MsJSR223PreProcessor.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/pre/MsJSR223PreProcessor.java @@ -8,6 +8,7 @@ import io.metersphere.api.dto.scenario.environment.EnvironmentConfig; import io.metersphere.api.dto.shell.filter.ScriptFilter; import io.metersphere.plugin.core.MsParameter; import io.metersphere.plugin.core.MsTestElement; +import io.metersphere.utils.JMeterVars; import lombok.Data; import lombok.EqualsAndHashCode; import org.apache.commons.collections.CollectionUtils; @@ -35,8 +36,12 @@ public class MsJSR223PreProcessor extends MsTestElement { @Override public void toHashTree(HashTree tree, List hashTree, MsParameter msParameter) { - ScriptFilter.verify(this.getScriptLanguage(), this.getName(), script); ParameterConfig config = (ParameterConfig) msParameter; + // 非导出操作,且不是启用状态则跳过执行 + if (!config.isOperating() && !this.isEnable()) { + return; + } + ScriptFilter.verify(this.getScriptLanguage(), this.getName(), script); if (StringUtils.isEmpty(this.getEnvironmentId())) { if (config.getConfig() != null) { if (config.getProjectId() != null) { @@ -54,19 +59,16 @@ public class MsJSR223PreProcessor extends MsTestElement { } } } - //替换Metersphere环境变量 - script = StringUtils.replace(script, RunningParamKeys.API_ENVIRONMENT_ID, "\"" + RunningParamKeys.RUNNING_PARAMS_PREFIX + this.getEnvironmentId() + ".\""); - - if(config.isOperating()){ - if (script.startsWith("io.metersphere.utils.JMeterVars.addVars")) { + //替换环境变量 + if (StringUtils.isNotEmpty(script)) { + script = StringUtils.replace(script, RunningParamKeys.API_ENVIRONMENT_ID, "\"" + RunningParamKeys.RUNNING_PARAMS_PREFIX + this.getEnvironmentId() + ".\""); + } + if (config.isOperating()) { + if (script.startsWith(JMeterVars.class.getCanonicalName())) { return; } } - // 非导出操作,且不是启用状态则跳过执行 - if (!config.isOperating() && !this.isEnable()) { - return; - } final HashTree jsr223PreTree = tree.add(getJSR223PreProcessor()); if (CollectionUtils.isNotEmpty(hashTree)) { hashTree.forEach(el -> { diff --git a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java index 7af3fe9dd4..936c185501 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -687,11 +687,11 @@ public class ApiAutomationService { this.add("TCPSampler"); }}; - private void setElement(JSONObject element, Integer num, boolean enable, String versionName, boolean versionEnable) { + private void setElement(JSONObject element, Integer num, Boolean enable, String versionName, Boolean versionEnable) { element.put("num", num); - element.put("enable", enable); + element.put("enable", enable == null ? false : enable); element.put("versionName", versionName); - element.put("versionEnable", versionEnable); + element.put("versionEnable", versionEnable == null ? false : versionEnable); } private JSONObject setRequest(JSONObject element) { @@ -1470,7 +1470,7 @@ public class ApiAutomationService { if (StringUtils.isNotEmpty(item.getScenarioDefinition())) { String jmx = generateJmx(item); if (StringUtils.isNotEmpty(jmx)) { - ApiScenrioExportJmx scenrioExportJmx = new ApiScenrioExportJmx(item.getName(), apiTestService.updateJmxString(jmx,item.getProjectId()).getXml()); + ApiScenrioExportJmx scenrioExportJmx = new ApiScenrioExportJmx(item.getName(), apiTestService.updateJmxString(jmx, item.getProjectId()).getXml()); JmxInfoDTO dto = apiTestService.updateJmxString(jmx, item.getProjectId()); scenrioExportJmx.setId(item.getId()); scenrioExportJmx.setVersion(item.getVersion()); @@ -1887,7 +1887,7 @@ public class ApiAutomationService { String testName = item.getName(); MsTestPlan testPlan = new MsTestPlan(); testPlan.setHashTree(new LinkedList<>()); - JmxInfoDTO dto = apiTestService.updateJmxString(generateJmx(item),item.getProjectId()); + JmxInfoDTO dto = apiTestService.updateJmxString(generateJmx(item), item.getProjectId()); String name = item.getName() + ".jmx"; dto.setId(item.getId()); dto.setName(name);