fix(接口自动化): 修复环境公共变量

This commit is contained in:
fit2-zhao 2020-12-25 13:57:36 +08:00
parent a09c4e3b04
commit 7645026dae
3 changed files with 28 additions and 3 deletions

View File

@ -82,14 +82,14 @@ public class MsScenario extends MsTestElement {
ex.printStackTrace();
}
}
// 场景变量
// 场景变量和环境变量
tree.add(arguments(config));
if (CollectionUtils.isNotEmpty(hashTree)) {
for (MsTestElement el : hashTree) {
el.toHashTree(tree, el.getHashTree(), config);
}
}
}
private Arguments arguments(ParameterConfig config) {

View File

@ -20,13 +20,17 @@ 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.MsTCPSampler;
import io.metersphere.api.dto.definition.request.timer.MsConstantTimer;
import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.api.service.ApiDefinitionService;
import io.metersphere.base.domain.ApiDefinitionWithBLOBs;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.LogUtil;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.http.control.AuthManager;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.collections.HashTree;
import org.apache.jorphan.collections.ListedHashTree;
@ -77,7 +81,7 @@ public abstract class MsTestElement {
@JSONField(ordinal = 8)
private boolean enable = true;
@JSONField(ordinal = 9)
private String refType ;
private String refType;
@JSONField(ordinal = 10)
private LinkedList<MsTestElement> hashTree;
@ -133,6 +137,22 @@ public abstract class MsTestElement {
ex.printStackTrace();
}
}
public Arguments addArguments(ParameterConfig config) {
if (config != null && config.getConfig() != null && config.getConfig().getCommonConfig() != null
&& CollectionUtils.isNotEmpty(config.getConfig().getCommonConfig().getVariables())) {
Arguments arguments = new Arguments();
arguments.setEnabled(true);
arguments.setName(name + "Variables");
arguments.setProperty(TestElement.TEST_CLASS, Arguments.class.getName());
arguments.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("ArgumentsPanel"));
config.getConfig().getCommonConfig().getVariables().stream().filter(KeyValue::isValid).filter(KeyValue::isEnable).forEach(keyValue ->
arguments.addArgument(keyValue.getName(), keyValue.getValue(), "=")
);
return arguments;
}
return null;
}
}

View File

@ -114,6 +114,11 @@ public class MsHTTPSamplerProxy extends MsTestElement {
config.setConfig(JSONObject.parseObject(environment.getConfig(), EnvironmentConfig.class));
}
}
// 添加环境中的公共变量
Arguments arguments = this.addArguments(config);
if (arguments != null) {
tree.add(this.addArguments(config));
}
try {
if (config != null && config.getConfig() != null) {
String url = config.getConfig().getHttpConfig().getProtocol() + "://" + config.getConfig().getHttpConfig().getSocket();