From a2a2bd05715dbe01d871fda2d4247db61eec8289 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Tue, 31 Jan 2023 15:10:25 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E5=AF=BC=E5=85=A5jmx=E6=96=87=E4=BB=B6=E4=BC=98=E5=8C=96?= =?UTF-8?q?=EF=BC=8C=E6=94=AF=E6=8C=81=E5=A4=9A=E7=BA=BF=E7=A8=8B=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --story=1011083 --user=赵勇 【Bug转需求】接口自动化支持导入多线程的jmx文件 https://www.tapd.cn/55049933/s/1329318 --- .../api/parse/scenario/JMeterParser.java | 37 +++- .../automation/scenario/EditApiScenario.vue | 187 +++++++++--------- 2 files changed, 121 insertions(+), 103 deletions(-) diff --git a/api-test/backend/src/main/java/io/metersphere/api/parse/scenario/JMeterParser.java b/api-test/backend/src/main/java/io/metersphere/api/parse/scenario/JMeterParser.java index 8f7e7dde13..45e520621c 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/parse/scenario/JMeterParser.java +++ b/api-test/backend/src/main/java/io/metersphere/api/parse/scenario/JMeterParser.java @@ -29,6 +29,7 @@ import io.metersphere.api.dto.definition.request.sampler.dubbo.MsConsumerAndServ import io.metersphere.api.dto.definition.request.sampler.dubbo.MsRegistryCenter; import io.metersphere.api.dto.definition.request.timer.MsConstantTimer; import io.metersphere.api.dto.definition.request.unknown.MsJmeterElement; +import io.metersphere.api.dto.definition.request.variable.ScenarioVariable; import io.metersphere.api.dto.scenario.Body; import io.metersphere.api.dto.scenario.DatabaseConfig; import io.metersphere.api.dto.scenario.KeyValue; @@ -37,10 +38,7 @@ import io.metersphere.api.parse.ApiImportAbstractParser; import io.metersphere.base.domain.ApiScenarioWithBLOBs; import io.metersphere.base.domain.ApiTestEnvironmentExample; import io.metersphere.base.domain.ApiTestEnvironmentWithBLOBs; -import io.metersphere.commons.constants.ElementConstants; -import io.metersphere.commons.constants.LoopConstants; -import io.metersphere.commons.constants.PropertyConstant; -import io.metersphere.commons.constants.RequestTypeConstants; +import io.metersphere.commons.constants.*; import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.*; import io.metersphere.environment.service.BaseEnvironmentService; @@ -48,8 +46,10 @@ import io.metersphere.plugin.core.MsTestElement; import io.metersphere.request.BodyFile; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; +import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.apache.jmeter.assertions.*; +import org.apache.jmeter.config.Argument; import org.apache.jmeter.config.ConfigTestElement; import org.apache.jmeter.control.ForeachController; import org.apache.jmeter.control.LoopController; @@ -75,6 +75,7 @@ import org.apache.jmeter.save.SaveService; import org.apache.jmeter.testelement.TestElement; import org.apache.jmeter.testelement.TestPlan; import org.apache.jmeter.testelement.property.JMeterProperty; +import org.apache.jmeter.threads.ThreadGroup; import org.apache.jmeter.timers.ConstantTimer; import org.apache.jorphan.collections.HashTree; @@ -105,7 +106,27 @@ public class JMeterParser extends ApiImportAbstractParser { MsScenario scenario = new MsScenario(); scenario.setReferenced("IMPORT"); - formatHashTree(testPlan, scenario); + + TestPlan plan = (TestPlan) testPlan.getArray()[0]; + if (plan.getArguments() != null) { + List variables = new LinkedList<>(); + plan.getArguments().forEach(item -> { + ScenarioVariable scenarioVariable = new ScenarioVariable(); + scenarioVariable.setId(UUID.randomUUID().toString()); + scenarioVariable.setName(item.getName()); + if (ObjectUtils.isNotEmpty(item.getObjectValue())) { + Argument arg = (Argument) item.getObjectValue(); + scenarioVariable.setValue(arg.getValue()); + } + scenarioVariable.setType(VariableTypeConstants.CONSTANT.name()); + variables.add(scenarioVariable); + }); + scenario.setVariables(variables); + } + if (CollectionUtils.isEmpty(scenario.getHashTree())) { + scenario.setHashTree(new LinkedList<>()); + } + formatHashTree(testPlan.getTree(plan), scenario); this.projectId = request.getProjectId(); ScenarioImport scenarioImport = new ScenarioImport(); scenarioImport.setData(parseObj(scenario, request)); @@ -697,11 +718,7 @@ public class JMeterParser extends ApiImportAbstractParser { } // 测试计划 if (key instanceof TestPlan) { - scenario.setName(((TestPlan) key).getName()); - elementNode = new MsJmeterElement(); - elementNode.setName(((TestPlan) key).getName()); - ((MsJmeterElement) elementNode).setJmeterElement(objToXml(key)); - ((MsJmeterElement) elementNode).setElementType(key.getClass().getSimpleName()); + continue; } // 线程组 else if (key instanceof ThreadGroup) { diff --git a/api-test/frontend/src/business/automation/scenario/EditApiScenario.vue b/api-test/frontend/src/business/automation/scenario/EditApiScenario.vue index 49a0a15df1..c24ec8e429 100644 --- a/api-test/frontend/src/business/automation/scenario/EditApiScenario.vue +++ b/api-test/frontend/src/business/automation/scenario/EditApiScenario.vue @@ -111,7 +111,7 @@ {{ $t('api_test.automation.scenario_total') }} + >{{ $t('api_test.automation.scenario_total') }} :{{ getVariableSize() }} @@ -152,10 +152,10 @@ size="mini" @command="handleCommand" v-permission="[ - 'PROJECT_API_SCENARIO:READ+EDIT', - 'PROJECT_API_SCENARIO:READ+CREATE', - 'PROJECT_API_SCENARIO:READ+COPY', - ]"> + 'PROJECT_API_SCENARIO:READ+EDIT', + 'PROJECT_API_SCENARIO:READ+CREATE', + 'PROJECT_API_SCENARIO:READ+COPY', + ]"> {{ $t('api_test.request.debug') }} {{ $t('api_test.automation.generate_report') }} @@ -172,10 +172,10 @@ @click="editScenario" title="ctrl + s" v-permission="[ - 'PROJECT_API_SCENARIO:READ+EDIT', - 'PROJECT_API_SCENARIO:READ+CREATE', - 'PROJECT_API_SCENARIO:READ+COPY', - ]"> + 'PROJECT_API_SCENARIO:READ+EDIT', + 'PROJECT_API_SCENARIO:READ+CREATE', + 'PROJECT_API_SCENARIO:READ+COPY', + ]"> {{ $t('commons.save') }} @@ -196,26 +196,26 @@ {{ $t('operating_log.change_history') }} + >{{ $t('operating_log.change_history') }} {{ $t('api_test.automation.request_total') }} {{ reqTotal }} {{ $t('api_test.automation.request_success') }} {{ reqSuccess }} - {{ $t('api_test.automation.request_error') }} - {{ reqError }} @@ -293,65 +293,65 @@ align="middle" slot-scope="{ node, data }" style="width: 100%"> - - - + class="custom-tree-node-col" + style="padding-left: 0px; padding-right: 0px" + v-show="node && data.hashTree && data.hashTree.length > 0 && !data.isLeaf"> + + + - - + + - - - - + + + + @@ -576,12 +576,11 @@ import { saveScenario, } from '@/business/automation/api-automation'; import MsComponentConfig from './component/ComponentConfig'; -import {ENV_TYPE} from 'metersphere-frontend/src/utils/constants'; -import {mergeRequestDocumentData} from '@/business/definition/api-definition'; -import {getEnvironmentByProjectId} from 'metersphere-frontend/src/api/environment'; -import {useApiStore} from '@/store'; -import {getDefaultVersion, setLatestVersionById} from 'metersphere-frontend/src/api/version'; - +import { ENV_TYPE } from 'metersphere-frontend/src/utils/constants'; +import { mergeRequestDocumentData } from '@/business/definition/api-definition'; +import { getEnvironmentByProjectId } from 'metersphere-frontend/src/api/environment'; +import { useApiStore } from '@/store'; +import { getDefaultVersion, setLatestVersionById } from 'metersphere-frontend/src/api/version'; const store = useApiStore(); @@ -790,7 +789,7 @@ export default { debugReportId: '', isPreventReClick: false, latestVersionId: '', - hasLatest: false + hasLatest: false, }; }, created() { @@ -923,6 +922,9 @@ export default { if (!array) { array = this.scenarioDefinition; } + if (!array) { + return; + } let isLeaf = true; let nodeType = ''; if (node) { @@ -2489,11 +2491,10 @@ export default { if (!hasLicense()) { return; } - getDefaultVersion(this.projectId) - .then(response => { - this.latestVersionId = response.data; - this.getVersionHistory(); - }); + getDefaultVersion(this.projectId).then((response) => { + this.latestVersionId = response.data; + this.getVersionHistory(); + }); }, getVersionHistory() { if (!hasLicense()) { @@ -2507,7 +2508,7 @@ export default { } let latestVersionData = response.data.filter((v) => v.versionId === this.latestVersionId); if (latestVersionData.length > 0) { - this.hasLatest = false + this.hasLatest = false; } else { this.hasLatest = true; } @@ -2567,8 +2568,8 @@ export default { projectId: this.projectId, type: 'SCENARIO', versionId: row.id, - resourceId: this.currentScenario.id - } + resourceId: this.currentScenario.id, + }; setLatestVersionById(param).then(() => { this.$success(this.$t('commons.modify_success')); this.checkout(row);