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 c04dc5ecbf..5d576dece2 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 @@ -20,12 +20,16 @@ import lombok.EqualsAndHashCode; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; 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.save.SaveService; import org.apache.jmeter.testelement.TestElement; import org.apache.jorphan.collections.HashTree; import java.util.LinkedList; import java.util.List; +import java.util.stream.Collectors; @Data @EqualsAndHashCode(callSuper = true) @@ -48,6 +52,8 @@ public class MsScenario extends MsTestElement { @JSONField(ordinal = 24) private boolean enableCookieShare; + private static final String BODY_FILE_DIR = "/opt/metersphere/data/body"; + public void toHashTree(HashTree tree, List hashTree, ParameterConfig config) { if (!this.isEnable()) { return; @@ -61,7 +67,7 @@ public class MsScenario extends MsTestElement { } } if (CollectionUtils.isNotEmpty(this.getVariables())) { - //config.setVariables(this.variables); + config.setVariables(this.variables); } if (this.getReferenced() != null && this.getReferenced().equals("Deleted")) { return; @@ -85,7 +91,9 @@ public class MsScenario extends MsTestElement { } // 场景变量和环境变量 tree.add(arguments(config)); - + addCsvDataSet(tree); + addCounter(tree); + addRandom(tree); if (CollectionUtils.isNotEmpty(hashTree)) { for (MsTestElement el : hashTree) { el.toHashTree(tree, el.getHashTree(), config); @@ -106,11 +114,11 @@ public class MsScenario extends MsTestElement { arguments.setName(name + "Variables"); arguments.setProperty(TestElement.TEST_CLASS, Arguments.class.getName()); arguments.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("ArgumentsPanel")); -// if (CollectionUtils.isNotEmpty(this.getVariables())) { -// variables.stream().filter(KeyValue::isValid).filter(KeyValue::isEnable).forEach(keyValue -> -// arguments.addArgument(keyValue.getName(), keyValue.getValue(), "=") -// ); -// } + if (CollectionUtils.isNotEmpty(this.getVariables())) { + variables.stream().filter(ScenarioVariable::isConstantValid).forEach(keyValue -> + arguments.addArgument(keyValue.getName(), keyValue.getValue(), "=") + ); + } if (config != null && config.getConfig() != null && config.getConfig().getCommonConfig() != null && CollectionUtils.isNotEmpty(config.getConfig().getCommonConfig().getVariables())) { config.getConfig().getCommonConfig().getVariables().stream().filter(KeyValue::isValid).filter(KeyValue::isEnable).forEach(keyValue -> @@ -119,4 +127,72 @@ public class MsScenario extends MsTestElement { } return arguments; } + + private void addCsvDataSet(HashTree tree) { + if (CollectionUtils.isNotEmpty(this.getVariables())) { + List list = variables.stream().filter(ScenarioVariable::isCSVValid).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(list)) { + list.forEach(item -> { + CSVDataSet csvDataSet = new CSVDataSet(); + csvDataSet.setEnabled(true); + csvDataSet.setProperty(TestElement.TEST_CLASS, CSVDataSet.class.getName()); + csvDataSet.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI")); + csvDataSet.setName(item.getName()); + csvDataSet.setProperty("fileEncoding", item.getEncoding()); + csvDataSet.setProperty("variableNames", item.getName()); + if (CollectionUtils.isNotEmpty(item.getFiles())) { + csvDataSet.setProperty("filename", BODY_FILE_DIR + "/" + item.getFiles().get(0).getId() + "_" + item.getFiles().get(0).getName()); + } + csvDataSet.setIgnoreFirstLine(false); + csvDataSet.setProperty("delimiter", item.getDelimiter()); + csvDataSet.setComment(item.getDescription()); + tree.add(csvDataSet); + }); + } + } + } + + private void addCounter(HashTree tree) { + if (CollectionUtils.isNotEmpty(this.getVariables())) { + List list = variables.stream().filter(ScenarioVariable::isCounterValid).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(list)) { + list.forEach(item -> { + CounterConfig counterConfig = new CounterConfig(); + counterConfig.setEnabled(true); + counterConfig.setProperty(TestElement.TEST_CLASS, CounterConfig.class.getName()); + counterConfig.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("CounterConfigGui")); + counterConfig.setName(item.getName()); + counterConfig.setStart(item.getStartNumber()); + counterConfig.setEnd(item.getEndNumber()); + counterConfig.setVarName(item.getName()); + counterConfig.setIncrement(item.getIncrement()); + counterConfig.setFormat(item.getValue()); + counterConfig.setComment(item.getDescription()); + tree.add(counterConfig); + }); + } + } + } + + private void addRandom(HashTree tree) { + if (CollectionUtils.isNotEmpty(this.getVariables())) { + List list = variables.stream().filter(ScenarioVariable::isRandom).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(list)) { + list.forEach(item -> { + RandomVariableConfig randomVariableConfig = new RandomVariableConfig(); + randomVariableConfig.setEnabled(true); + randomVariableConfig.setProperty(TestElement.TEST_CLASS, RandomVariableConfig.class.getName()); + randomVariableConfig.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI")); + randomVariableConfig.setName(item.getName()); + randomVariableConfig.setProperty("variableName", item.getName()); + randomVariableConfig.setProperty("outputFormat", item.getValue()); + randomVariableConfig.setProperty("minimumValue", item.getMinNumber()); + randomVariableConfig.setProperty("maximumValue", item.getMaxNumber()); + randomVariableConfig.setComment(item.getDescription()); + tree.add(randomVariableConfig); + }); + } + } + } + } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/ParameterConfig.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/ParameterConfig.java index fc938e4dbd..9f790cc7a9 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/ParameterConfig.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/ParameterConfig.java @@ -1,6 +1,6 @@ package io.metersphere.api.dto.definition.request; -import io.metersphere.api.dto.scenario.KeyValue; +import io.metersphere.api.dto.definition.request.variable.ScenarioVariable; import io.metersphere.api.dto.scenario.environment.EnvironmentConfig; import lombok.Data; @@ -11,7 +11,7 @@ public class ParameterConfig { // 环境配置 private EnvironmentConfig config; // 公共场景参数 - private List variables; + private List variables; // 公共Cookie private boolean enableCookieShare; diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/variable/EnumVariableType.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/variable/EnumVariableType.java index 3e301b45e1..062b67a356 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/variable/EnumVariableType.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/variable/EnumVariableType.java @@ -1,5 +1,5 @@ package io.metersphere.api.dto.definition.request.variable; -public enum ScenarioVariableType { +public enum EnumVariableType { CONSTANT, LIST, CSV, COUNTER, RANDOM, } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/variable/ScenarioVariable.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/variable/ScenarioVariable.java index 7447f3cf0e..b5fc7d4ee7 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/variable/ScenarioVariable.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/variable/ScenarioVariable.java @@ -2,6 +2,7 @@ package io.metersphere.api.dto.definition.request.variable; import io.metersphere.api.dto.scenario.request.BodyFile; import lombok.Data; +import org.apache.commons.lang3.StringUtils; import java.util.List; @@ -10,20 +11,48 @@ public class ScenarioVariable { // CONSTANT LIST CSV COUNTER RANDOM private String type; + private String id; private String name; // 常量值,列表值[] ,计数器输出格式,随机数输出格式 private String value; private String description; // csv private List files; - private String splits; + private String delimiter; private String encoding; // counter private int startNumber; private int endNumber; private int increment; // random - private int minNumber; - private int maxNumber; + private String minNumber; + private String maxNumber; + public boolean isConstantValid() { + if ((StringUtils.equals(this.type, EnumVariableType.CONSTANT.name()) || StringUtils.equals(this.type, EnumVariableType.LIST.name())) && StringUtils.isNotEmpty(name) && StringUtils.isNotEmpty(value)) { + return true; + } + return false; + } + + public boolean isCSVValid() { + if (StringUtils.equals(this.type, EnumVariableType.CSV.name()) && StringUtils.isNotEmpty(name)) { + return true; + } + return false; + } + + public boolean isCounterValid() { + if (StringUtils.equals(this.type, EnumVariableType.COUNTER.name()) && StringUtils.isNotEmpty(name)) { + return true; + } + return false; + } + + public boolean isRandom() { + if (StringUtils.equals(this.type, EnumVariableType.RANDOM.name()) && StringUtils.isNotEmpty(name)) { + return true; + } + return false; + } } diff --git a/frontend/src/business/components/api/automation/scenario/EditApiScenario.vue b/frontend/src/business/components/api/automation/scenario/EditApiScenario.vue index 3919b16c6c..54a8f92a74 100644 --- a/frontend/src/business/components/api/automation/scenario/EditApiScenario.vue +++ b/frontend/src/business/components/api/automation/scenario/EditApiScenario.vue @@ -107,7 +107,7 @@ {{$t('api_test.automation.scenario_total')}} - :{{this.currentScenario.variables!=undefined?this.currentScenario.variables.length-1: 0}} + :{{this.currentScenario.variables!=undefined?this.currentScenario.variables.length: 0}} 共享cookie @@ -941,11 +941,6 @@ background-color: white; } - .ms-opt-btn { - float: right; - margin-right: 20px; - } - .ms-debug-div { border: 1px #DCDFE6 solid; border-radius: 4px; @@ -1029,4 +1024,8 @@ font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", Arial, sans-serif; font-size: 13px; } + .ms-opt-btn { + position: fixed; + right: 50px; + } diff --git a/frontend/src/business/components/api/automation/scenario/variable/EditCsv.vue b/frontend/src/business/components/api/automation/scenario/variable/EditCsv.vue index 269022f9a2..54529a99fc 100644 --- a/frontend/src/business/components/api/automation/scenario/variable/EditCsv.vue +++ b/frontend/src/business/components/api/automation/scenario/variable/EditCsv.vue @@ -34,7 +34,7 @@ 分隔符 - + diff --git a/frontend/src/business/components/api/automation/scenario/variable/EditRandom.vue b/frontend/src/business/components/api/automation/scenario/variable/EditRandom.vue index 7f80648514..d93d29ad16 100644 --- a/frontend/src/business/components/api/automation/scenario/variable/EditRandom.vue +++ b/frontend/src/business/components/api/automation/scenario/variable/EditRandom.vue @@ -12,12 +12,12 @@ :rows="2" size="small"/> - - - 最大值 - + + + + + -