feat(接口自动化): 导入jmx 添加HTTP 请求头处理

This commit is contained in:
fit2-zhao 2021-02-23 17:02:49 +08:00
parent 22855ba8cb
commit 80e5b4935d
6 changed files with 64 additions and 16 deletions

View File

@ -58,6 +58,7 @@ import org.apache.jmeter.extractor.RegexExtractor;
import org.apache.jmeter.extractor.XPath2Extractor;
import org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor;
import org.apache.jmeter.modifiers.JSR223PreProcessor;
import org.apache.jmeter.protocol.http.control.HeaderManager;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
import org.apache.jmeter.protocol.http.util.HTTPFileArg;
import org.apache.jmeter.protocol.java.sampler.JSR223Sampler;
@ -77,13 +78,17 @@ import java.util.*;
public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
private final String ENV_NAME = "导入数据环境";
/**
* todo 存放单个请求下的Header 为了和平台对应
*/
private Map<Integer, List<Object>> headerMap = new HashMap<>();
@Override
public ScenarioImport parse(InputStream inputSource, ApiTestImportRequest request) {
try {
Object scriptWrapper = SaveService.loadElement(inputSource);
HashTree testPlan = this.getHashTree(scriptWrapper);
// 优先初始化数据源
// 优先初始化数据源及部分参数
preInitPool(request.getProjectId(), testPlan);
MsScenario scenario = new MsScenario();
@ -126,8 +131,9 @@ public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
return (HashTree) field.get(scriptWrapper);
}
private void convertHttpSampler(MsHTTPSamplerProxy samplerProxy, HTTPSamplerProxy source) {
private void convertHttpSampler(MsHTTPSamplerProxy samplerProxy, Object key) {
try {
HTTPSamplerProxy source = (HTTPSamplerProxy) key;
BeanUtils.copyBean(samplerProxy, source);
if (source != null && source.getHTTPFiles().length > 0) {
samplerProxy.getBody().setBinary(new ArrayList<>());
@ -170,10 +176,23 @@ public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
samplerProxy.setPath(source.getPath());
samplerProxy.setMethod(source.getMethod());
if (source.getUrl() != null) {
// samplerProxy.setUrl(source.getUrl().toString());
samplerProxy.setUrl(source.getUrl().toString());
}
samplerProxy.setId(UUID.randomUUID().toString());
samplerProxy.setType("HTTPSamplerProxy");
// 处理HTTP协议的请求头
if (headerMap.containsKey(key.hashCode())) {
List<KeyValue> keyValues = new LinkedList<>();
headerMap.get(key.hashCode()).forEach(item -> {
HeaderManager headerManager = (HeaderManager) item;
if (headerManager.getHeaders() != null) {
for (int i = 0; i < headerManager.getHeaders().size(); i++) {
keyValues.add(new KeyValue(headerManager.getHeader(i).getName(), headerManager.getHeader(i).getValue()));
}
}
});
samplerProxy.setHeaders(keyValues);
}
} catch (Exception e) {
e.printStackTrace();
}
@ -258,7 +277,7 @@ public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
// 初始化已有数据池
initDataSource(projectId, ENV_NAME);
// 添加当前jmx 中新的数据池
preCreatePool(hashTree);
preCreate(hashTree);
// 更新数据源
ApiTestEnvironmentService environmentService = CommonBeanFactory.getBean(ApiTestEnvironmentService.class);
dataPools.getEnvConfig().setDatabaseConfigs(new ArrayList<>(dataPools.getDataSources().values()));
@ -272,7 +291,7 @@ public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
}
}
private void preCreatePool(HashTree tree) {
private void preCreate(HashTree tree) {
for (Object key : tree.keySet()) {
// JDBC 数据池
if (key instanceof DataSourceElement) {
@ -304,12 +323,29 @@ public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
}
dataPools.getDataSources().put(dataSourceElement.getPropertyAsString("dataSource"), newConfig);
}
} else if (key instanceof HTTPSamplerProxy) {
// 把HTTP 请求下的HeaderManager 取出来
HashTree node = tree.get(key);
if (node != null) {
for (Object nodeKey : node.keySet()) {
if (nodeKey instanceof HeaderManager) {
if (headerMap.containsKey(key.hashCode())) {
headerMap.get(key.hashCode()).add(nodeKey);
} else {
List<Object> objects = new LinkedList<Object>() {{
this.add(nodeKey);
}};
headerMap.put(key.hashCode(), objects);
}
}
}
}
}
// 递归子项
HashTree node = tree.get(key);
if (node != null) {
preCreatePool(node);
preCreate(node);
}
}
}
@ -507,7 +543,7 @@ public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
else if (key instanceof HTTPSamplerProxy) {
elementNode = new MsHTTPSamplerProxy();
((MsHTTPSamplerProxy) elementNode).setBody(new Body());
convertHttpSampler((MsHTTPSamplerProxy) elementNode, (HTTPSamplerProxy) key);
convertHttpSampler((MsHTTPSamplerProxy) elementNode, key);
}
// TCP请求
else if (key instanceof TCPSampler) {
@ -610,6 +646,10 @@ public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
}
// 平台不能识别的Jmeter步骤
else {
// HTTP 请求下的所有HeaderManager已经加到请求中
if (scenario instanceof MsHTTPSamplerProxy && key instanceof HeaderManager) {
continue;
}
elementNode = new MsJmeterElement();
elementNode.setType("JmeterElement");
TestElement testElement = (TestElement) key;

View File

@ -95,7 +95,10 @@ public class MsScenario extends MsTestElement {
config.setVariables(this.variables);
}
// 场景变量和环境变量
tree.add(arguments(config));
Arguments arguments = arguments(config);
if (arguments != null) {
tree.add(arguments);
}
this.addCsvDataSet(tree, variables);
this.addCounter(tree, variables);
this.addRandom(tree, variables);
@ -139,11 +142,11 @@ public class MsScenario extends MsTestElement {
arguments.setProperty(TestElement.TEST_CLASS, Arguments.class.getName());
arguments.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("ArgumentsPanel"));
if (CollectionUtils.isNotEmpty(this.getVariables())) {
variables.stream().filter(ScenarioVariable::isConstantValid).forEach(keyValue ->
this.getVariables().stream().filter(ScenarioVariable::isConstantValid).forEach(keyValue ->
arguments.addArgument(keyValue.getName(), keyValue.getValue(), "=")
);
List<ScenarioVariable> variableList = variables.stream().filter(ScenarioVariable::isListValid).collect(Collectors.toList());
List<ScenarioVariable> variableList = this.getVariables().stream().filter(ScenarioVariable::isListValid).collect(Collectors.toList());
variableList.forEach(item -> {
String[] arrays = item.getValue().split(",");
for (int i = 0; i < arrays.length; i++) {
@ -157,8 +160,10 @@ public class MsScenario extends MsTestElement {
arguments.addArgument(keyValue.getName(), keyValue.getValue(), "=")
);
}
return arguments;
if (arguments.getArguments() != null && arguments.getArguments().size() > 0) {
return arguments;
}
return null;
}

View File

@ -173,6 +173,7 @@ public class MsHTTPSamplerProxy extends MsTestElement {
sampler.setPort(urlObject.getPort());
sampler.setProtocol(urlObject.getProtocol());
String envPath = StringUtils.equals(urlObject.getPath(), "/") ? "" : urlObject.getPath();
sampler.setPath(envPath);
if (CollectionUtils.isNotEmpty(this.getRest()) && this.isRest()) {
envPath = getRestParameters(URLDecoder.decode(envPath, "UTF-8"));
sampler.setPath(envPath);

View File

@ -8,6 +8,7 @@ import lombok.EqualsAndHashCode;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.threads.ThreadGroup;
import org.apache.jorphan.collections.HashTree;
@ -40,7 +41,9 @@ public class MsJmeterElement extends MsTestElement {
} else if (!(scriptWrapper instanceof TestPlan) && !(scriptWrapper instanceof ThreadGroup)) {
elementTree = tree.add(scriptWrapper);
}
if (scriptWrapper instanceof TestElement) {
((TestElement) scriptWrapper).setName(this.getName());
}
if (CollectionUtils.isNotEmpty(hashTree)) {
for (MsTestElement el : hashTree) {
el.toHashTree(elementTree, el.getHashTree(), config);

View File

@ -12,8 +12,7 @@
@select-all="handleSelectAll"
@select="handleSelect"
@header-dragend="headerDragend"
:height="screenHeight"
v-loading="loading">
:height="screenHeight">
<el-table-column type="selection" width="50"/>

View File

@ -31,7 +31,7 @@
<p class="tip">{{ $t('api_test.definition.request.req_param') }} </p>
<ms-api-request-form :isShowEnable="true" :referenced="true" :headers="request.headers " :request="request"
v-if="request.protocol==='HTTP' || request.type==='HTTPSamplerProxy'"/>
<ms-tcp-basis-parameters :request="request" v-if="request.protocol==='TCP'|| request.type==='TCPSampler'"/>
<ms-tcp-basis-parameters :request="request" v-if="request.protocol==='TCP'|| request.type==='TCPSampler'" :showScript="false"/>
<ms-sql-basis-parameters :request="request" v-if="request.protocol==='SQL'|| request.type==='JDBCSampler'"
:showScript="false"/>
<ms-dubbo-basis-parameters :request="request"