vue学习,设计一个测试前后端分离开发时的测试工具

news/2024/7/10 0:21:22 标签: vue, 测试工具

文章目录

  • why?
  • what?
  • how?
    • 工具效果:
    • 工具代码:

why?

在使用vue-cli进行前后端项目分离开发时,每次测试,我都需要将vue项目build成一个文件放入web项目中,因此我在开发前端时,会用静态数据来做测试。
但后台Control层的测试,在涉及到多种 request方法的使用,如Get、Post、Delete、Put
因此,以前在浏览器url栏里直接输入请求的方式来测试就不再可行

what?

我自己写的这个工具使用了 : vue + jQuery(ajax)框架
是一个html文件

how?

工具效果:

在这里插入图片描述

工具代码:

<!DOCTYPE html>
<html>
   <head>
   	<meta charset="utf-8">
   	<title>测试交互数据</title>
   	<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
   	<script src="https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.js"></script>
   	<!-- author: gz -->
   </head>
   <body>
   	<div id="app">
   		<div id="input">
   			<strong>Ajax.URL:</strong>
   			<label v-text="pre_url" @click="update_pre_url()" ></label>
   			<input type="text" value="re" v-model="req_url" />
   			<button class="btn" @click="go()">Go</button>
   			<input type="text" v-model="req_method" class="method" />
   			<div class="ajust_v"></div>
   			<strong>参数名称用英文逗号隔开:</strong>
   			<input type="text" v-model="req_param_str" />
   			<button @click="addParam()" class="btn">+</button>
   			<button class="btn" @click="reset()">reset</button>
   			<div class="ajust_v"></div>
   			<div class="ajust_v" v-for="(word, key) in req_param" :key="key">
   				<label v-text="key + ' :'"></label>
   				<input type="text" v-model="req_param[key]" />
   			</div>
   		</div>
   		<hr />
   		<div id="result" v-show="resp_data.code != undefined ">
   			<strong v-text="'Code :' + resp_data.code"></strong>
   			<div class="ajust_v"></div>
   			<strong>message:</strong>
   			<div v-text="resp_data.message"></div>
   			<strong>data:</strong>
   			<table v-if="resp_data_table.length > 0" border="1" frame="none" >
   				<tr >
   					<td v-for="(word, key) in resp_data_table[0]" :key="key" v-text="key"></td>
   				</tr>
   				<tr v-for="(item, index) in resp_data_table" :key="index">
   					<td v-for="(word, key) in item" v-text="word"></td>
   				</tr>
   			</table>
   			<div v-if="resp_data_code.length > 0" v-text="resp_data_code"></div>
   		</div>
   	</div>
   	<script>
   		var vm = new Vue({
   			el: '#app',
   			data: {
   				msg: 'hehe',
   				pre_url: 'localhost:8080', // url前缀
   				req_url: '/test',
   				req_param_str: '',
   				req_param: {},
   				req_method: 'post',
   				resp_data: {},
   				resp_data_table: [],
   				resp_data_code: ''
   			},
   			methods: {
   				update_pre_url(){
   				   this.pre_url=prompt("新的url前缀:", this.pre_url)
   				},
   				/* 添加参数 */
   				addParam() {
   					if (typeof this.req_param_str == undefined || this.req_param_str == '')
   						return
   					var tmp_list = this.req_param_str.split(',')
   					for (var pa in tmp_list) {
   						Vue.set(this.req_param, tmp_list[pa], '')
   					}
   					this.req_param_str = ""
   				},
   				/* 重置参数 */
   				reset() {
   					this.req_param = {}
   				},
   				/* 发送请求 */
   				go() {
   					this.resp_data_code = ""
   					this.resp_data_table = []
   					var m_url = this.req_url
   					var datas = this.req_param
   					var method = this.req_method
   					var tmp_resp = ''
   					$.ajax({
   						url: m_url,
   						type: method,
   						data: datas,
   						async: false,
   						success(data) {
   							tmp_resp = data
   						}
   					})
   					this.resp_data = tmp_resp
   					if (typeof this.resp_data.data != undefined && typeof this.resp_data.data != '') {
   						if (this.resp_data.data instanceof Object) {
   							this.resp_data_table.push(this.resp_data.data)
   						} else if (this.resp_data.data instanceof Array) {
   							this.resp_data_table = this.resp_data.data
   						} else {
   							this.resp_data_code = this.resp_data.data
   						}
   					} else {
   						console.log("resp_data.data : error : " + resp_data.data)
   					}
   				}
   			}
   		})
   	</script>
   	<style>
   		* {
   			margin: 0;
   			padding: 0;
   		}

   		#app {
   			width: 500px;
   			margin: 0 auto;
   			margin-top: 20px;
   			padding: 10px;
   			border: 3px solid antiquewhite;
   		}

   		.btn {
   			padding: 2px 10px;
   			border: none;
   			background-color: aquamarine;
   			border-radius: 4px;
   		}

   		.ajust_v {
   			margin: 5px;
   		}

   		.method {
   			width: 50px;
   		}

   		td{
   			padding: 5px;
   		}
   	</style>
   </body>
</html>


http://www.niftyadmin.cn/n/1152304.html

相关文章

(错误记录)Vue: Unknown custom element

错误&#xff1a; vue.js:634 [Vue warn]: Unknown custom element: <ve-pie> - did you register the component correctly? For recursive components, make sure to provide the "name" option. 原因&#xff1a;引用了插件&#xff0c;却没有在main.js里使…

手写200行代码实现精简版SpringMVC

文章目录1. Spring IOC、DI、MVC 的原理图2. 开始构建自己的SpringMVC2.1 文件配置2.2 先用后写2.3 注解2.4 DispatchServlet的init() 初始化2.5 doPost&#xff08;&#xff09;进行映射处理2.6 完整的 MyDispatchServlet 代码3. 运行效果4. 遗留问题1. Spring IOC、DI、MVC 的…

php课程---Windows.open()方法参数详解

Window.open()方法参数详解1, 最基本的弹出窗口代码 window.open(page.html);2, 经过设置后的弹出窗口 window.open(page.html, newwindow, height100, width400, top0, left0, toolbarno, menubarno, scrollbarsno, resizableno, locationno, statusno) //该句写成一行代…

Istio - DistributedTracing(服务追踪)

官方参考文档&#xff1a; https://istio.io/docs/tasks/telemetry/distributed-tracing/overview/ https://istio.io/docs/tasks/traffic-management/egress/ https://istio.io/docs/examples/advanced-gateways/egress-gateway/ https://istio.io/help/faq/distributed-t…

R scholar和rentrez | NCBI和Google scholar文献数据挖掘

主要会用到两个R包&#xff1a; rentrez: Entrez in Rscholar: Analyse Citation Data from Google Scholar RISmed 包可以查询 PubMed 数据库中的信息。 目的1&#xff1a;输入检索词&#xff0c;从GSE数据库中爬取附带的参考文献。转载于:https://www.cnblogs.com/leezx/p/10…

Java, 对Excel解析的封装

文章目录最终的调用方法源代码Excel对应的实体类实体类要实现的接口核心封装代码依赖的jar包完整Demo项目最终的调用方法 List<ExcelPOJO> result new ArrayList<>(); ResolveExcel<ExcelPOJO> resolveExcel new ResolveExcel<ExcelPOJO>(file, Exc…

[转]使用JDBC进行批处理

http://mousepc.iteye.com/blog/1131462 •业务场景&#xff1a;当需要向数据库发送一批SQL语句执行时&#xff0c;应避免向数据库一条条的发送执行&#xff0c;而应采用JDBC的批处理机制&#xff0c;以提升执行效率。 一、•实现批处理有两种方式&#xff0c;第一种方式&#…

基于Istio的服务版本路由改造

在切换Istio过程中&#xff0c;鉴于Istio强大的路由功能&#xff0c;希望对服务架构中的路由服务进行改造&#xff08;替换&#xff09;&#xff0c;使用Istio中的相关路由功能来代替传统路由服务&#xff0c;故进行如下探索...... 原服务架构如下图&#xff1a; 服务请求步骤…