当前位置:网站首页>Interface test advanced interface script using -apipost (pre/post execution script)
Interface test advanced interface script using -apipost (pre/post execution script)
2022-08-10 18:19:00 【InfoQ】
The action time of the pre-execution script
The pre-execution script is a script that is executed before the request is sent.
The role of pre-execution scripts
Pre-execution scripts can accomplish the following functions:
- Write JS functions to realize complex calculations;
- Printing of variables
- Define, get, delete, clear environment variables
- Define, get, delete, clear global variables
- Get request parameters
- dynamically add or delete a header request parameter
- dynamically add or delete a query request parameter
- Dynamicly add or delete a body request parameter
- Send HTTPRequest
Write JS functions to implement complex calculations
We can define a function _random in the pre-executed script,
function _random(){
return 'Hello, China' + Math.random();
}
It returns a string: "Hello China" + random number, which can be passed
apt.globals.set("random_var", _random());
Assign it to the global variable random_var.
Pre-execution script prints debugging variables
We can print the required variables to the console through console.log() to view the current value of a variable.As shown in the example above
Define, get, delete, clear environment variables
apt.variables.set("key", "value"); //Set an environment variable key
apt.variables.get("key"); // Get the value of the environment variable key
apt.variables.delete("key"); //Delete the environment variable key
apt.variables.clear(); // Clear all defined environment variables
Define, get, delete, clear global variables
apt.globals.set("key", "value"); //SetA global variable key
apt.globals.get("key"); //Set a global variable key
apt.globals.delete("key"); //Set a global variable key with value value
apt.globals.clear(); //Clear all defined global variables
Get request parameters
PassThe request object obtains request parameters. For details, please refer to the document "APIPOST Built-in Variables".
Dynamicly add or delete a header request parameter
apt.setRequestHeader("key", "value"); // Dynamically add a header whose key is key and value is valueParameters
apt.removeRequestHeader("key"); //Remove the parameter whose key is key in the header parameter
dynamically add or delete a query request parameter
apt.setRequestQuery("key", "value"); // Dynamically add a query parameter whose key is key and value is value
apt.removeRequestQuery("key"); //Remove the key in the query parameter as keyThe parameters
dynamically add and delete a body request parameter
apt.setRequestBody("key", "value");// Dynamically add a key as the key valueThe body parameter is only valid for form-data and urlencode
apt.removeRequestBody("key");//Remove the parameter whose key is key in the body Only valid for form-data and urlencode
Sending HTTP Requests
We can use AJAX's $.ajax method to send an http request in a pre-executed script.The following is a simple example demo: use a pre-executed script to send a request to
https://echo.apipost.cn/get.php
, and assign the bigint of the response result to the global variable bigint.
$.ajax({
url:"https://echo.apipost.cn/get.php",
method:"POST",
headers:{
"content-type":"application/json"
},
timeout:"10000",
async:false, // remember thisIf the item is set to false, the request must be synchronized
data:JSON.stringify({"email":"[email protected]","password":"123456"}),
success:function (response) {
apt.globals.set("bigint",response.bigint);
}
}
Apipost download address:
Apipost-based on collaboration, not limited to API documentation, debugging, Mock
———————————————
Copyright statement: This article is an original article by CSDN blogger "Elegance...", following the CC 4.0 BY-SA copyright agreement, please attach the original source for reprintingLink and this statement.
Original link:
Interface test advanced interface script use-apipost (pre/post-execution script)_Elegance... Blog-CSDNBlog
边栏推荐
- 产品说明丨Android端使用MobPush快速集成方法
- pip install fatal error C1083 cannot open include file "io.h" No such file or directory
- unity 坑坑001
- 【HMS core】【FAQ】AR Engine、Analytics Kit、Video Editor Kit、Image Kit、Map Kit典型问题合集2
- 【接入指南 之 直接接入】手把手教你快速上手接入HONOR Connect平台(中)
- 三星Galaxy Watch5产品图片流出 非Pro表款亦有蓝宝石加持
- 测试接口出现“data“: “Full authentication is required to access this resource“凭证已过期
- Interpretation of ZLMediaKit server source code---RTSP push and pull
- 【FAQ】HarmonyOS ETS如何给组件设置边框
- R语言使用ggpubr包的ggbarplot函数可视化柱状图、设置add参数为mean_se和jitter可视化不同水平均值的柱状图并为柱状图添加误差线(se标准误差)和抖动数据点分布
猜你喜欢
随机推荐
「业务架构」业务能力的热图是什么,有啥用?
Interpretation of ZLMediaKit server source code---RTSP push and pull
Toronto Research Chemicals BTK甜味剂配方丨D-Abequose
【FAQ】【Push Kit】推送服务,回执配置一直报错、回执过期修改、怎么删除配置的回执
接口测试进阶接口脚本使用—apipost(预/后执行脚本)
海思HI3516DV300开发资料
【ARK UI】HarmonyOS ETS的引导页的实现
FlexSim仿真软件入门笔记:基本操作、快捷键
背景视频铺满盒子
AVFrame related api memory management
机器人控制器编程实践指导书旧版-实践七 无线通信(网络)
eager模式和graph模式 Tensorflow
【独立站运营】做社交媒体营销的两大关键点
【测试】黑盒测试用例设计方法
1001 A+B Format (string processing)
【快应用】实现自定义导航栏组件
同一块中出现两个 * 就不能正常显示
Xilinx FPGA收发器参考时钟设计应用
记录某博彩网站渗透
Active users of mobile banking grew rapidly in June, hitting a half-year high