refactor(接口自动化): 场景变量增加批量添加

This commit is contained in:
Captain.B 2021-07-06 16:27:46 +08:00 committed by 刘瑞斌
parent f3d1a29d6e
commit e1e98a8a24
1 changed files with 245 additions and 221 deletions

View File

@ -10,9 +10,11 @@
<div> <div>
<el-row style="margin-bottom: 10px"> <el-row style="margin-bottom: 10px">
<div style="float: left"> <div style="float: left">
<el-input :placeholder="$t('commons.search_by_name')" v-model="selectVariable" size="small" @change="filter" <el-input :placeholder="$t('commons.search_by_name')" v-model="selectVariable" size="small"
@change="filter"
@keyup.enter="filter"> @keyup.enter="filter">
<el-select v-model="searchType" slot="prepend" :placeholder="$t('test_resource_pool.type')" style="width: 90px" @change="filter"> <el-select v-model="searchType" slot="prepend" :placeholder="$t('test_resource_pool.type')"
style="width: 90px" @change="filter">
<el-option value="CONSTANT" :label="$t('api_test.automation.constant')"></el-option> <el-option value="CONSTANT" :label="$t('api_test.automation.constant')"></el-option>
<el-option value="LIST" :label="$t('test_track.case.list')"></el-option> <el-option value="LIST" :label="$t('test_track.case.list')"></el-option>
<el-option value="CSV" label="CSV"></el-option> <el-option value="CSV" label="CSV"></el-option>
@ -21,35 +23,41 @@
</el-select> </el-select>
</el-input> </el-input>
</div> </div>
<div style="float: right"> <div style="float: right">
<el-dropdown split-button type="primary" @command="handleClick" @click="handleClick('CONSTANT')" <el-dropdown split-button type="primary" @command="handleClick" @click="handleClick('CONSTANT')"
size="small" style="margin-left: 10px"> size="small" style="margin-left: 10px">
{{ $t('commons.add') }} {{ $t('commons.add') }}
<el-dropdown-menu slot="dropdown"> <el-dropdown-menu slot="dropdown">
<el-dropdown-item command="CONSTANT">{{$t('api_test.automation.constant')}}</el-dropdown-item> <el-dropdown-item command="CONSTANT">{{ $t('api_test.automation.constant') }}</el-dropdown-item>
<el-dropdown-item command="LIST">{{$t('test_track.case.list')}}</el-dropdown-item> <el-dropdown-item command="LIST">{{ $t('test_track.case.list') }}</el-dropdown-item>
<el-dropdown-item command="CSV">CSV</el-dropdown-item> <el-dropdown-item command="CSV">CSV</el-dropdown-item>
<el-dropdown-item command="COUNTER">{{$t('api_test.automation.counter')}}</el-dropdown-item> <el-dropdown-item command="COUNTER">{{ $t('api_test.automation.counter') }}</el-dropdown-item>
<el-dropdown-item command="RANDOM">{{$t('api_test.automation.random')}}</el-dropdown-item> <el-dropdown-item command="RANDOM">{{ $t('api_test.automation.random') }}</el-dropdown-item>
</el-dropdown-menu> </el-dropdown-menu>
</el-dropdown> </el-dropdown>
<el-button size="small" style="margin-left: 10px" @click="deleteVariable">{{ $t('commons.delete') }} <el-button size="small" style="margin-left: 10px" @click="deleteVariable">{{ $t('commons.delete') }}
</el-button> </el-button>
</div> </div>
<div style="float: right; margin-top: 10px">
<el-link @click="batchAddParameter" type="primary" :disabled="disabled">
{{ $t("commons.batch_add") }}
</el-link>
</div>
</el-row> </el-row>
<el-row> <el-row>
<el-col :span="12"> <el-col :span="12">
<div style="border:1px #DCDFE6 solid; min-height: 400px;border-radius: 4px ;width: 100% ;"> <div style="border:1px #DCDFE6 solid; min-height: 400px;border-radius: 4px ;width: 100% ;">
<el-table ref="table" border :data="variables" class="adjust-table" :row-class-name="tableRowClassName" <el-table ref="table" border :data="variables" class="adjust-table"
:row-class-name="tableRowClassName"
@select-all="select" @select-all="select"
@select="select" @select="select"
@row-click="edit" @row-click="edit"
v-loading="loading" height="400px"> v-loading="loading" height="400px">
<el-table-column type="selection" width="38"/> <el-table-column type="selection" width="38"/>
<el-table-column prop="num" label="ID" sortable width="60"/> <el-table-column prop="num" label="ID" sortable width="60"/>
<el-table-column prop="name" :label="$t('api_test.variable_name')" sortable show-overflow-tooltip/> <el-table-column prop="name" :label="$t('api_test.variable_name')" sortable
show-overflow-tooltip/>
<el-table-column prop="type" :label="$t('test_track.case.type')" width="70"> <el-table-column prop="type" :label="$t('test_track.case.type')" width="70">
<template v-slot:default="scope"> <template v-slot:default="scope">
<span>{{ types.get(scope.row.type) }}</span> <span>{{ types.get(scope.row.type) }}</span>
@ -57,6 +65,7 @@
</el-table-column> </el-table-column>
<el-table-column prop="value" :label="$t('api_test.value')" show-overflow-tooltip/> <el-table-column prop="value" :label="$t('api_test.value')" show-overflow-tooltip/>
</el-table> </el-table>
<batch-add-parameter @batchSave="batchSaveParameter" ref="batchAddParameter"/>
</div> </div>
</el-col> </el-col>
<el-col :span="12"> <el-col :span="12">
@ -82,12 +91,13 @@
</span> </span>
</el-tooltip> </el-tooltip>
<el-row> <el-row>
<el-link class="ms-variable-link" @click="batchAdd" style="color: #783887" :disabled="disabled"> {{ $t("commons.batch_add") }} <el-link class="ms-variable-link" @click="batchAddHeader" type="primary" :disabled="disabled">
{{ $t("commons.batch_add") }}
</el-link> </el-link>
</el-row> </el-row>
<div style="min-height: 400px"> <div style="min-height: 400px">
<ms-api-key-value :items="headers" :suggestions="headerSuggestions"/> <ms-api-key-value :items="headers" :suggestions="headerSuggestions"/>
<batch-add-parameter @batchSave="batchSave" ref="batchAddParameter"/> <batch-add-parameter @batchSave="batchSaveHeader" ref="batchAddHeader"/>
</div> </div>
</el-tab-pane> </el-tab-pane>
</el-tabs> </el-tabs>
@ -102,229 +112,243 @@
</template> </template>
<script> <script>
import MsEditConstant from "./EditConstant"; import MsEditConstant from "./EditConstant";
import MsDialogFooter from "../../../../common/components/MsDialogFooter"; import MsDialogFooter from "../../../../common/components/MsDialogFooter";
import MsTableHeader from "@/business/components/common/components/MsTableHeader"; import MsTableHeader from "@/business/components/common/components/MsTableHeader";
import MsTablePagination from "@/business/components/common/pagination/TablePagination"; import MsTablePagination from "@/business/components/common/pagination/TablePagination";
import MsEditCounter from "./EditCounter"; import MsEditCounter from "./EditCounter";
import MsEditRandom from "./EditRandom"; import MsEditRandom from "./EditRandom";
import MsEditListValue from "./EditListValue"; import MsEditListValue from "./EditListValue";
import MsEditCsv from "./EditCsv"; import MsEditCsv from "./EditCsv";
import {getUUID} from "@/common/js/utils"; import {getUUID} from "@/common/js/utils";
import MsApiKeyValue from "../../../definition/components/ApiKeyValue"; import MsApiKeyValue from "../../../definition/components/ApiKeyValue";
import BatchAddParameter from "../../../definition/components/basis/BatchAddParameter"; import BatchAddParameter from "../../../definition/components/basis/BatchAddParameter";
import {KeyValue} from "../../../definition/model/ApiTestModel"; import {KeyValue} from "../../../definition/model/ApiTestModel";
import {REQUEST_HEADERS} from "@/common/js/constants"; import {REQUEST_HEADERS} from "@/common/js/constants";
export default { export default {
name: "MsVariableList", name: "MsVariableList",
components: { components: {
MsEditConstant, MsEditConstant,
MsDialogFooter, MsDialogFooter,
MsTableHeader, MsTableHeader,
MsTablePagination, MsTablePagination,
MsEditCounter, MsEditCounter,
MsEditRandom, MsEditRandom,
MsEditListValue, MsEditListValue,
MsEditCsv, MsEditCsv,
MsApiKeyValue, MsApiKeyValue,
BatchAddParameter BatchAddParameter
},
data() {
return {
variables: [],
headers: [],
activeName: "variable",
searchType: "",
selectVariable: "",
condition: {},
types: new Map([
['CONSTANT', this.$t('api_test.automation.constant')],
['LIST', this.$t('test_track.case.list')],
['CSV', 'CSV'],
['COUNTER', this.$t('api_test.automation.counter')],
['RANDOM', this.$t('api_test.automation.random')]
]),
visible: false,
selection: [],
loading: false,
currentPage: 1,
editData: {},
pageSize: 10,
total: 0,
headerSuggestions: REQUEST_HEADERS,
disabled: false,
};
},
methods: {
batchAddParameter() {
this.$refs.batchAddParameter.open();
}, },
data() { batchAddHeader() {
return { this.$refs.batchAddHeader.open();
variables: [], },
headers: [], _handleBatchVars(data) {
activeName: "variable", let params = data.split("\n");
searchType: "", let keyValues = [];
selectVariable: "", params.forEach(item => {
condition: {}, let line = item.split(/|,/);
types: new Map([ let required = false;
['CONSTANT', this.$t('api_test.automation.constant')], if (line[1] === '必填' || line[1] === 'true') {
['LIST', this.$t('test_track.case.list')], required = true;
['CSV', 'CSV'], }
['COUNTER', this.$t('api_test.automation.counter')], keyValues.push(new KeyValue({
['RANDOM', this.$t('api_test.automation.random')] name: line[0],
]), required: required,
visible: false, value: line[2],
selection: [], description: line[3],
loading: false, type: "text",
currentPage: 1, valid: false,
editData: {}, file: false,
pageSize: 10, encode: true,
total: 0, enable: true,
headerSuggestions: REQUEST_HEADERS, contentType: "text/plain"
disabled: false, }));
});
return keyValues;
},
batchSaveHeader(data) {
if (data) {
let keyValues = this._handleBatchVars(data);
keyValues.forEach(item => {
this.headers.unshift(item);
});
} }
}, },
methods: { batchSaveParameter(data) {
batchAdd() { if (data) {
this.$refs.batchAddParameter.open(); let keyValues = this._handleBatchVars(data);
}, keyValues.forEach(item => {
batchSave(data) { item.type = 'CONSTANT';
if (data) { this.addParameters(item);
let params = data.split("\n"); });
let keyValues = []; }
params.forEach(item => { },
let line = item.split(/|,/); handleClick(command) {
let required = false; this.editData = {delimiter: ","};
if (line[1] === '必填' || line[1] === 'true') { this.editData.type = command;
required = true; this.addParameters(this.editData);
} },
keyValues.push(new KeyValue({ edit(row) {
name: line[0], this.$refs.table.clearSelection();
required: required, this.$refs.table.toggleRowSelection(row);
value: line[2], this.selection = [row.id];
description: line[3], this.editData = row;
type: "text", },
valid: false, tableRowClassName(row) {
file: false, if (row.row.hidden) {
encode: true, return 'ms-variable-hidden-row';
enable: true, }
contentType: "text/plain" return '';
})); },
}) addParameters(v) {
keyValues.forEach(item => { v.id = getUUID();
this.headers.unshift(item); if (v.type === 'CSV') {
}) v.delimiter = ",";
}
this.variables.push(v);
let index = 1;
this.variables.forEach(item => {
item.num = index;
index++;
});
},
select(selection) {
this.selection = selection.map(s => s.id);
},
isSelect(row) {
return this.selection.includes(row.id);
},
open: function (variables, headers, disabled) {
if (variables) {
this.variables = variables;
}
if (headers) {
this.headers = headers;
}
this.visible = true;
this.editData = {type: "CONSTANT"};
this.addParameters(this.editData);
this.disabled = disabled;
},
save() {
this.visible = false;
},
close() {
this.visible = false;
let saveVariables = [];
this.variables.forEach(item => {
item.hidden = undefined;
if (item.name && item.name != "") {
saveVariables.push(item);
} }
}, });
handleClick(command) { this.selectVariable = "";
this.editData = {delimiter: ","}; this.searchType = "";
this.editData.type = command; this.$emit('setVariables', saveVariables, this.headers);
this.addParameters(this.editData); },
}, deleteVariable() {
edit(row) { let ids = Array.from(this.selection);
this.$refs.table.clearSelection(); if (ids.length == 0) {
this.$refs.table.toggleRowSelection(row); this.$warning("请选择一条数据删除");
this.selection = [row.id]; return;
this.editData = row; }
}, ids.forEach(row => {
tableRowClassName(row) { const index = this.variables.findIndex(d => d.id === row);
if (row.row.hidden) { this.variables.splice(index, 1);
return 'ms-variable-hidden-row'; });
} this.selection = [];
return ''; this.editData = {type: "CONSTANT"};
}, },
addParameters(v) { filter() {
v.id = getUUID(); let datas = [];
if (v.type === 'CSV') { this.variables.forEach(item => {
v.delimiter = ","; if (this.searchType && this.searchType != "" && this.selectVariable && this.selectVariable != "") {
} if ((item.type && item.type.toLowerCase().indexOf(this.searchType.toLowerCase()) == -1) || (item.name && item.name.toLowerCase().indexOf(this.selectVariable.toLowerCase()) == -1)) {
this.variables.push(v); item.hidden = true;
let index = 1;
this.variables.forEach(item => {
item.num = index;
index++;
})
},
select(selection) {
this.selection = selection.map(s => s.id);
},
isSelect(row) {
return this.selection.includes(row.id)
},
open: function (variables, headers, disabled) {
if (variables) {
this.variables = variables;
}
if (headers) {
this.headers = headers;
}
this.visible = true;
this.editData = {type: "CONSTANT"};
this.addParameters(this.editData);
this.disabled = disabled;
},
save() {
this.visible = false;
},
close() {
this.visible = false;
let saveVariables = [];
this.variables.forEach(item => {
item.hidden = undefined;
if (item.name && item.name != "") {
saveVariables.push(item);
}
})
this.selectVariable = "";
this.searchType = "";
this.$emit('setVariables', saveVariables, this.headers);
},
deleteVariable() {
let ids = Array.from(this.selection);
if (ids.length == 0) {
this.$warning("请选择一条数据删除");
return;
}
ids.forEach(row => {
const index = this.variables.findIndex(d => d.id === row);
this.variables.splice(index, 1);
})
this.selection = [];
this.editData = {type: "CONSTANT"};
},
filter() {
let datas = [];
this.variables.forEach(item => {
if (this.searchType && this.searchType != "" && this.selectVariable && this.selectVariable != "") {
if ((item.type && item.type.toLowerCase().indexOf(this.searchType.toLowerCase()) == -1) || (item.name && item.name.toLowerCase().indexOf(this.selectVariable.toLowerCase()) == -1)) {
item.hidden = true;
} else {
item.hidden = undefined;
}
}
else if (this.selectVariable && this.selectVariable != "") {
if (item.name && item.name.toLowerCase().indexOf(this.selectVariable.toLowerCase()) == -1) {
item.hidden = true;
} else {
item.hidden = undefined;
}
}
else if (this.searchType && this.searchType != "") {
if (item.type && item.type.toLowerCase().indexOf(this.searchType.toLowerCase()) == -1) {
item.hidden = true;
} else {
item.hidden = undefined;
}
} else { } else {
item.hidden = undefined; item.hidden = undefined;
} }
datas.push(item); } else if (this.selectVariable && this.selectVariable != "") {
}) if (item.name && item.name.toLowerCase().indexOf(this.selectVariable.toLowerCase()) == -1) {
this.variables = datas; item.hidden = true;
}, } else {
createFilter(queryString) { item.hidden = undefined;
return item => { }
return (item.type && item.type.toLowerCase().indexOf(queryString.toLowerCase()) !== -1); } else if (this.searchType && this.searchType != "") {
}; if (item.type && item.type.toLowerCase().indexOf(this.searchType.toLowerCase()) == -1) {
}, item.hidden = true;
} } else {
item.hidden = undefined;
}
} else {
item.hidden = undefined;
}
datas.push(item);
});
this.variables = datas;
},
createFilter(queryString) {
return item => {
return (item.type && item.type.toLowerCase().indexOf(queryString.toLowerCase()) !== -1);
};
},
} }
};
</script> </script>
<style> <style>
.ms-variable-hidden-row { .ms-variable-hidden-row {
display: none; display: none;
} }
.ms-variable-header { .ms-variable-header {
background: #783887; background: #783887;
color: white; color: white;
height: 18px; height: 18px;
border-radius: 42%; border-radius: 42%;
} }
.ms-variable-link { .ms-variable-link {
float: right; float: right;
margin-right: 45px; margin-right: 45px;
} }
fieldset { fieldset {
padding: 0px; padding: 0px;
margin: 0px; margin: 0px;
min-width: 100%; min-width: 100%;
min-inline-size: 0px; min-inline-size: 0px;
border: 0px; border: 0px;
} }
</style> </style>