feat(接口自动化): 完成场景变量

This commit is contained in:
fit2-zhao 2021-01-08 17:54:09 +08:00
parent fe74a26fd2
commit a57e07bba5
7 changed files with 129 additions and 25 deletions

View File

@ -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<MsTestElement> 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<ScenarioVariable> 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<ScenarioVariable> 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<ScenarioVariable> 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);
});
}
}
}
}

View File

@ -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<KeyValue> variables;
private List<ScenarioVariable> variables;
// 公共Cookie
private boolean enableCookieShare;

View File

@ -1,5 +1,5 @@
package io.metersphere.api.dto.definition.request.variable;
public enum ScenarioVariableType {
public enum EnumVariableType {
CONSTANT, LIST, CSV, COUNTER, RANDOM,
}

View File

@ -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<BodyFile> 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;
}
}

View File

@ -107,7 +107,7 @@
</el-col>
<el-col :span="3" class="ms-col-one ms-font">
<el-link class="head" @click="showScenarioParameters">{{$t('api_test.automation.scenario_total')}}</el-link>
{{this.currentScenario.variables!=undefined?this.currentScenario.variables.length-1: 0}}
{{this.currentScenario.variables!=undefined?this.currentScenario.variables.length: 0}}
</el-col>
<el-col :span="3" class="ms-col-one ms-font">
<el-checkbox v-model="enableCookieShare">共享cookie</el-checkbox>
@ -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;
}
</style>

View File

@ -34,7 +34,7 @@
<span>分隔符</span>
</el-col>
<el-col :span="20">
<el-input v-model="editData.splits" size="small"/>
<el-input v-model="editData.delimiter" size="small"/>
</el-col>
</el-row>

View File

@ -12,12 +12,12 @@
:rows="2" size="small"/>
</el-form-item>
<el-form-item label="最小值" prop="set">
<el-input-number size="small" v-model="editData.minNumber" placeholder="0" :max="1000*10000000" :min="0"/>
<span style="margin: 0px 10px 10px ">最大值</span>
<el-input-number size="small" v-model="editData.maxNumber" placeholder="10" :max="1000*10000000" :min="0"/>
<el-form-item label="最小值" prop="minNumber">
<el-input size="small" v-model="editData.minNumber" placeholder="0"/>
</el-form-item>
<el-form-item label="最大值" prop="maxNumber">
<el-input size="small" v-model="editData.maxNumber" placeholder="10" />
</el-form-item>
<el-form-item label="开始" prop="value">
<el-input v-model="editData.value" placeholder="000产生至少3位数字。user_000输出形式为user_nnn"></el-input>
</el-form-item>