fix(场景自动化) 增加数组数据处理

This commit is contained in:
fit2-zhao 2021-09-16 18:11:46 +08:00 committed by fit2-zhao
parent 4931c7816b
commit 8b50869490
4 changed files with 313 additions and 285 deletions

View File

@ -274,27 +274,8 @@ public class MsHTTPSamplerProxy extends MsTestElement {
addCertificate(config, httpSamplerTree); addCertificate(config, httpSamplerTree);
//增加全局前后至脚本 //增加全局前后至脚本
MsJSR223PreProcessor preProcessor = httpConfig.getPreProcessor(); if (httpConfig != null) {
MsJSR223PostProcessor postProcessor = httpConfig.getPostProcessor(); this.setScript(httpConfig, httpSamplerTree, config);
if(preProcessor != null){
if (preProcessor.getEnvironmentId() == null) {
if (this.getEnvironmentId() == null) {
preProcessor.setEnvironmentId(useEnvironment);
} else {
preProcessor.setEnvironmentId(this.getEnvironmentId());
}
}
preProcessor.toHashTree(httpSamplerTree, preProcessor.getHashTree(), config);
}
if(postProcessor != null){
if (postProcessor.getEnvironmentId() == null) {
if (this.getEnvironmentId() == null) {
postProcessor.setEnvironmentId(useEnvironment);
} else {
postProcessor.setEnvironmentId(this.getEnvironmentId());
}
}
postProcessor.toHashTree(httpSamplerTree, postProcessor.getHashTree(), config);
} }
if (CollectionUtils.isNotEmpty(hashTree)) { if (CollectionUtils.isNotEmpty(hashTree)) {
for (MsTestElement el : hashTree) { for (MsTestElement el : hashTree) {
@ -311,6 +292,31 @@ public class MsHTTPSamplerProxy extends MsTestElement {
} }
private void setScript(HttpConfig httpConfig, HashTree httpSamplerTree, ParameterConfig config) {
MsJSR223PreProcessor preProcessor = httpConfig.getPreProcessor();
MsJSR223PostProcessor postProcessor = httpConfig.getPostProcessor();
if (preProcessor != null) {
if (preProcessor.getEnvironmentId() == null) {
if (this.getEnvironmentId() == null) {
preProcessor.setEnvironmentId(useEnvironment);
} else {
preProcessor.setEnvironmentId(this.getEnvironmentId());
}
}
preProcessor.toHashTree(httpSamplerTree, preProcessor.getHashTree(), config);
}
if (postProcessor != null) {
if (postProcessor.getEnvironmentId() == null) {
if (this.getEnvironmentId() == null) {
postProcessor.setEnvironmentId(useEnvironment);
} else {
postProcessor.setEnvironmentId(this.getEnvironmentId());
}
}
postProcessor.toHashTree(httpSamplerTree, postProcessor.getHashTree(), config);
}
}
private void initConnectAndResponseTimeout(ParameterConfig config) { private void initConnectAndResponseTimeout(ParameterConfig config) {
if (config.isEffective(this.getProjectId())) { if (config.isEffective(this.getProjectId())) {
String useEvnId = config.getConfig().get(this.getProjectId()).getApiEnvironmentid(); String useEvnId = config.getConfig().get(this.getProjectId()).getApiEnvironmentid();
@ -318,14 +324,14 @@ public class MsHTTPSamplerProxy extends MsTestElement {
this.setEnvironmentId(useEvnId); this.setEnvironmentId(useEvnId);
} }
CommonConfig commonConfig = config.getConfig().get(this.getProjectId()).getCommonConfig(); CommonConfig commonConfig = config.getConfig().get(this.getProjectId()).getCommonConfig();
if(commonConfig != null){ if (commonConfig != null) {
if(this.getConnectTimeout() == null || StringUtils.equals(this.getConnectTimeout(),"60000")){ if (this.getConnectTimeout() == null || StringUtils.equals(this.getConnectTimeout(), "60000")) {
if(commonConfig.getRequestTimeout() != 0){ if (commonConfig.getRequestTimeout() != 0) {
this.setConnectTimeout(String.valueOf(commonConfig.getRequestTimeout())); this.setConnectTimeout(String.valueOf(commonConfig.getRequestTimeout()));
} }
} }
if(this.getResponseTimeout() == null || StringUtils.equals(this.getResponseTimeout(),"60000")){ if (this.getResponseTimeout() == null || StringUtils.equals(this.getResponseTimeout(), "60000")) {
if(commonConfig.getResponseTimeout() != 0){ if (commonConfig.getResponseTimeout() != 0) {
this.setResponseTimeout(String.valueOf(commonConfig.getResponseTimeout())); this.setResponseTimeout(String.valueOf(commonConfig.getResponseTimeout()));
} }
} }
@ -340,15 +346,16 @@ public class MsHTTPSamplerProxy extends MsTestElement {
private HttpConfig getHttpConfig(ParameterConfig config) { private HttpConfig getHttpConfig(ParameterConfig config) {
if (config.isEffective(this.getProjectId())) { if (config.isEffective(this.getProjectId())) {
EnvironmentConfig environmentConfig = config.getConfig().get(this.getProjectId()); EnvironmentConfig environmentConfig = config.getConfig().get(this.getProjectId());
if (environmentConfig != null){ if (environmentConfig != null) {
String useEvnId = environmentConfig.getApiEnvironmentid(); String useEvnId = environmentConfig.getApiEnvironmentid();
if(this.authManager == null && environmentConfig.getAuthManager() != null && environmentConfig.getAuthManager().containsKey("hashTree") ){ if (this.authManager == null && environmentConfig.getAuthManager() != null && environmentConfig.getAuthManager().containsKey("hashTree")) {
try { try {
JSONArray jsonArray = environmentConfig.getAuthManager().getJSONArray("hashTree"); JSONArray jsonArray = environmentConfig.getAuthManager().getJSONArray("hashTree");
if(jsonArray.size() > 0){ if (jsonArray.size() > 0) {
this.authManager = jsonArray.getJSONObject(0).toJavaObject(MsAuthManager.class); this.authManager = jsonArray.getJSONObject(0).toJavaObject(MsAuthManager.class);
} }
}catch (Exception e){} } catch (Exception e) {
}
} }
if (StringUtils.isNotEmpty(useEvnId) && !StringUtils.equals(useEvnId, this.getEnvironmentId())) { if (StringUtils.isNotEmpty(useEvnId) && !StringUtils.equals(useEvnId, this.getEnvironmentId())) {
this.setEnvironmentId(useEvnId); this.setEnvironmentId(useEvnId);
@ -702,18 +709,18 @@ public class MsHTTPSamplerProxy extends MsTestElement {
for (KeyValue keyValue : keyValues) { for (KeyValue keyValue : keyValues) {
boolean hasHead = false; boolean hasHead = false;
//检查是否已经有重名的Head如果Header重复会导致执行报错 //检查是否已经有重名的Head如果Header重复会导致执行报错
if(headerManager.getHeaders() != null){ if (headerManager.getHeaders() != null) {
for(int i = 0; i < headerManager.getHeaders().size(); i ++){ for (int i = 0; i < headerManager.getHeaders().size(); i++) {
Header header = headerManager.getHeader(i); Header header = headerManager.getHeader(i);
String headName = header.getName(); String headName = header.getName();
if(StringUtils.equals(headName,keyValue.getName())){ if (StringUtils.equals(headName, keyValue.getName())) {
hasHead = true; hasHead = true;
break; break;
} }
} }
} }
if(!hasHead){ if (!hasHead) {
headerManager.add(new Header(keyValue.getName(), ScriptEngineUtils.buildFunctionCallString(keyValue.getValue()))); headerManager.add(new Header(keyValue.getName(), ScriptEngineUtils.buildFunctionCallString(keyValue.getValue())));
} }
} }

View File

@ -63,19 +63,29 @@ public class HttpConfig {
} }
} }
public HttpConfig getModuleCondition(String moduleId, HttpConfigCondition configCondition) { // public HttpConfig getModuleCondition(String moduleId, HttpConfigCondition configCondition) {
List<String> moduleIds = new ArrayList<>(); // List<String> moduleIds = new ArrayList<>();
if (CollectionUtils.isNotEmpty(configCondition.getDetails())) { // if (CollectionUtils.isNotEmpty(configCondition.getDetails())) {
if (CollectionUtils.isEmpty(configCondition.getModuleIds())) { // if (CollectionUtils.isEmpty(configCondition.getModuleIds())) {
for (KeyValue keyValue : configCondition.getDetails()) { // for (KeyValue keyValue : configCondition.getDetails()) {
getAllChild(keyValue.getValue(), moduleIds); // getAllChild(keyValue.getValue(), moduleIds);
} // }
configCondition.setModuleIds(moduleIds); // configCondition.setModuleIds(moduleIds);
} else { // } else {
moduleIds = configCondition.getModuleIds(); // moduleIds = configCondition.getModuleIds();
} // }
//
// if (moduleIds.contains(moduleId)) {
// return initHttpConfig(configCondition);
// }
// }
// return null;
// }
if (moduleIds.contains(moduleId)) { public HttpConfig getModuleCondition(String moduleId, HttpConfigCondition configCondition) {
if (CollectionUtils.isNotEmpty(configCondition.getDetails())) {
List<KeyValue> details = configCondition.getDetails().stream().filter(detail -> StringUtils.contains(detail.getValue(), moduleId)).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(details)) {
return initHttpConfig(configCondition); return initHttpConfig(configCondition);
} }
} }

@ -1 +1 @@
Subproject commit 6e2b1d3d7d79f985e6edf81eb593670b8da87465 Subproject commit a6a66f87a57346cdd7da22086bac5e82a7e47bdb

View File

@ -69,7 +69,7 @@
</el-tab-pane> </el-tab-pane>
<el-tab-pane name="create" v-if="hasPermission('PROJECT_API_DEFINITION:READ+CREATE_API') && hasLicense() && definitionTest"> <el-tab-pane name="create" v-if="hasPermission('PROJECT_API_DEFINITION:READ+CREATE_API') && hasLicense() && definitionTest">
<template v-slot:label> <template v-slot:label>
<el-button size="mini" type="primary" @click.stop @click="generate">{{$t('commons.generate_test_data')}}</el-button> <el-button size="mini" type="primary" @click.stop @click="generate">{{ $t('commons.generate_test_data') }}</el-button>
</template> </template>
</el-tab-pane> </el-tab-pane>
</el-tabs> </el-tabs>
@ -83,23 +83,24 @@
</template> </template>
<script> <script>
import MsApiKeyValue from "../../ApiKeyValue"; import MsApiKeyValue from "../../ApiKeyValue";
import MsApiBody from "../../body/ApiBody"; import MsApiBody from "../../body/ApiBody";
import MsApiAuthConfig from "../../auth/ApiAuthConfig"; import MsApiAuthConfig from "../../auth/ApiAuthConfig";
import ApiRequestMethodSelect from "../../collapse/ApiRequestMethodSelect"; import ApiRequestMethodSelect from "../../collapse/ApiRequestMethodSelect";
import {REQUEST_HEADERS} from "@/common/js/constants"; import {REQUEST_HEADERS} from "@/common/js/constants";
import MsApiVariable from "../../ApiVariable"; import MsApiVariable from "../../ApiVariable";
import MsApiAssertions from "../../assertion/ApiAssertions"; import MsApiAssertions from "../../assertion/ApiAssertions";
import MsApiExtract from "../../extract/ApiExtract"; import MsApiExtract from "../../extract/ApiExtract";
import {Body, KeyValue} from "../../../model/ApiTestModel"; import {Body, KeyValue} from "../../../model/ApiTestModel";
import {hasLicense, getUUID} from "@/common/js/utils"; import {hasLicense, getUUID} from "@/common/js/utils";
import BatchAddParameter from "../../basis/BatchAddParameter"; import BatchAddParameter from "../../basis/BatchAddParameter";
import MsApiAdvancedConfig from "./ApiAdvancedConfig"; import MsApiAdvancedConfig from "./ApiAdvancedConfig";
import MsJsr233Processor from "../../../../automation/scenario/component/Jsr233Processor"; import MsJsr233Processor from "../../../../automation/scenario/component/Jsr233Processor";
import ApiDefinitionStepButton from "../components/ApiDefinitionStepButton"; import ApiDefinitionStepButton from "../components/ApiDefinitionStepButton";
import {hasPermission} from '@/common/js/utils'; import {hasPermission} from '@/common/js/utils';
import Convert from "@/business/components/common/json-schema/convert/convert"; import Convert from "@/business/components/common/json-schema/convert/convert";
export default {
export default {
name: "MsApiHttpRequestForm", name: "MsApiHttpRequestForm",
components: { components: {
ApiDefinitionStepButton, ApiDefinitionStepButton,
@ -118,8 +119,8 @@
method: String, method: String,
request: {}, request: {},
response: {}, response: {},
definitionTest:{ definitionTest: {
type:Boolean, type: Boolean,
default() { default() {
return false; return false;
} }
@ -209,19 +210,22 @@
let index = this.request.hashTree.indexOf(row); let index = this.request.hashTree.indexOf(row);
this.request.hashTree.splice(index, 1); this.request.hashTree.splice(index, 1);
this.reload(); this.reload();
}, }
,
copyRow(row) { copyRow(row) {
let obj = JSON.parse(JSON.stringify(row)); let obj = JSON.parse(JSON.stringify(row));
obj.id = getUUID(); obj.id = getUUID();
this.request.hashTree.push(obj); this.request.hashTree.push(obj);
this.reload(); this.reload();
}, }
,
reload() { reload() {
this.isReloadData = true this.isReloadData = true
this.$nextTick(() => { this.$nextTick(() => {
this.isReloadData = false this.isReloadData = false
}) })
}, }
,
init() { init() {
if (!this.request.body) { if (!this.request.body) {
this.request.body = new Body(); this.request.body = new Body();
@ -235,17 +239,20 @@
if (!this.request.arguments) { if (!this.request.arguments) {
this.request.arguments = []; this.request.arguments = [];
} }
}, }
// body ,
// body
reloadBody() { reloadBody() {
this.isBodyShow = false; this.isBodyShow = false;
this.$nextTick(() => { this.$nextTick(() => {
this.isBodyShow = true; this.isBodyShow = true;
}); });
}, }
,
batchAdd() { batchAdd() {
this.$refs.batchAddParameter.open(); this.$refs.batchAddParameter.open();
}, }
,
format(array, obj) { format(array, obj) {
if (array) { if (array) {
let isAdd = true; let isAdd = true;
@ -260,7 +267,8 @@
this.request.arguments.unshift(obj); this.request.arguments.unshift(obj);
} }
} }
}, }
,
batchSave(data) { batchSave(data) {
if (data) { if (data) {
let params = data.split("\n"); let params = data.split("\n");
@ -285,13 +293,13 @@
keyValues.forEach(item => { keyValues.forEach(item => {
switch (this.activeName) { switch (this.activeName) {
case "parameters": case "parameters":
this.format(this.request.arguments,item); this.format(this.request.arguments, item);
break; break;
case "rest": case "rest":
this.format(this.request.rest,item); this.format(this.request.rest, item);
break; break;
case "headers": case "headers":
this.format(this.request.headers,item); this.format(this.request.headers, item);
break; break;
default: default:
break; break;
@ -301,28 +309,31 @@
} }
} }
} }
</script> </script>
<style scoped> <style scoped>
.ms-query { .ms-query {
background: #783887; background: #783887;
color: white; color: white;
height: 18px; height: 18px;
border-radius: 42%; border-radius: 42%;
} }
.ms-header {
.ms-header {
background: #783887; background: #783887;
color: white; color: white;
height: 18px; height: 18px;
border-radius: 42%; border-radius: 42%;
} }
.request-tabs {
.request-tabs {
margin: 20px; margin: 20px;
min-height: 200px; min-height: 200px;
} }
.ms-el-link {
.ms-el-link {
float: right; float: right;
margin-right: 45px; margin-right: 45px;
} }
</style> </style>