|
1 | 1 | import React, { Component } from 'react';
|
2 |
| -import logo from './logo.svg'; |
| 2 | +import GlobalInput from './component/GlobalInput'; |
| 3 | +import Interface from './component/Interface'; |
| 4 | +import datasource from './test_data'; |
3 | 5 | import './App.css';
|
| 6 | +import VConsole from 'vconsole'; |
| 7 | +import Axios from 'axios'; |
| 8 | + |
| 9 | +const vcon = new VConsole(); |
| 10 | +vcon.setOption('maxLogNumber', 500); |
4 | 11 |
|
5 | 12 | class App extends Component {
|
| 13 | + constructor(props) { |
| 14 | + super(props); |
| 15 | + this.state = { |
| 16 | + tc:JSON.parse(datasource), |
| 17 | + origin:"dock", |
| 18 | + status:[],//0 空闲,1正在执行, 2 成功, 3失败 state describe |
| 19 | + }; |
| 20 | + this.onGlobalInputChanged = this.onGlobalInputChanged.bind(this); |
| 21 | + this.onApiInputChanged = this.onApiInputChanged.bind(this); |
| 22 | + this.onRunApi = this.onRunApi.bind(this); |
| 23 | + this.onRunAll = this.onRunAll.bind(this); |
| 24 | + } |
| 25 | + componentDidMount() { |
| 26 | + |
| 27 | + } |
| 28 | + |
| 29 | + //---------------------------support function -----------------------// |
| 30 | + currentOrigin() { |
| 31 | + return this.state.tc.origin[this.state.origin]; |
| 32 | + } |
| 33 | + |
| 34 | + apiFromIndex(index) { |
| 35 | + const {tc} = this.state; |
| 36 | + return tc.interface[index]; |
| 37 | + } |
| 38 | + |
| 39 | + varValue(api, token) { |
| 40 | + if (api.input.hasOwnProperty(token)) { |
| 41 | + return api.input[token]; |
| 42 | + } |
| 43 | + else { |
| 44 | + var res = this.state.tc.global_input[token]; |
| 45 | + if(res === undefined) { |
| 46 | + res = ''; |
| 47 | + } |
| 48 | + return res; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + wholeUrl(api) { |
| 53 | + var reg = /\$\{.+?\}/g; |
| 54 | + var retArray; |
| 55 | + var path = api.path; |
| 56 | + while((retArray = reg.exec(api.path)) !== null) { |
| 57 | + var target = retArray[0].substring(2, retArray[0].length-1); |
| 58 | + path = path.replace(retArray[0], this.varValue(api, target)); |
| 59 | + } |
| 60 | + return this.currentOrigin() + path; |
| 61 | + } |
| 62 | + |
| 63 | + requestApi(api, index) { |
| 64 | + var {status} = this.state; |
| 65 | + status[index] = { |
| 66 | + state:1, |
| 67 | + describe:'', |
| 68 | + } |
| 69 | + this.setState({ |
| 70 | + status:status, |
| 71 | + }); |
| 72 | + Axios({ |
| 73 | + method:api.method, |
| 74 | + url:this.wholeUrl(api), |
| 75 | + |
| 76 | + }).then((response)=>{ |
| 77 | + var RET = response.data; |
| 78 | + var selfAssert = eval(api.assert); |
| 79 | + console.log(`api index ${index} ,assert result:${selfAssert}`); |
| 80 | + var newStatus = this.state.status; |
| 81 | + newStatus[index] = { |
| 82 | + state:selfAssert?2:3, |
| 83 | + describe:JSON.stringify(RET), |
| 84 | + }; |
| 85 | + this.setState({ |
| 86 | + status:newStatus, |
| 87 | + }); |
| 88 | + }).catch((error)=>{ |
| 89 | + console.log(error); |
| 90 | + var newStatus = this.state.status; |
| 91 | + var errorDict = error; |
| 92 | + if (error.response) { |
| 93 | + errorDict = error.response; |
| 94 | + if(error.response.data) { |
| 95 | + errorDict = error.response.data; |
| 96 | + } |
| 97 | + } |
| 98 | + newStatus[index] = { |
| 99 | + state:3, |
| 100 | + describe:JSON.stringify(error.response.data), |
| 101 | + }; |
| 102 | + this.setState({ |
| 103 | + status:newStatus, |
| 104 | + }); |
| 105 | + } |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + //----------------------------event response -----------------------// |
| 110 | + onRunAll(){ |
| 111 | + const {tc} = this.state; |
| 112 | + tc.interface.forEach( |
| 113 | + (element, index) => { |
| 114 | + this.requestApi(element, index); |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + onGlobalInputChanged(key,value) { |
| 119 | + const {tc} = this.state; |
| 120 | + tc.global_input[key] = value; |
| 121 | + this.setState({ |
| 122 | + tc:tc, |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + onApiInputChanged(index,key, value) { |
| 127 | + const {tc} = this.state; |
| 128 | + tc.interface[index].input[key] = value; |
| 129 | + this.setState({ |
| 130 | + tc:tc, |
| 131 | + }); |
| 132 | + } |
| 133 | + |
| 134 | + onRunApi(index) { |
| 135 | + console.log(`run api index:${index}`); |
| 136 | + var api = this.apiFromIndex(index); |
| 137 | + this.requestApi(api,index); |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | + |
6 | 142 | render() {
|
| 143 | + const {tc} = this.state; |
7 | 144 | return (
|
8 | 145 | <div className="App">
|
9 |
| - <header className="App-header"> |
10 |
| - <img src={logo} className="App-logo" alt="logo" /> |
11 |
| - <p> |
12 |
| - Edit <code>src/App.js</code> and save to reload. |
13 |
| - </p> |
14 |
| - <a |
15 |
| - className="App-link" |
16 |
| - href="https://reactjs.org" |
17 |
| - target="_blank" |
18 |
| - rel="noopener noreferrer" |
19 |
| - > |
20 |
| - Learn React |
21 |
| - </a> |
22 |
| - </header> |
| 146 | + <h1>{tc.name}</h1> |
| 147 | + <div className="meta-info"> |
| 148 | + <div>环境:{this.state.origin}, origin:{this.currentOrigin()}</div> |
| 149 | + <GlobalInput global_input={tc.global_input} |
| 150 | + onGlobalInputChanged={this.onGlobalInputChanged} |
| 151 | + /> |
| 152 | + <button onClick={this.onRunAll}>全部运行</button> |
| 153 | + </div> |
| 154 | + <Interface interface ={tc.interface} |
| 155 | + onApiInputChanged={this.onApiInputChanged} |
| 156 | + onRunApi = {this.onRunApi} |
| 157 | + status ={this.state.status} |
| 158 | + /> |
23 | 159 | </div>
|
24 | 160 | );
|
25 | 161 | }
|
|
0 commit comments