fix(接口自动化): 场景步骤中接口无法执行的问题

This commit is contained in:
shiziyuan9527 2021-02-26 13:14:56 +08:00
parent 34fc7e8cf6
commit 025fa9cde0
10 changed files with 61 additions and 15 deletions

View File

@ -189,7 +189,7 @@ public class MsScenario extends MsTestElement {
}
});
}
if (config != null && config.getConfig() != null && config.getConfig().get(this.getProjectId()).getCommonConfig() != null
if (config != null && config.getConfig() != null && config.getConfig().get(this.getProjectId()) != null && config.getConfig().get(this.getProjectId()).getCommonConfig() != null
&& CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getCommonConfig().getVariables())) {
config.getConfig().get(this.getProjectId()).getCommonConfig().getVariables().stream().filter(KeyValue::isValid).filter(KeyValue::isEnable).forEach(keyValue ->
arguments.addArgument(keyValue.getName(), keyValue.getValue(), "=")

View File

@ -114,7 +114,7 @@ public class MsHTTPSamplerProxy extends MsTestElement {
sampler.setFollowRedirects(this.isFollowRedirects());
sampler.setUseKeepAlive(true);
sampler.setDoMultipart(this.isDoMultipartPost());
if (config != null && config.getConfig() != null) {
if (config != null && config.getConfig() != null && !config.getConfig().isEmpty()) {
config.setConfig(config.getConfig());
} else {
// 单独接口执行

View File

@ -13,6 +13,7 @@ import io.metersphere.api.dto.definition.parse.ApiDefinitionImportParserFactory;
import io.metersphere.api.dto.definition.request.ParameterConfig;
import io.metersphere.api.dto.definition.request.ScheduleInfoSwaggerUrlRequest;
import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
import io.metersphere.api.dto.scenario.request.RequestType;
import io.metersphere.api.dto.swaggerurl.SwaggerTaskResult;
import io.metersphere.api.dto.swaggerurl.SwaggerUrlRequest;
@ -85,6 +86,8 @@ public class ApiDefinitionService {
private ScheduleMapper scheduleMapper;
@Resource
private ApiTestCaseMapper apiTestCaseMapper;
@Resource
private ApiTestEnvironmentService environmentService;
private static Cache cache = Cache.newHardMemoryCache(0, 3600 * 24);
@ -427,8 +430,21 @@ public class ApiDefinitionService {
public String run(RunDefinitionRequest request, List<MultipartFile> bodyFiles) {
List<String> bodyUploadIds = new ArrayList<>(request.getBodyUploadIds());
FileUtils.createBodyFiles(bodyUploadIds, bodyFiles);
ParameterConfig config = new ParameterConfig();
config.setProjectId(request.getProjectId());
Map<String, EnvironmentConfig> envConfig = new HashMap<>();
Map<String, String> map = request.getEnvironmentMap();
if (map != null) {
map.keySet().forEach(id -> {
ApiTestEnvironmentWithBLOBs environment = environmentService.get(map.get(id));
EnvironmentConfig env = JSONObject.parseObject(environment.getConfig(), EnvironmentConfig.class);
envConfig.put(id, env);
});
config.setConfig(envConfig);
}
HashTree hashTree = request.getTestElement().generateHashTree(config);
String runMode = ApiRunMode.DEFINITION.name();
if (StringUtils.isNotBlank(request.getType()) && StringUtils.equals(request.getType(), ApiRunMode.API_PLAN.name())) {

View File

@ -149,7 +149,7 @@
<span class="custom-tree-node father" slot-scope="{ node, data}" style="width: 96%">
<!-- 步骤组件-->
<ms-component-config :type="data.type" :scenario="data" :response="response" :currentScenario="currentScenario"
:currentEnvironmentId="currentEnvironmentId" :node="node" :project-list="projectList"
:currentEnvironmentId="currentEnvironmentId" :node="node" :project-list="projectList" :env-map="projectEnvMap"
@remove="remove" @copyRow="copyRow" @suggestClick="suggestClick" @refReload="reload"/>
</span>
</el-tree>

View File

@ -16,7 +16,7 @@
<el-tag size="mini" style="margin-left: 20px" v-if="request.referenced==='Copy'">{{ $t('commons.copy') }}</el-tag>
<el-tag size="mini" style="margin-left: 20px" v-if="request.referenced ==='REF'">{{ $t('api_test.scenario.reference') }}</el-tag>
<span style="margin-left: 20px;">{{getProjectName(request.projectId)}}</span>
<ms-run :debug="true" :reportId="reportId" :run-data="runData"
<ms-run :debug="true" :reportId="reportId" :run-data="runData" :env-map="envMap"
@runRefresh="runRefresh" ref="runTest"/>
</template>
@ -84,7 +84,8 @@
default: false,
},
currentEnvironmentId: String,
projectList: Array
projectList: Array,
envMap: Map
},
components: {
CustomizeReqInfo,
@ -249,13 +250,20 @@
this.reload();
},
run() {
if (!this.currentEnvironmentId) {
this.$error(this.$t('api_test.environment.select_environment'));
return;
if (!this.envMap || this.envMap.size === 0) {
this.$warning("请在环境配置中为该步骤所属项目选择运行环境!");
return false;
} else if (this.envMap && this.envMap.size > 0) {
const env = this.envMap.get(this.request.projectId);
if (!env) {
this.$warning("请在环境配置中为该步骤所属项目选择运行环境!");
return false;
}
}
this.request.active = true;
this.loading = true;
this.runData = [];
this.runData.projectId = this.request.projectId;
this.request.useEnvironment = this.currentEnvironmentId;
this.request.customizeReq = this.isCustomizeReq;
let debugData = {

View File

@ -2,7 +2,7 @@
<div class="request-form">
<component :is="component" :scenario="scenario" :controller="scenario" :timer="scenario" :assertions="scenario" :extract="scenario" :jsr223-processor="scenario" :request="scenario" :currentScenario="currentScenario" :currentEnvironmentId="currentEnvironmentId" :node="node"
:draggable="true" :title="title" :color="titleColor" :background-color="backgroundColor" @suggestClick="suggestClick(node)" :response="response"
@remove="remove" @copyRow="copyRow" @refReload="refReload" :project-list="projectList"/>
@remove="remove" @copyRow="copyRow" @refReload="refReload" :project-list="projectList" :env-map="envMap"/>
</div>
</template>
@ -28,7 +28,8 @@
currentEnvironmentId: String,
response: {},
node: {},
projectList: Array
projectList: Array,
envMap: Map
},
data() {
return {

View File

@ -2,7 +2,7 @@
<span></span>
</template>
<script>
import {getUUID, getBodyUploadFiles,getCurrentProjectID} from "@/common/js/utils";
import {getUUID, getBodyUploadFiles, getCurrentProjectID, strMapToObj} from "@/common/js/utils";
import ThreadGroup from "./jmeter/components/thread-group";
import TestPlan from "./jmeter/components/test-plan";
@ -14,7 +14,8 @@
debug: Boolean,
reportId: String,
runData: Array,
type: String
type: String,
envMap: Map
},
data() {
return {
@ -64,7 +65,16 @@
this.runData.forEach(item => {
threadGroup.hashTree.push(item);
})
let reqObj = {id: this.reportId, testElement: testPlan, type: this.type,projectId:getCurrentProjectID()};
let projectId = "";
// envMap
if (!this.envMap) {
projectId = getCurrentProjectID();
} else {
//
projectId = this.runData.projectId;
}
let reqObj = {id: this.reportId, testElement: testPlan, type: this.type,projectId:getCurrentProjectID(), environmentMap: strMapToObj(this.envMap)};
let bodyFiles = getBodyUploadFiles(reqObj, this.runData);
let url = "";
if (this.debug) {

View File

@ -38,7 +38,7 @@
</ms-drawer>
<!-- 执行组件 -->
<ms-run :debug="false" :environment="environment" :reportId="reportId" :run-data="runData"
<ms-run :debug="false" :environment="environment" :reportId="reportId" :run-data="runData" :env-map="envMap"
@runRefresh="runRefresh" ref="runTest"/>
<!--批量编辑-->
<ms-batch-edit ref="batchEdit" @batchEdit="batchEdit" :typeArr="typeArr" :value-arr="valueArr"/>
@ -111,6 +111,7 @@
priority: CASE_PRIORITY,
method: REQ_METHOD,
},
envMap: new Map
}
},
watch: {
@ -274,6 +275,7 @@
this.$get('/api/definition/get/' + row.request.id, response => {
row.request.path = response.data.path; // pathpath
row.request.useEnvironment = this.environment.id;
row.request.projectId = getCurrentProjectID();
this.runData.push(row.request);
/*触发执行操作*/
this.reportId = getUUID().substring(0, 8);

View File

@ -64,7 +64,7 @@
ref="caseList"/>
<!-- 执行组件 -->
<ms-run :debug="false" :environment="api.environment" :reportId="reportId" :run-data="runData"
<ms-run :debug="false" :environment="api.environment" :reportId="reportId" :run-data="runData" :env-map="envMap"
@runRefresh="runRefresh" ref="runTest"/>
</div>
@ -110,6 +110,7 @@ export default {
},
runData: [],
reportId: "",
envMap: new Map
}
},
props: {apiData: {}, currentProtocol: String, syncTabs: Array, projectId: String},

View File

@ -328,3 +328,11 @@ export function handleCtrlSEvent(event, func) {
return false;
}
}
export function strMapToObj(strMap){
let obj= Object.create(null);
for (let[k,v] of strMap) {
obj[k] = v;
}
return obj;
}