fix(接口自动化): 执行场景非变量的参数不做处理

This commit is contained in:
fit2-zhao 2020-12-28 19:36:58 +08:00
parent bee13b6b94
commit f5ea7d25b9
7 changed files with 267 additions and 8 deletions

View File

@ -101,7 +101,6 @@ public abstract class MsTestElement {
public String getJmx(HashTree hashTree) {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
SaveService.saveTree(hashTree, baos);
System.out.print(baos.toString());
return baos.toString();
} catch (Exception e) {
e.printStackTrace();

View File

@ -158,12 +158,13 @@ public class MsHTTPSamplerProxy extends MsTestElement {
sampler.setDomain(URLDecoder.decode(urlObject.getHost(), "UTF-8"));
sampler.setPort(urlObject.getPort());
sampler.setProtocol(urlObject.getProtocol());
String envPath = StringUtils.equals(urlObject.getPath(), "/") ? "" : urlObject.getPath();
if (CollectionUtils.isNotEmpty(this.getRest()) && this.isRest()) {
sampler.setPath(getRestParameters(URLDecoder.decode(urlObject.getPath(), "UTF-8")));
envPath = getRestParameters(URLDecoder.decode(envPath, "UTF-8"));
sampler.setPath(envPath);
}
if (CollectionUtils.isNotEmpty(this.getArguments())) {
sampler.setPath(getPostQueryParameters(URLDecoder.decode(urlObject.getPath(), "UTF-8")));
sampler.setPath(getPostQueryParameters(URLDecoder.decode(envPath, "UTF-8")));
}
}
} catch (Exception e) {

View File

@ -66,7 +66,7 @@ public class JMeterService {
}
}
private HashTree getHashTree(Object scriptWrapper) throws Exception {
public static HashTree getHashTree(Object scriptWrapper) throws Exception {
Field field = scriptWrapper.getClass().getDeclaredField("testPlan");
field.setAccessible(true);
return (HashTree) field.get(scriptWrapper);

View File

@ -166,6 +166,16 @@ public class JmeterDocumentParser {
u += k + "=" + v;
return u;
});
//rest参数处理
if (url.contains("@")) {
String vars[] = url.split("@");
for (String item : vars) {
if (item.endsWith("/")) {
item = item.substring(0, item.length() - 1);
}
url = url.replace("@" + item, ScriptEngineUtils.calculate("@" + item));
}
}
ele.setTextContent(url + ((params != null && !params.equals("?")) ? params : ""));
break;
case "Argument.value":

View File

@ -13,6 +13,7 @@ import io.metersphere.api.dto.definition.request.*;
import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
import io.metersphere.api.jmeter.JMeterService;
import io.metersphere.api.parse.old.JmeterDocumentParser;
import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.ApiScenarioMapper;
import io.metersphere.base.mapper.ApiScenarioReportMapper;
@ -38,6 +39,7 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.jmeter.save.SaveService;
import org.apache.jorphan.collections.HashTree;
import org.apache.jorphan.collections.ListedHashTree;
import org.springframework.stereotype.Service;
@ -45,6 +47,8 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;
@ -356,7 +360,18 @@ public class ApiAutomationService {
runMode = ApiRunMode.SCENARIO_PLAN.name();
}
// 调用执行方法
jMeterService.runDefinition(request.getId(), jmeterHashTree, request.getReportId(), runMode);
String jmx = testPlan.getJmx(jmeterHashTree);
byte[] bytes = JmeterDocumentParser.parse(jmx.getBytes());
InputStream is = new ByteArrayInputStream(bytes);
try {
Object scriptWrapper = SaveService.loadElement(is);
HashTree hashTree = JMeterService.getHashTree(scriptWrapper);
// 调用执行方法
jMeterService.runDefinition(request.getId(), hashTree, request.getReportId(), runMode);
} catch (Exception e) {
e.printStackTrace();
LogUtil.error(e.getMessage());
}
return request.getId();
}
@ -380,9 +395,20 @@ public class ApiAutomationService {
config.setConfig(envConfig);
HashTree hashTree = request.getTestElement().generateHashTree(config);
// 调用执行方法
jMeterService.runDefinition(request.getId(), hashTree, request.getReportId(), ApiRunMode.SCENARIO.name());
String jmx = request.getTestElement().getJmx(hashTree);
createScenarioReport(request.getId(), request.getScenarioId(), request.getScenarioName(), ReportTriggerMode.MANUAL.name(), request.getExecuteType(), request.getProjectId(),
SessionUtils.getUserId());
byte[] bytes = JmeterDocumentParser.parse(jmx.getBytes());
InputStream is = new ByteArrayInputStream(bytes);
try {
Object scriptWrapper = SaveService.loadElement(is);
HashTree jmeterHashTree = JMeterService.getHashTree(scriptWrapper);
// 调用执行方法
jMeterService.runDefinition(request.getId(), jmeterHashTree, request.getReportId(), ApiRunMode.SCENARIO.name());
} catch (Exception e) {
e.printStackTrace();
LogUtil.error(e.getMessage());
}
return request.getId();
}

View File

@ -14,6 +14,7 @@ import io.metersphere.api.jmeter.JMeterService;
import io.metersphere.api.jmeter.TestResult;
import io.metersphere.api.parse.ApiImportParser;
import io.metersphere.api.parse.ApiImportParserFactory;
import io.metersphere.api.parse.old.JmeterDocumentParser;
import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.ApiDefinitionMapper;
import io.metersphere.base.mapper.ApiTestFileMapper;
@ -34,6 +35,7 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.jmeter.save.SaveService;
import org.apache.jorphan.collections.HashTree;
import org.aspectj.util.FileUtil;
import org.springframework.stereotype.Service;
@ -307,7 +309,17 @@ public class ApiDefinitionService {
runMode = ApiRunMode.API_PLAN.name();
}
// 调用执行方法
jMeterService.runDefinition(request.getId(), hashTree, request.getReportId(), runMode);
String jmx = request.getTestElement().getJmx(hashTree);
byte[] bytes = JmeterDocumentParser.parse(jmx.getBytes());
InputStream is = new ByteArrayInputStream(bytes);
try {
Object scriptWrapper = SaveService.loadElement(is);
HashTree testPlan = JMeterService.getHashTree(scriptWrapper);
// 调用执行方法
jMeterService.runDefinition(request.getId(), testPlan, request.getReportId(), runMode);
} catch (Exception e) {
e.printStackTrace();
}
return request.getId();
}

View File

@ -0,0 +1,211 @@
<template>
<div v-loading="loading">
<el-table
:data="tableData"
style="width: 100%;margin-bottom: 20px;"
row-key="id"
default-expand-all
@cell-click="editor"
:tree-props="{children: 'children', hasChildren: 'hasChildren'}">
<el-table-column
prop="name"
label="名称">
<template slot-scope="scope">
<el-input v-if="scope.row.id === tabClickIndex && tabClickProperty === 'name'" v-model="scope.row.name" size="mini" style="padding-left: 20px" @blur="inputBlur(scope.row)"></el-input>
<span v-else>{{scope.row.name}}</span>
</template>
</el-table-column>
<el-table-column
prop="required"
label="必输项"
align="center"
width="80">
<template slot-scope="scope">
<el-checkbox v-model="scope.row.required"/>
</template>
</el-table-column>
<el-table-column
prop="type"
width="120px"
label="类型">
<template slot-scope="scope">
<el-select v-model="scope.row.type" slot="prepend" size="small">
<el-option v-for="item in typeData" :key="item.id" :label="item.label" :value="item.id"/>
</el-select>
</template>
</el-table-column>
<el-table-column
prop="value"
label="内容">
<template slot-scope="scope">
<el-input v-if="scope.row.id === tabClickIndex && tabClickProperty === 'value'" v-model="scope.row.value" size="mini"></el-input>
<span v-else>{{scope.row.value}}</span>
</template>
</el-table-column>
<el-table-column
prop="describe"
label="描述">
<template slot-scope="scope">
<el-input v-if="scope.row.id === tabClickIndex && tabClickProperty === 'describe'" v-model="scope.row.describe" size="mini"></el-input>
<span v-else>{{scope.row.describe}}</span>
</template>
</el-table-column>
<el-table-column
prop="opt"
label="操作"
width="100">
<template slot-scope="scope">
<div>
<i class="el-icon-setting" style="margin-left: 5px;cursor: pointer" @click="setting(scope.row)"/>
<i class="el-icon-plus" style="margin-left: 5px;cursor: pointer" @click="add(scope.row)"/>
<i class="el-icon-close" style="margin-left: 5px;cursor: pointer" @click="deleteRow(scope.row)"/>
</div>
</template>
</el-table-column>
</el-table>
</div>
</template>
<script>
import {getUUID} from "@/common/js/utils";
export default {
name: "MsJsonTable",
components: {},
data() {
return {
loading: false,
tableData: [{
id: "root",
parent: null,
name: 'root',
required: true,
type: 'object',
value: 'object',
describe: 'describe',
editor: false,
children: [],
}]
,
tabClickProperty: "",
tabClickIndex: "",
typeData: [
{id: 'string', label: 'string'},
{id: 'number', label: 'number'},
{id: 'array', label: 'array'},
{id: 'object', label: 'object'},
{id: 'boolean', label: 'boolean'},
{id: 'integer', label: 'integer'}
]
}
},
methods: {
editor(row, column) {
switch (column.property) {
case 'name':
this.tabClickIndex = row.id
this.tabClickProperty = column.property
break
case 'value':
this.tabClickIndex = row.id
this.tabClickProperty = column.property
break;
case 'describe':
this.tabClickIndex = row.id
this.tabClickProperty = column.property
break;
default:
return
}
},
inputBlur() {
this.tabClickIndex = null;
this.tabClickProperty = '';
this.reload();
},
reload() {
this.loading = true;
this.$nextTick(() => {
this.loading = false
})
},
setting() {
},
add(row) {
let obj = {
id: getUUID(),
name: 'field',
required: true,
type: 'string',
value: 'test',
parent: null,
describe: 'describe',
children: [],
}
console.log("all", this.tableData)
console.log("row", row)
if (row.type === "object") {
obj.parent = row.id;
row.children.push(obj);
return;
}
let parentRow = {};
const index = this.tableData.findIndex(d => d.id != undefined && row.parent != undefined && d.id === row.parent);
if (index != -1) {
parentRow = this.tableData[index];
} else {
for (let i in this.tableData) {
if (this.tableData[i].children) {
parentRow = this.recursiveRemove(this.tableData[i].children, row);
}
}
}
console.log(parentRow)
if (parentRow) {
obj.parent = parentRow.id;
parentRow.children.push(obj);
return;
}
this.tableData.push(obj);
},
recursiveRemove(arr, row) {
for (let i in arr) {
const index = arr.findIndex(d => d.id != undefined && row.id != undefined && d.id === row.id)
if (index != -1) {
arr.splice(index, 1);
return arr[i];
}
if (arr[i].children != undefined && arr[i].children.length > 0) {
this.recursiveRemove(arr[i].children, row);
}
}
},
deleteRow(row) {
const index = this.tableData.findIndex(d => d.id != undefined && row.id != undefined && d.id === row.id)
if (index == -1) {
this.tableData.forEach(item => {
if (item.children) {
this.recursiveRemove(item.children, row);
}
})
} else {
this.tableData.splice(index, 1);
}
}
}
}
</script>
<style scoped>
.name-input >>> .el-input__inner {
height: 25px;
line-height: 25px;
}
</style>