CreateTestPlan.vue 4.9 KB
Newer Older
H
haifeng414 已提交
1
<template>
H
haifeng414 已提交
2
  <div class="edit-testplan-container">
H
haifeng414 已提交
3 4
    <el-row>
      <el-col :span="10">
H
haifeng414 已提交
5
        <el-input placeholder="请输入名称" v-model="testPlan.name" class="input-with-select">
6
          <el-select v-model="testPlan.projectId" slot="prepend" placeholder="请选择项目">
H
haifeng414 已提交
7 8 9 10
            <el-option
              v-for="item in projects"
              :key="item.id"
              :label="item.name"
H
haifeng414 已提交
11
              :value="item.id">
H
haifeng414 已提交
12 13 14 15 16 17 18 19 20
            </el-option>
          </el-select>
        </el-input>
      </el-col>
      <el-button type="primary" plain @click="save">保存</el-button>
      <el-button type="primary" plain @click="saveAndRun">保存并执行</el-button>
      <el-button type="warning" plain @click="cancel">取消</el-button>
    </el-row>

H
haifeng414 已提交
21
    <el-tabs v-model="active" type="border-card" :stretch="true">
H
haifeng414 已提交
22
      <el-tab-pane label="基础配置">
23
        <test-plan-basic-config :test-plan="testPlan"/>
H
haifeng414 已提交
24 25
      </el-tab-pane>
      <el-tab-pane label="压力配置">
26
        <test-plan-pressure-config/>
H
haifeng414 已提交
27 28
      </el-tab-pane>
      <el-tab-pane label="高级配置">
29
        <test-plan-advanced-config/>
H
haifeng414 已提交
30 31 32 33 34 35
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
H
haifeng414 已提交
36 37 38
  import TestPlanBasicConfig from './components/BasicConfig';
  import TestPlanPressureConfig from './components/PressureConfig';
  import TestPlanAdvancedConfig from './components/AdvancedConfig';
H
haifeng414 已提交
39 40 41 42

  export default {
    name: "CreateTestPlan",
    components: {
H
haifeng414 已提交
43 44 45
      TestPlanBasicConfig,
      TestPlanPressureConfig,
      TestPlanAdvancedConfig,
H
haifeng414 已提交
46
    },
H
haifeng414 已提交
47
    props: ['testPlanObj'],
H
haifeng414 已提交
48
    data() {
H
haifeng414 已提交
49
      return {
H
haifeng414 已提交
50
        testPlan: {},
51
        listProjectPath: "/project/listAll",
H
haifeng414 已提交
52
        savePath: "/testplan/save",
53
        editPath: "/testplan/edit",
H
haifeng414 已提交
54
        runPath: "/testplan/run",
55
        projects: [],
H
haifeng414 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
        active: '0',
        tabs: [{
          title: '场景配置',
          id: '0',
          component: 'BasicConfig'
        }, {
          title: '压力配置',
          id: '1',
          component: 'PressureConfig'
        }, {
          title: '高级配置',
          id: '2',
          component: 'AdvancedConfig'
        }]
      }
H
haifeng414 已提交
71
    },
H
haifeng414 已提交
72 73 74 75
    created() {
      if (this.testPlanObj) {
        this.testPlan = this.testPlanObj;
      }
76
      this.listProjects();
H
haifeng414 已提交
77
    },
H
haifeng414 已提交
78
    methods: {
79
      listProjects() {
H
haifeng414 已提交
80 81
        this.$get(this.listProjectPath, response => {
          this.projects = response.data;
82
        })
83
      },
H
haifeng414 已提交
84
      save() {
H
haifeng414 已提交
85 86 87
        if (!this.validTestPlan()) {
          return;
        }
H
haifeng414 已提交
88

H
haifeng414 已提交
89 90
        let options = this.getSaveOption();

H
haifeng414 已提交
91 92 93 94 95
        this.$request(options, () => {
          this.$message({
            message: '保存成功!',
            type: 'success'
          });
H
haifeng414 已提交
96 97 98 99 100 101 102 103 104
        });
      },
      saveAndRun() {
        if (!this.validTestPlan()) {
          return;
        }

        let options = this.getSaveOption();

H
haifeng414 已提交
105 106 107 108 109 110 111
        this.$request(options, () => {
          this.$message({
            message: '保存成功!',
            type: 'success'
          });

          this.$post(this.runPath, {id: this.testPlan.id}).then(() => {
H
haifeng414 已提交
112
            this.$message({
H
haifeng414 已提交
113
              message: '正在运行!',
H
haifeng414 已提交
114 115
              type: 'success'
            });
H
haifeng414 已提交
116
          })
H
haifeng414 已提交
117 118 119
        });
      },
      getSaveOption() {
H
haifeng414 已提交
120
        let formData = new FormData();
121
        let url = this.testPlan.id ? this.editPath : this.savePath;
H
haifeng414 已提交
122

123 124 125
        if (!this.testPlan.file.id) {
          formData.append("file", this.testPlan.file);
        }
H
haifeng414 已提交
126
        // file属性不需要json化
127 128 129
        let requestJson = JSON.stringify(this.testPlan, function (key, value) {
          return key === "file" ? undefined : value
        });
H
haifeng414 已提交
130 131 132 133
        formData.append('request', new Blob([requestJson], {
          type: "application/json"
        }));

H
haifeng414 已提交
134
        return {
H
haifeng414 已提交
135
          method: 'POST',
136
          url: url,
H
haifeng414 已提交
137 138 139 140 141
          data: formData,
          headers: {
            'Content-Type': undefined
          }
        };
H
haifeng414 已提交
142
      },
H
haifeng414 已提交
143 144 145
      cancel() {
        this.$router.push({path: '/'})
      },
H
haifeng414 已提交
146 147 148 149 150 151 152 153 154 155
      validTestPlan() {
        if (!this.testPlan.name) {
          this.$message({
            message: '测试名称不能为空!',
            type: 'error'
          });

          return false;
        }

H
haifeng414 已提交
156
        if (!this.testPlan.projectId) {
H
haifeng414 已提交
157 158 159 160 161 162 163 164
          this.$message({
            message: '项目不能为空!',
            type: 'error'
          });

          return false;
        }

H
haifeng414 已提交
165
        if (!this.testPlan.file) {
H
haifeng414 已提交
166 167 168 169 170 171 172 173 174 175
          this.$message({
            message: 'jmx文件不能为空!',
            type: 'error'
          });

          return false;
        }

        /// todo: 其他校验
        return true;
H
haifeng414 已提交
176
      }
H
haifeng414 已提交
177 178 179
    }
  }
</script>
H
haifeng414 已提交
180 181

<style>
H
haifeng414 已提交
182
  .edit-testplan-container .el-tabs__nav {
H
haifeng414 已提交
183 184 185
    float: none;
    text-align: center;
  }
H
haifeng414 已提交
186

H
haifeng414 已提交
187
  .edit-testplan-container .el-select .el-input {
H
haifeng414 已提交
188 189 190
    width: 130px;
  }

H
haifeng414 已提交
191
  .edit-testplan-container .input-with-select .el-input-group__prepend {
H
haifeng414 已提交
192 193
    background-color: #fff;
  }
H
haifeng414 已提交
194
</style>