fix(接口测试): 修复循环控制器单独执行收不到结果问题

This commit is contained in:
fit2-zhao 2021-12-27 17:38:16 +08:00 committed by fit2-zhao
parent f808535bbf
commit c89260ce86
5 changed files with 47 additions and 110 deletions

View File

@ -14,6 +14,8 @@ public class RunDefinitionRequest {
private String reportId;
private String runMode;
private boolean isDebug;
private boolean saved;

View File

@ -374,9 +374,9 @@ public class ApiScenarioExecuteService {
}
uploadBodyFiles(request.getBodyFileRequestIds(), bodyFiles);
FileUtils.createBodyFiles(request.getScenarioFileIds(), scenarioFiles);
String runMode = StringUtils.isEmpty(request.getRunMode()) ? ApiRunMode.SCENARIO.name() : request.getRunMode();
// 调用执行方法
JmeterRunRequestDTO runRequest = new JmeterRunRequestDTO(request.getId(), request.getId(), ApiRunMode.SCENARIO.name(), hashTree);
JmeterRunRequestDTO runRequest = new JmeterRunRequestDTO(request.getId(), request.getId(), runMode, hashTree);
runRequest.setDebug(true);
jMeterService.run(runRequest);
return request.getId();

View File

@ -12,6 +12,7 @@ export default {
components: {},
props: {
environment: Map,
runMode: String,
debug: Boolean,
reportId: String,
runData: Object,
@ -69,7 +70,7 @@ export default {
testPlan.hashTree.push(threadGroup);
this.sort(testPlan.hashTree);
let reqObj = {
id: this.reportId, reportId: this.reportId, scenarioName: this.runData.name, saved: this.saved,
id: this.reportId, reportId: this.reportId, scenarioName: this.runData.name, saved: this.saved, runMode: this.runMode,
scenarioId: this.runData.id, testElement: testPlan, projectId: getCurrentProjectID(), environmentMap: strMapToObj(map),
environmentType: this.environmentType, environmentGroupId: this.environmentGroupId, environmentJson: JSON.stringify(strMapToObj(map))
};

View File

@ -89,16 +89,7 @@
<template v-slot:result>
<div v-loading="loading">
<p class="tip">{{ $t('api_test.definition.request.res_param') }} </p>
<div v-if="request.result">
<div v-for="(scenario,h) in request.result.scenarios" :key="h">
<el-tabs v-model="request.activeName" closable class="ms-tabs">
<el-tab-pane v-for="(item,i) in scenario.requestResults" :label="'循环'+(i+1)" :key="i" style="margin-bottom: 5px">
<api-response-component :currentProtocol="request.protocol" :apiActive="true" :result="item"/>
</el-tab-pane>
</el-tabs>
</div>
</div>
<div v-else-if="showXpackCompnent&&request.backEsbDataStruct != null">
<div v-if="showXpackCompnent&&request.backEsbDataStruct != null">
<esb-definition-response
:currentProtocol="request.protocol"
:request="request"
@ -147,7 +138,6 @@ import ApiBaseComponent from "../common/ApiBaseComponent";
import ApiResponseComponent from "./ApiResponseComponent";
import CustomizeReqInfo from "@/business/components/api/automation/scenario/common/CustomizeReqInfo";
import TemplateComponent from "@/business/components/track/plan/view/comonents/report/TemplateComponent/TemplateComponent";
import {ENV_TYPE} from "@/common/js/constants";
import {getUrl} from "@/business/components/api/automation/scenario/component/urlhelper";
const requireComponent = require.context('@/business/components/xpack/', true, /\.vue$/);
@ -256,6 +246,10 @@ export default {
environmentType(val) {
this.envType = val;
},
'$store.state.currentApiCase.debugLoop'() {
this.forStatus();
this.reload();
},
'$store.state.currentApiCase.resetDataSource'() {
if (this.request.id && this.request.referenced !== 'REF') {
this.initDataSource();

View File

@ -1,6 +1,6 @@
<template>
<div>
<ms-run :debug="true" :environment="envMap" :reportId="reportId" :run-data="debugData" @runRefresh="runRefresh" ref="runTest"/>
<ms-run :debug="true" :environment="envMap" :reportId="reportId" :runMode="'DEFINITION'" :run-data="debugData" @runRefresh="runRefresh" ref="runTest"/>
<api-base-component @copy="copyRow" @active="active(controller)" @remove="remove" :data="controller" :draggable="draggable" :is-max="isMax" :show-btn="showBtn" color="#02A7F0" background-color="#F4F4F5" :title="$t('api_test.automation.loop_controller')" v-loading="loading">
<template v-slot:headerLeft>
@ -12,7 +12,6 @@
<template v-slot:message>
<span v-if="requestResult && requestResult.scenarios && requestResult.scenarios.length > 0 " style="color: #8c939d;margin-right: 10px">
<!--{{$t('api_test.automation.loop_name')}}{{requestResult.scenarios.length}} 成功{{success}} 失败{{error}}-->
</span>
</template>
@ -89,7 +88,7 @@ import ApiBaseComponent from "../common/ApiBaseComponent";
import ApiResponseComponent from "./ApiResponseComponent";
import MsRun from "../DebugRun";
import {getUUID} from "@/common/js/utils";
import {ELEMENT_TYPE, STEP} from "../Setting";
import {STEP} from "../Setting";
export default {
name: "MsLoopController",
@ -118,8 +117,7 @@ export default {
data() {
return {
loading: false,
activeName: "first",
requestResult: {responseResult: {}},
requestResult: new Map(),
success: 0,
error: 0,
debugData: {},
@ -159,6 +157,7 @@ export default {
value: "is not empty",
},
stepFilter: new STEP,
messageWebSocket: {},
},
};
},
@ -168,6 +167,35 @@ export default {
},
},
methods: {
initMessageSocket() {
let protocol = "ws://";
if (window.location.protocol === 'https:') {
protocol = "wss://";
}
const uri = protocol + window.location.host + "/ws/" + this.reportId;
this.requestResult = new Map();
this.messageWebSocket = new WebSocket(uri);
this.messageWebSocket.onmessage = this.onDebugMessage;
},
onDebugMessage(e) {
if (e.data && e.data.startsWith("result_")) {
let data = JSON.parse(e.data.substring(7));
let resourceId = data.resourceId.split("_")[0];
if (this.requestResult.has(resourceId)) {
this.requestResult.get(resourceId).push(data);
} else {
this.requestResult.set(resourceId, [data]);
}
} else if (e.data && e.data.indexOf("MS_TEST_END") !== -1) {
this.loading = false;
this.node.expanded = true;
this.messageWebSocket.close();
//
this.setResult(this.controller.hashTree);
this.$store.state.currentApiCase = {debugLoop: getUUID()};
this.reload();
}
},
getCode() {
if (this.node && this.node.data.code && this.node.data.debug) {
if (this.node.data.code && this.node.data.code === 'error') {
@ -178,25 +206,6 @@ export default {
}
return '';
},
initResult() {
if (this.controller) {
switch (this.controller.loopType) {
case "LOOP_COUNT":
this.requestResult = this.controller.countController && this.controller.countController.requestResult ? this.controller.countController.requestResult : {};
break;
case "FOREACH":
this.requestResult = this.controller.forEachController && this.controller.forEachController.requestResult ? this.controller.forEachController.requestResult : {};
break;
case "WHILE":
this.requestResult = this.controller.whileController && this.controller.whileController.requestResult ? this.controller.whileController.requestResult : {};
break;
default:
break;
}
}
this.getFails();
this.activeName = this.requestResult && this.requestResult.scenarios && this.requestResult.scenarios.length > 0 ? this.requestResult.scenarios[0].name : "";
},
switchChange() {
if (this.controller.hashTree && this.controller.hashTree.length > 1) {
this.$warning(this.$t('api_test.automation.loop_message'));
@ -214,7 +223,6 @@ export default {
this.recursive(item.hashTree, count);
}
});
if (count > 1) {
this.$warning(this.$t('api_test.automation.loop_message'));
this.controller.countController.proceed = true;
@ -282,33 +290,15 @@ export default {
});
},
runRefresh() {
this.getReport();
},
getFails() {
this.error = 0;
this.success = 0;
if (this.requestResult.scenarios && this.requestResult.scenarios !== null) {
this.requestResult.scenarios.forEach((scenario) => {
if (scenario.requestResults) {
scenario.requestResults.forEach((item) => {
if (item.error > 0) {
this.error++;
return;
}
});
}
});
this.success = this.requestResult.scenarios && this.requestResult.scenarios !== null ? this.requestResult.scenarios.length - this.error : 0;
}
this.initMessageSocket();
},
setResult(hashTree) {
if (hashTree) {
hashTree.forEach((item) => {
if (item.type === "HTTPSamplerProxy" || item.type === "DubboSampler" || item.type === "JDBCSampler" || item.type === "TCPSampler") {
item.result = this.requestResult;
item.activeName = this.activeName;
item.activeName = "0";
item.active = true;
item.requestResult = [];
item.requestResult = this.requestResult.get(item.resourceId);
}
if (item.hashTree && item.hashTree.length > 0) {
this.setResult(item.hashTree);
@ -316,61 +306,11 @@ export default {
});
}
},
getReport() {
if (this.reportId) {
let url = "/api/scenario/report/get/" + this.reportId;
this.$get(url, (response) => {
this.report = response.data || {};
if (response.data) {
if (this.isNotRunning) {
try {
this.requestResult = JSON.parse(this.report.content);
if (!this.requestResult) {
this.requestResult = {scenarios: []};
}
this.controller.requestResult = this.requestResult;
switch (this.controller.loopType) {
case "LOOP_COUNT":
this.controller.countController.requestResult = this.requestResult;
break;
case "FOREACH":
this.controller.forEachController.requestResult = this.requestResult;
break;
case "WHILE":
this.controller.whileController.requestResult = this.requestResult;
break;
default:
break;
}
this.getFails();
this.activeName = this.requestResult && this.requestResult.scenarios && this.requestResult.scenarios !== null && this.requestResult.scenarios.length > 0 ? this.requestResult.scenarios[0].name : "";
//
this.setResult(this.controller.hashTree);
this.$emit("refReload", this.node);
} catch (e) {
throw e;
}
this.loading = false;
this.node.expanded = true;
this.reload();
} else {
setTimeout(this.getReport, 2000);
}
} else {
this.loading = false;
this.$error(this.$t("api_report.not_exist"));
}
});
}
},
},
computed: {
hasEmptyOperator() {
return !!this.controller.operator && this.controller.operator.indexOf("empty") > 0;
},
isNotRunning() {
return "Running" !== this.report.status;
},
},
};
</script>