当前位置:网站首页>如何在自动化测试中使用MitmProxy获取数据返回?
如何在自动化测试中使用MitmProxy获取数据返回?
2022-07-17 01:59: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,都可以进行更多延伸扩展。
最后感谢每一个认真阅读我文章的人,下面这个网盘链接也是我费了几天时间整理的非常全面的,希望也能帮助到有需要的你!

这些资料,对于想转行做【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!凡事要趁早,特别是技术行业,一定要提升技术功底。希望对大家有所帮助……
如果你不想一个人野蛮生长,找不到系统的资料,问题得不到帮助,坚持几天便放弃的感受的话,可以点击下方小卡片加入我们群,大家可以一起讨论交流,里面会有各种软件测试资料和技术交流。
| 点击文末小卡片领取 |
敲字不易,如果此文章对你有帮助的话,点个赞收个藏来个关注,给作者一个鼓励。也方便你下次能够快速查找。
自学推荐B站视频:
零基础转行软件测试:自学完软件测试,拿到了字节的测试岗offer,堪称B站最好的视频!
边栏推荐
- 谷歌 Chrome 浏览器安装 PWA 应用将显示更多描述信息
- Oracle queries the host name and the corresponding IP address
- AI 之 OpenCvSharp 大图找小图(案例版)
- Underline shortcut
- Gdb+vscode for debugging 8 - use core to analyze dead cycles, deadlocks, and segment errors
- No, check it out
- Derivation of PCA principal component analysis (dimension reduction) process
- Dive Into Deep Learning——2.2数据预处理
- 上班摸鱼打卡模拟器微信小程序源码
- 論文閱讀:U-Net++: Redesigning Skip Connections to Exploit Multiscale Features in Image Segmentation
猜你喜欢

爬虫学习(5):手把手教你爬虫requests实战演练

一种鲁棒变形卷积神经网络图像去噪

Chengxin University envi_ IDL first week experiment test: simple operation of array + detailed analysis

Machine learning library scikit learn (linear model, ridge regression, insert a column of data, extract the required column, vector machine (SVM), clustering)

Thinkphp5.0模型操作使用page进行分页
![Leetcode: dynamic programming [basic problem solving]](/img/fc/0ff622576a47868f8ecf70116ea2f2.png)
Leetcode: dynamic programming [basic problem solving]

Number of supported question banks and examination question banks of swiftui examination question bank project (tutorial includes source code)

The third day of the three questions of Luogu daily (make up on the fourth day)

SparkCore核心设计:RDD,220716,

367. Effective complete square (necessary for entry)
随机推荐
机器学习库Scikit-Learn(线性模型、岭回归、插入一列数据(insert)、提取所需列、向量机(SVM)、聚类)
Game theory of catching lice
Labelme starts normally, but cannot be opened
基于Matlab的男女声音信号分析与处理
Through openharmony compatibility evaluation, the big brother development board and rich teaching and training resources have been ready
Envi: (the most detailed tutorial in 2022) custom coordinate system
Chengxin University envi_ IDL second week class content: open hdf4 file and read the file, as well as simple data processing and saving + detailed analysis
Le cinquième jour de trois questions par jour à luogu
The third day of the three questions of Luogu daily (make up on the fourth day)
367. 有效的完全平方数(入门必会)
How to paste data in Excel into CXGRID
Leetcode: 0-1 knapsack problem in dynamic programming [come and set the template directly]
[2016 CCPC Hangzhou j] just a math problem (Mobius inversion)
MySQL addition, deletion, query and modification (basic)
[2016 CCPC 杭州J] Just a Math Problem (莫比乌斯反演)
10. Redis interview FAQ
Unity解决同材质物体重叠产生Z-Fighting的问题
洛谷每日三题之第四天
The fifth day of the third question of Luogu daily
Method of realizing horizontal and vertical centering of unknown width and height elements