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.ReferenceDTO;
import io.metersphere.api.dto.definition.*;
import io.metersphere.api.dto.definition.parse.ApiDefinitionImport;
import io.metersphere.api.service.ApiDefinitionService;
import io.metersphere.base.domain.ApiDefinition;
import io.metersphere.commons.constants.RoleConstants;
@ -93,7 +94,7 @@ public class ApiDefinitionController {
@PostMapping(value = "/import", consumes = {"multipart/form-data"})
@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);
}

View File

@ -13,5 +13,7 @@ public class ApiTestImportRequest {
private String projectId;
private String platform;
private Boolean useEnvironment;
// 来自场景的导入不需要存储
private boolean saved = true;
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());
ApiDefinitionImport apiImport = null;
try {
@ -345,8 +345,10 @@ public class ApiDefinitionService {
LogUtil.error(e.getMessage(), e);
MSException.throwException(Translator.get("parse_data_error"));
}
importApiTest(request, apiImport);
return "SUCCESS";
if (request.isSaved()) {
importApiTest(request, apiImport);
}
return apiImport;
}
private void importApiTest(ApiTestImportRequest importRequest, ApiDefinitionImport apiImport) {

View File

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

View File

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