fix(场景自动化): 场景变量设置

This commit is contained in:
fit2-zhao 2021-01-27 11:34:49 +08:00
parent eefb6be647
commit c820538f9f
5 changed files with 98 additions and 75 deletions

View File

@ -20,9 +20,6 @@ import lombok.EqualsAndHashCode;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
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.config.CSVDataSet;
import org.apache.jmeter.config.RandomVariableConfig;
import org.apache.jmeter.modifiers.CounterConfig;
import org.apache.jmeter.save.SaveService; import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement; import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.HashTree;
@ -68,7 +65,8 @@ public class MsScenario extends MsTestElement {
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
ApiScenarioWithBLOBs scenario = apiAutomationService.getApiScenario(this.getId()); ApiScenarioWithBLOBs scenario = apiAutomationService.getApiScenario(this.getId());
JSONObject element = JSON.parseObject(scenario.getScenarioDefinition()); JSONObject element = JSON.parseObject(scenario.getScenarioDefinition());
hashTree = mapper.readValue(element.getString("hashTree"), new TypeReference<LinkedList<MsTestElement>>() {}); hashTree = mapper.readValue(element.getString("hashTree"), new TypeReference<LinkedList<MsTestElement>>() {
});
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
@ -88,9 +86,9 @@ public class MsScenario extends MsTestElement {
} }
// 场景变量和环境变量 // 场景变量和环境变量
tree.add(arguments(config)); tree.add(arguments(config));
addCsvDataSet(tree); this.addCsvDataSet(tree, variables);
addCounter(tree); this.addCounter(tree, variables);
addRandom(tree); this.addRandom(tree, variables);
if (CollectionUtils.isNotEmpty(hashTree)) { if (CollectionUtils.isNotEmpty(hashTree)) {
for (MsTestElement el : hashTree) { for (MsTestElement el : hashTree) {
el.toHashTree(tree, el.getHashTree(), config); el.toHashTree(tree, el.getHashTree(), config);
@ -133,71 +131,5 @@ public class MsScenario extends MsTestElement {
return arguments; 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

@ -22,6 +22,7 @@ import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
import io.metersphere.api.dto.definition.request.sampler.MsJDBCSampler; import io.metersphere.api.dto.definition.request.sampler.MsJDBCSampler;
import io.metersphere.api.dto.definition.request.sampler.MsTCPSampler; import io.metersphere.api.dto.definition.request.sampler.MsTCPSampler;
import io.metersphere.api.dto.definition.request.timer.MsConstantTimer; import io.metersphere.api.dto.definition.request.timer.MsConstantTimer;
import io.metersphere.api.dto.definition.request.variable.ScenarioVariable;
import io.metersphere.api.dto.scenario.KeyValue; import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig; import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
import io.metersphere.api.service.ApiDefinitionService; import io.metersphere.api.service.ApiDefinitionService;
@ -33,6 +34,9 @@ import io.metersphere.commons.utils.LogUtil;
import lombok.Data; import lombok.Data;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.jmeter.config.Arguments; 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.protocol.http.control.AuthManager; import org.apache.jmeter.protocol.http.control.AuthManager;
import org.apache.jmeter.save.SaveService; import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement; import org.apache.jmeter.testelement.TestElement;
@ -42,6 +46,7 @@ import org.apache.jorphan.collections.ListedHashTree;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type") @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type")
@JsonSubTypes({ @JsonSubTypes({
@ -91,6 +96,7 @@ public abstract class MsTestElement {
@JSONField(ordinal = 10) @JSONField(ordinal = 10)
private LinkedList<MsTestElement> hashTree; private LinkedList<MsTestElement> hashTree;
private static final String BODY_FILE_DIR = "/opt/metersphere/data/body";
// 公共环境逐层传递如果自身有环境 以自身引用环境为准否则以公共环境作为请求环境 // 公共环境逐层传递如果自身有环境 以自身引用环境为准否则以公共环境作为请求环境
public void toHashTree(HashTree tree, List<MsTestElement> hashTree, ParameterConfig config) { public void toHashTree(HashTree tree, List<MsTestElement> hashTree, ParameterConfig config) {
for (MsTestElement el : hashTree) { for (MsTestElement el : hashTree) {
@ -166,6 +172,74 @@ public abstract class MsTestElement {
} }
return null; return null;
} }
protected void addCsvDataSet(HashTree tree,List<ScenarioVariable> variables) {
if (CollectionUtils.isNotEmpty(variables)) {
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);
});
}
}
}
protected void addCounter(HashTree tree,List<ScenarioVariable> variables) {
if (CollectionUtils.isNotEmpty(variables)) {
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);
});
}
}
}
protected void addRandom(HashTree tree,List<ScenarioVariable> variables) {
if (CollectionUtils.isNotEmpty(variables)) {
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

@ -60,6 +60,12 @@ public class MsLoopController extends MsTestElement {
config.setStepType("LOOP"); config.setStepType("LOOP");
final HashTree groupTree = controller(tree); final HashTree groupTree = controller(tree);
if (CollectionUtils.isNotEmpty(config.getVariables())) {
this.addCsvDataSet(groupTree, config.getVariables());
this.addCounter(groupTree, config.getVariables());
this.addRandom(groupTree, config.getVariables());
}
// 循环下都增加一个计数器用于结果统计 // 循环下都增加一个计数器用于结果统计
groupTree.add(addCounterConfig()); groupTree.add(addCounterConfig());
// 不打开执行成功后轮询功能则成功后就停止循环 // 不打开执行成功后轮询功能则成功后就停止循环

View File

@ -1,7 +1,7 @@
<template> <template>
<el-form :model="editData" label-position="right" label-width="80px" size="small" ref="form"> <el-form :model="editData" label-position="right" label-width="80px" size="small" ref="form">
<el-form-item :label="$t('api_test.variable_name')" prop="name"> <el-form-item :label="$t('api_test.variable_name')" prop="name">
<el-input v-model="editData.name" :placeholder="$t('api_test.variable_name')"></el-input> <el-input v-model="editData.name" :placeholder="$t('api_test.variable_name')" ref="nameInput"></el-input>
</el-form-item> </el-form-item>
<el-form-item :label="$t('commons.description')" prop="description"> <el-form-item :label="$t('commons.description')" prop="description">
@ -25,6 +25,17 @@
props: { props: {
editData: {}, editData: {},
}, },
created() {
this.$nextTick(() => {
this.$refs.nameInput.focus();
});
},
watch: {
editData() {
this.$refs.nameInput.focus();
}
}
} }
</script> </script>

View File

@ -37,7 +37,7 @@
<div style="margin:20px"> <div style="margin:20px">
<el-button style="margin-right:10px" @click="deleteVariable">{{$t('commons.delete')}}</el-button> <el-button style="margin-right:10px" @click="deleteVariable">{{$t('commons.delete')}}</el-button>
<el-dropdown split-button type="primary" @command="handleClick" placement="top-end"> <el-dropdown split-button type="primary" @command="handleClick" @click="handleClick('CONSTANT')" placement="top-end">
{{$t('commons.add')}} {{$t('commons.add')}}
<el-dropdown-menu slot="dropdown"> <el-dropdown-menu slot="dropdown">
<el-dropdown-item command="CONSTANT">常量</el-dropdown-item> <el-dropdown-item command="CONSTANT">常量</el-dropdown-item>