feat(接口自动化): 增加外部导入到场景功能

This commit is contained in:
fit2-zhao 2020-12-16 14:14:00 +08:00
parent 00415e6084
commit 86c6254d48
6 changed files with 84 additions and 45 deletions

View File

@ -7,6 +7,7 @@ import io.metersphere.api.dto.ApiTestImportRequest;
import io.metersphere.api.dto.automation.ApiScenarioRequest; import io.metersphere.api.dto.automation.ApiScenarioRequest;
import io.metersphere.api.dto.automation.ReferenceDTO; import io.metersphere.api.dto.automation.ReferenceDTO;
import io.metersphere.api.dto.definition.*; import io.metersphere.api.dto.definition.*;
import io.metersphere.api.dto.definition.parse.ApiDefinitionImport;
import io.metersphere.api.service.ApiDefinitionService; import io.metersphere.api.service.ApiDefinitionService;
import io.metersphere.base.domain.ApiDefinition; import io.metersphere.base.domain.ApiDefinition;
import io.metersphere.commons.constants.RoleConstants; import io.metersphere.commons.constants.RoleConstants;
@ -93,7 +94,7 @@ public class ApiDefinitionController {
@PostMapping(value = "/import", consumes = {"multipart/form-data"}) @PostMapping(value = "/import", consumes = {"multipart/form-data"})
@RequiresRoles(value = {RoleConstants.TEST_USER, RoleConstants.TEST_MANAGER}, logical = Logical.OR) @RequiresRoles(value = {RoleConstants.TEST_USER, RoleConstants.TEST_MANAGER}, logical = Logical.OR)
public String testCaseImport(@RequestPart(value = "file", required = false) MultipartFile file, @RequestPart("request") ApiTestImportRequest request) { public ApiDefinitionImport testCaseImport(@RequestPart(value = "file", required = false) MultipartFile file, @RequestPart("request") ApiTestImportRequest request) {
return apiDefinitionService.apiTestImport(file, request); return apiDefinitionService.apiTestImport(file, request);
} }

View File

@ -13,5 +13,7 @@ public class ApiTestImportRequest {
private String projectId; private String projectId;
private String platform; private String platform;
private Boolean useEnvironment; private Boolean useEnvironment;
// 来自场景的导入不需要存储
private boolean saved = true;
private String swaggerUrl; private String swaggerUrl;
} }

View File

@ -336,7 +336,7 @@ public class ApiDefinitionService {
} }
public String apiTestImport(MultipartFile file, ApiTestImportRequest request) { public ApiDefinitionImport apiTestImport(MultipartFile file, ApiTestImportRequest request) {
ApiImportParser apiImportParser = ApiImportParserFactory.getApiImportParser(request.getPlatform()); ApiImportParser apiImportParser = ApiImportParserFactory.getApiImportParser(request.getPlatform());
ApiDefinitionImport apiImport = null; ApiDefinitionImport apiImport = null;
try { try {
@ -345,8 +345,10 @@ public class ApiDefinitionService {
LogUtil.error(e.getMessage(), e); LogUtil.error(e.getMessage(), e);
MSException.throwException(Translator.get("parse_data_error")); MSException.throwException(Translator.get("parse_data_error"));
} }
importApiTest(request, apiImport); if (request.isSaved()) {
return "SUCCESS"; importApiTest(request, apiImport);
}
return apiImport;
} }
private void importApiTest(ApiTestImportRequest importRequest, ApiDefinitionImport apiImport) { private void importApiTest(ApiTestImportRequest importRequest, ApiDefinitionImport apiImport) {

View File

@ -267,6 +267,8 @@
<!--场景公共参数--> <!--场景公共参数-->
<ms-scenario-parameters :currentScenario="currentScenario" @addParameters="addParameters" ref="scenarioParameters"/> <ms-scenario-parameters :currentScenario="currentScenario" @addParameters="addParameters" ref="scenarioParameters"/>
<!--外部导入-->
<api-import ref="apiImport" :saved="false" @refresh="apiImport"/>
</div> </div>
</el-card> </el-card>
</template> </template>
@ -293,6 +295,7 @@
import MsApiScenarioComponent from "./ApiScenarioComponent"; import MsApiScenarioComponent from "./ApiScenarioComponent";
import MsApiReportDetail from "../report/ApiReportDetail"; import MsApiReportDetail from "../report/ApiReportDetail";
import MsScenarioParameters from "./ScenarioParameters"; import MsScenarioParameters from "./ScenarioParameters";
import ApiImport from "../../definition/components/import/ApiImport";
export default { export default {
name: "EditApiScenario", name: "EditApiScenario",
@ -301,13 +304,21 @@
currentScenario: {}, currentScenario: {},
}, },
components: { components: {
ApiEnvironmentConfig, MsScenarioParameters, ApiEnvironmentConfig,
MsApiReportDetail, MsAddTag, MsRun, MsScenarioParameters,
MsApiScenarioComponent, MsImportApiScenario, MsApiReportDetail,
MsJsr233Processor, MsConstantTimer, MsAddTag, MsRun,
MsIfController, MsApiAssertions, MsApiScenarioComponent,
MsApiExtract, MsApiDefinition, MsImportApiScenario,
MsApiComponent, MsApiCustomize MsJsr233Processor,
MsConstantTimer,
MsIfController,
MsApiAssertions,
MsApiExtract,
MsApiDefinition,
MsApiComponent,
MsApiCustomize,
ApiImport,
}, },
data() { data() {
return { return {
@ -402,6 +413,7 @@
this.scenarioVisible = true; this.scenarioVisible = true;
break; break;
default: default:
this.$refs.apiImport.open();
break; break;
} }
this.sort(); this.sort();
@ -622,36 +634,40 @@
}, },
setFiles(item, bodyUploadFiles, obj) { setFiles(item, bodyUploadFiles, obj) {
if (item.body) { if (item.body) {
item.body.kvs.forEach(param => { if (item.body.kvs) {
if (param.files) { item.body.kvs.forEach(param => {
param.files.forEach(item => { if (param.files) {
if (item.file) { param.files.forEach(item => {
if (!item.id) { if (item.file) {
let fileId = getUUID().substring(0, 12); if (!item.id) {
item.name = item.file.name; let fileId = getUUID().substring(0, 12);
item.id = fileId; item.name = item.file.name;
item.id = fileId;
}
obj.bodyUploadIds.push(item.id);
bodyUploadFiles.push(item.file);
} }
obj.bodyUploadIds.push(item.id); });
bodyUploadFiles.push(item.file); }
} });
}); }
} if (item.body.binary) {
}); item.body.binary.forEach(param => {
item.body.binary.forEach(param => { if (param.files) {
if (param.files) { param.files.forEach(item => {
param.files.forEach(item => { if (item.file) {
if (item.file) { if (!item.id) {
if (!item.id) { let fileId = getUUID().substring(0, 12);
let fileId = getUUID().substring(0, 12); item.name = item.file.name;
item.name = item.file.name; item.id = fileId;
item.id = fileId; }
obj.bodyUploadIds.push(item.id);
bodyUploadFiles.push(item.file);
} }
obj.bodyUploadIds.push(item.id); });
bodyUploadFiles.push(item.file); }
} });
}); }
}
});
} }
}, },
recursiveFile(arr, bodyUploadFiles, obj) { recursiveFile(arr, bodyUploadFiles, obj) {
@ -736,6 +752,15 @@
addParameters(data) { addParameters(data) {
this.currentScenario.variables = data; this.currentScenario.variables = data;
this.reload(); this.reload();
},
apiImport(importData) {
if (importData && importData.data) {
importData.data.forEach(item => {
this.setApiParameter(item, "API", "Copy");
})
this.sort();
this.reload();
}
} }
} }
} }

View File

@ -225,7 +225,7 @@
if (!this.$refs.apiList[0].tableData) { if (!this.$refs.apiList[0].tableData) {
return; return;
} }
let obj = {protocol: this.currentProtocol, data: this.$refs.apiList[0].tableData} let obj = {projectName: getCurrentProjectID(), protocol: this.currentProtocol, data: this.$refs.apiList[0].tableData}
downloadFile("导出API.json", JSON.stringify(obj)); downloadFile("导出API.json", JSON.stringify(obj));
}, },
refresh(data) { refresh(data) {
@ -255,7 +255,7 @@
changeProtocol(data) { changeProtocol(data) {
this.currentProtocol = data; this.currentProtocol = data;
}, },
showExecResult(row){ showExecResult(row) {
this.debug(row); this.debug(row);
}, },

View File

@ -70,6 +70,12 @@
export default { export default {
name: "ApiImport", name: "ApiImport",
components: {MsDialogFooter}, components: {MsDialogFooter},
props: {
saved: {
type: Boolean,
default: true,
}
},
data() { data() {
return { return {
visible: false, visible: false,
@ -148,7 +154,7 @@
}, },
uploadValidate(file, fileList) { uploadValidate(file, fileList) {
let suffix = file.name.substring(file.name.lastIndexOf('.') + 1); let suffix = file.name.substring(file.name.lastIndexOf('.') + 1);
if (!this.selectedPlatform.suffixes.has(suffix)) { if (this.selectedPlatform.suffixes && !this.selectedPlatform.suffixes.has(suffix)) {
this.$warning(this.$t('api_test.api_import.suffixFormatErr')); this.$warning(this.$t('api_test.api_import.suffixFormatErr'));
return false; return false;
} }
@ -170,7 +176,7 @@
let res = response.data; let res = response.data;
this.$success(this.$t('test_track.case.import.success')); this.$success(this.$t('test_track.case.import.success'));
this.visible = false; this.visible = false;
this.$emit('refresh'); this.$emit('refresh', res);
}); });
} else { } else {
return false; return false;
@ -181,8 +187,11 @@
let param = {}; let param = {};
Object.assign(param, this.formData); Object.assign(param, this.formData);
param.platform = this.selectedPlatformValue; param.platform = this.selectedPlatformValue;
param.moduleId = this.currentModule.id; param.saved = this.saved;
param.modulePath = this.currentModule.path; if (this.currentModule) {
param.moduleId = this.currentModule.id;
param.modulePath = this.currentModule.path;
}
param.projectId = getCurrentProjectID(); param.projectId = getCurrentProjectID();
if (!this.swaggerUrlEable) { if (!this.swaggerUrlEable) {
param.swaggerUrl = undefined; param.swaggerUrl = undefined;