1、mitmproxy抓包和python脚本结合使用,提升测试效率
1.1、使用场景:
- 测试线上真实答题活动,题目100条,要验证成功题目的场景。题目不是按照顺序出来,从答题文件中寻找正确答案费时。
1.2、处理方式
- 编写脚本,抓包获取接口的题目,然后将接口中题目字段全部取出,然后读取文件中的题目内容,进行匹配,打印出对应题目的答案
1.3、脚本:
from mitmproxy import http,ctx
import json
import csv
class Modify:
def __init__(self):
self.answer = self.findAnswerByCsv()
# GetQuestion.answer = self.findAnswerByCsv()
print(self.answer)
def findAnswerByCsv(self):
csv_file = csv.reader(open('../../resource/quest-1.csv','r',encoding="gbk"))
print(csv_file)
dict = {}
for row in csv_file:
dict[row[0]] = row[1]
return dict
def response(self, flow):
if flow.request.url.startswith('https://xxx.xx.xxx.xxx.cn/tool/question/list'):
question = []
response = json.loads(flow.response.get_text())
//data是题目的json数据,取出对应的name字段,去文件中获取的题目-答案(字典类型)获取对应的答案。
for i in response['data']:
print(self.answer[i['name']])