当前位置:网站首页>如何在自动化测试中使用MitmProxy获取数据返回?
如何在自动化测试中使用MitmProxy获取数据返回?
2022-07-16 19:08:00 【二 黑】
背景介绍
当我们在接口或UI自动化项目中,常常会出现这种现象——明明是正常请求,却无法获取到想要的数据返回。
比如:
场景A:页面是动态数据,第一次进入页面获取到的数据,和下次进入页面获取到的数据完全不一样。
场景B:页面数据有展示限制,页面某些模块或整个页面数据仅展示3次,且通过服务端返回控制。
这时我们要怎么办?
如果是手工测试时,我们大可以直接使用Charles、Fiddler进行模拟,毕竟代理软件基本能解决大部分测试需求。
那如果是在自动化项目中呢?如果还是使用代理软件——其实也不是完全不行,但我们还有更佳的实践方式。
也就是使用MitmProxy作为正向代理拦截请求和返回数据。
如何使用MitmProxy作为正向代理 实现Maplocal
MitmProxy是一个三方库,我们在程序中可以把它当成Charles来使用。
这里以实现Maplocal功能进行说明:
(1)Maplocal功能,简单理解就是当针对特定请求,返回特定数据。
比如请求"www.baidu.com",正常应该返回:
{
"post": {
"title": "百度一下",
},
"type": 1
}
但我们修改返回数据,只要有"www.baidu.com"请求过来,就让它自动返回:
{
"post": {
"title": "谷歌一下啦啦啦",
},
"type": 1
}
这就是一个最基础的maplocal案例。
(2)在MitmProxy里如何实现Maplocal功能呢?
这里以get请求实现进行说明:
get.json 配置文件,在这里记录所有需要拦截的get请求:
{
"https:www.baidu.com":[
{
"jsonPath":"../data/maplocal/first_tab.json"
}
]
}
MitmProxy脚本中:
from mitmproxy import http
from mitmproxy import ctx
class InterceptRequests:
def __init__(self):
def request(self, flow: http.HTTPFlow) -> None:
"""
使用request事件实现map local
:param flow:
:return:
"""
# GET 请求处理
if method == "GET":
pure_url = get_url_host(interceptUrl)
url_data = get_json.get(pure_url)
if url_data:
interce_params = get_url_params(interceptUrl)
params = get_json_value(get_path, pure_url)
for para in params:
if para.items() <= interce_params.items():
jsonpath = para.get("jsonPath")
jsonpath_abs = path(jsonpath)
self.file_maplocal(flow, jsonpath_abs)
addons = [
InterceptRequests()
]
在执行的脚本中(如本地shell 或 其他执行的py脚本里):
# kill mitmproxy
lsof -n -i:8999 | grep LISTEN | awk '{print $2}' | xargs kill
# start mitmproxy
mitmdump -p 8999 -s mitmHandler.py
启动脚本后,连接MitmProxy的设备的请求全部都会被脚本监控。
如果出现"www.baidu.com"的请求,就会到get.json里找到对应的json,自动返回给设备。
这样,就实现了一个最简单的maplocal。
当然基于目前的MitmProxy脚本,我们还可以进行一些功能扩展。
如针对接口类型get或post请求,针对请求参数类型json或x-www-form-urlencoded,都可以进行更多延伸扩展。
学习资源分享
最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走
这些资料,对于想自学【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!凡事要趁早,特别是技术行业,一定要提升技术功底。希望对大家有所帮助…….
加入下方我的交流群免费获取!
边栏推荐
- 【图文并茂】U盘启动盘制作 U盘启动盘重装系统教程
- 一人用低代码开发平台搭建整个集团的数字化系统解决方案
- Internet of things aquarium designed based on stm32+ Huawei cloud IOT [play with Huawei cloud]
- Simple and efficient code suggestions
- Installing MySQL database on Linux server (detailed tutorial)
- Layoffs are coming
- 机器学习笔记 - 使用 GAN 进行数据增强以进行缺陷检测
- 【面试:并发篇15:多线程:synchronized优化原理】
- The University of Leuven recruited postdoctoral researchers to use ai/ml to analyze images of solar activity areas and predict flares
- MySQL -- string function
猜你喜欢

淺學js中的關系運算符

机器学习笔记 - 使用 GAN 进行数据增强以进行缺陷检测

机器学习笔记 - 基于Keras的GAN:图像去模糊的应用

Mysql——字符串函数

What are the directions of voice signal processing in audio and video?

MySQL -- string function

Redis - detailed explanation of slot management commands

Transaction isolation level

阿普奇 ABOX-700 工控机 MinipiceCAN卡在电力巡检机器人中的应用

【C】 Dynamic memory management
随机推荐
[leetcode problem solving report] 423 Rebuild numbers from English
Word cloud graph, word frequency graph, specially statistics the word cloud word frequency of some keywords
将一个文件夹中的内容复制到另一个文件夹中
[C language brush leetcode] 1432 The maximum difference that can be obtained by changing an integer (m)
Synaesthesia integration architecture and key technologies
Other new features of MySQL MySQL 8
淺學js中的關系運算符
Idea creates new branches and merges code (others - > DEV)
【PHP代码审计】Pikachu靶场漏洞入门分析
Linux服务器上备份mysql数据库(详细教程)
博客从 CloudBase 迁移至云主机
阿普奇 E8 工控机——MinipiceCAN卡在建筑机器人的应用
Layoffs are coming
MySQL variables, process control and cursor exercises
Blog migration from cloudbase to virtual machine
ES6-新增的数组方法之最常用的几种 map(),filter(),reduce(),forEach(),
I rolled up a small and beautiful [markdown static blog program] at home
机器学习笔记 - 使用 GAN 进行数据增强以进行缺陷检测
LeetCode 第23天
基于Gensim计算文本相似度