上两篇文章我们介绍了swagger-ui文档转换jmx脚本&yapi文档转换jmx脚本给jmeter使用的方式,已经可以覆盖大多数公司的接口文档转换,可以帮助我们的测试攻城狮解放双手,节省很多时间!
但是还有一个问题,就是有的公司的接口文档是需要登录权限的,我们进行脚本转换时,因为没有登录,导致接口原始数据下载失败,我们的转换工具也无能为力,必须人手动登录成功后下载原始swaggerjson接口数据!
一键自动生成接口测试脚本,解放双手,降低纯手力劳动占比,进而给自己提供更多的时间去思考、理解产品和设计更多“聪明”的用例
今天我们以yapi接口文档为例,写代码自动登录,让我们的脚本去下载接口原始数据然后进行jmx转换!
https://apidock.test.com/api/plugin/exportSwagger?type=OpenAPIV2&pid=29&status=all&isWiki=false
def yapi_cookies():
url = "https://apidock.test.com/api/user/login_by_ldap"
payload = "{\"email\":\"test1\",\"password\":******\"\"}"
headers = {
'Content-Type': 'application/json; charset=utf-8',
'Connection': 'keep-alive'
}
response = requests.request("POST", url, headers=headers, data=payload)
log.info(response.headers['Set-Cookie'].split(';')[0])
return response.headers['Set-Cookie'].split(';')[0]
def get_yapi_json(pid, path):
url = "https://apidock.test.com/api/plugin/exportSwagger?type=OpenAPIV2&pid={}&status=all&isWiki=false".format(
pid)
headers = {
'Content-Type': 'application/json; charset=utf-8',
'Connection': 'keep-alive',
'cookie': '{}; _yapi_uid=3480'.format(yapi_cookies())
}
log.info(headers)
response = requests.request("GET", url, headers=headers)
data = json.loads(response.text)
title = data.get("info")["title"]
log.info(title)
with open("{}/data.json".format(path), 'w', encoding='utf-8') as fp:
fp.write(json.dumps(data, indent=4, ensure_ascii=False))
return data
pip install -U swaggerjmx
# -*- coding: utf-8 -*-
from swaggerjmx.convert import conversion
from swaggerjmx.settings import Settings as ST
# swagger_url_json_path
# 从yapi获取的json字符串
ST.swagger_json = get_yapi_json(pid, path)
# report_path
ST.report_path = 'jmx'
# 开始转换
conversion()
以上便是 swaggerjmx 的基本用法介绍。
如果您有发现错误,或者您对 swaggerjmx 有任何建议,欢迎到 swaggerjmx Issues 发表,非常感谢您的支持。您的反馈和建议非常宝贵,希望您的参与能帮助 swaggerjmx 做得更好。
关注「测试开发囤货」公众号回复「编程」,送你一本全彩 Python 编程电子书。