hyb
2025-05-20 e8003b5c66494c398fa8b716e0872771e2ea4af8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<template>
    <el-table
        highlight-current-row
        :cell-style="{ paddingTop: '4px', paddingBottom: '4px' }"
        stripe
        :height="height"
        :data="tableData"
        style="width: 100%;"
        @cell-mouse-enter="cellMouseEnter"
        @cell-mouse-leave="cellMouseLeave"
    >
        <el-table-column label="测试之前执行的方法" width="500">
            <template v-slot="scope">
                <el-input
                    clearable
                    v-model.trim="scope.row.setup"
                    placeholder="${ setup_hooks function($request, *args, **kwargs) }"
                ></el-input>
            </template>
        </el-table-column>
 
        <el-table-column label="测试之后执行的方法" width="500">
            <template v-slot="scope">
                <el-input
                    clearable
                    v-model.trim="scope.row.teardown"
                    placeholder="${ teardown_hooks function($response, *args, **kwargs) }"
                ></el-input>
            </template>
        </el-table-column>
 
        <el-table-column>
            <template v-slot="scope">
                <el-row v-show="scope.row === currentRow">
                    <el-button
                        icon="el-icon-circle-plus-outline"
                        size="mini"
                        type="info"
                        @click="addHooks"
                    ></el-button>
                    <el-button
                        icon="el-icon-delete"
                        size="mini"
                        type="danger"
                        v-show="scope.$index !== 0"
                        @click="deleteHooks(scope.$index)"
                    ></el-button>
                </el-row>
            </template>
        </el-table-column>
    </el-table>
</template>
 
<script>
import { isEqual } from "lodash";
 
export default {
    name: "Hooks",
    data() {
        return {
            currentRow: "",
            originalTableData: [],
            tableData: [
                {
                    setup: "",
                    teardown: ""
                }
            ]
        };
    },
    props: {
        save: Boolean,
        hooks: {
            required: false
        }
    },
    computed: {
        height() {
            return window.screen.height - 440;
        }
    },
    watch: {
        save() {
            this.$emit("hooks", this.parse_hooks(), this.tableData);
        },
        tableData: {
            deep: true,
            handler() {
                this.checkDataChanges();
            }
        },
        hooks: {
            deep: true,
            handler(newVal) {
                if (newVal.length !== 0) {
                    this.tableData = JSON.parse(JSON.stringify(newVal));
                    this.setOriginalData();
                }
            }
        }
    },
    methods: {
        setOriginalData() {
            this.originalTableData = JSON.parse(JSON.stringify(this.tableData));
        },
        checkDataChanges() {
            const hasChanged = !isEqual(this.originalTableData, this.tableData);
            this.$emit("dataChanged", hasChanged);
        },
        cellMouseEnter(row) {
            this.currentRow = row;
        },
        cellMouseLeave(row) {
            this.currentRow = "";
        },
        addHooks(index, row, flag) {
            this.tableData.push({
                setup: "",
                teardown: ""
            });
        },
        deleteHooks(index, row) {
            this.tableData.splice(index, 1);
        },
        parse_hooks() {
            let hooks = {
                setup_hooks: [],
                teardown_hooks: []
            };
            for (let content of this.tableData) {
                if (content.setup !== "") {
                    hooks.setup_hooks.push(content.setup);
                }
                if (content.teardown !== "") {
                    hooks.teardown_hooks.push(content.teardown);
                }
            }
            return hooks;
        }
    },
    mounted() {
        this.setOriginalData();
    }
};
</script>
 
<style scoped></style>