Skip to content

Commit

Permalink
feat: update cli tools
Browse files Browse the repository at this point in the history
  • Loading branch information
Pactortester committed Jun 15, 2022
1 parent d19cf77 commit d93e38d
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 22 deletions.
28 changes: 24 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pip install -U swaggerjmx


- github:https://github.com/Pactortester/swaggerjmx.git
- pypi:https://pypi.org/project/swaggerjmx/#history
- pypi:https://pypi.org/project/swaggerjmx


## 社区地址
Expand All @@ -40,8 +40,8 @@ pip install -U swaggerjmx
1. 将swagger-ui文档转换为jmx文件。
2. 将yapi文档转换为jmx文件。


## Demo_1
## 代码方式
### Demo_1
- 可以直接访问 swagger_url (http://ip:port/v2/api-doc) 不需要登录的,使用Demo_1方式转换

```python
Expand All @@ -59,7 +59,7 @@ conversion()
```


## Demo_2
### Demo_2
- 需要登录才能访问的,可以复制swagger_url页面上的json信息,保存json文件,使用Demo_2方式转换

```python
Expand All @@ -74,6 +74,26 @@ ST.report_path = 'jmx'
# 开始转换
conversion()

```

## 命令行方式
### 预置参数
```shell
(venv) lijiawei@bogon swaggerjmx % swaggerjmx -h
usage: swaggerjmx [-h] -i INPUT [-o OUTPUT]

Swagger or YApi convert jmx tool! Created: Lijiawei. Version 1.1.0

optional arguments:
-h, --help show this help message and exit
-i INPUT, --input INPUT
The input swagger json file or swagger url.
-o OUTPUT, --output OUTPUT
The output jmx file path(default jmx). If it exists, new endpoints will be overwrite.
```
### 使用方式
```shell
swaggerjmx -i <path_to_swagger_json_or_swagger_url> -o <path_to_output_jmx>
```
## 截图:

Expand Down
5 changes: 1 addition & 4 deletions swaggerjmx/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ def main():
"""
parser = argparse.ArgumentParser(description=__description__)

parser.add_argument(
"-v", "--version", dest="version", action="store_true", help="show version"
)
parser.add_argument(
'-i', '--input', help='The input swagger json file or swagger url.', required=True)
parser.add_argument(
'-o', '--output', help='The output jmx file path(default jmx). If it exists, new endpoints will be overwrite.',
'-o', '--output', help='The output jmx file path(default jmx). If it exists, old file will be overwrite.',
default="jmx")

args = parser.parse_args()
Expand Down
7 changes: 4 additions & 3 deletions swaggerjmx/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from swaggerjmx.get_swagger import get_test_plan
from swaggerjmx.to_jmx import *
from swaggerjmx.settings import Settings as ST
from loguru import logger


def conversion():
Expand All @@ -31,7 +32,7 @@ def conversion():
tree.write('{}/jmeter-'.format(ST.report_path) + re.sub(replace_str, "-", file_name) + '.jmx', pretty_print=True,
xml_declaration=True,
encoding='utf-8')
print('swagger convert jmx is success!')
logger.info('swagger convert jmx is success!')
except FileNotFoundError as e:
print(e)
print('swagger convert jmx is fail!')
logger.error(e)
logger.error('swagger convert jmx is fail!')
15 changes: 14 additions & 1 deletion swaggerjmx/get_swagger.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# -*- coding: utf-8 -*-
import os
import sys
from json import JSONDecodeError

import requests
import json
from loguru import logger


def get_test_plan(swagger_url=None, swagger_url_json_path=None, swagger_json=None):
Expand All @@ -21,9 +25,18 @@ def get_test_plan(swagger_url=None, swagger_url_json_path=None, swagger_json=Non

elif swagger_url is not None:
response = requests.get(swagger_url)
data = json.loads(response.text, strict=False)
try:
data = json.loads(response.text, strict=False)
except JSONDecodeError as e:
logger.error(f"The response value of {swagger_url} is not in json format")
logger.warning("Please check if you have access rights?")
logger.warning("Maybe you should use swagger json file to convert!")
sys.exit(0)

elif swagger_url_json_path is not None:
if not os.path.exists(swagger_url_json_path):
logger.error(f"FileNotFoundError: [Errno 2] No such file or directory: '{swagger_url_json_path}'")
sys.exit(0)
try:
with open(swagger_url_json_path, 'r', encoding='utf-8') as f:
data = json.load(f, strict=False)
Expand Down
9 changes: 0 additions & 9 deletions swaggerjmx/to_jmx.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,6 @@ def arguments(parent_xml):
Arguments.set("enabled", "true")
collectionProp = etree.SubElement(Arguments, "collectionProp")
collectionProp.set("name", "Arguments.arguments")
# elementProp = etree.SubElement(Arguments, "elementProp") common_api(elementProp, {"name":
# "ThreadGroup.main_controller", "elementType": "LoopController", "guiclass": "LoopControlPanel", "testclass":
# "LoopController", "testname": "Loop Controller", "enabled": "true"})

# collectionProp.set("name", "Argument.name")
# collectionProp.text = '${baseUrl}'
# collectionProp = etree.SubElement(elementProp, "stringProp")
# collectionProp.set("name", "Argument.value")
# collectionProp.text = '1'
return etree.SubElement(parent_xml, "hashTree")


Expand Down
2 changes: 1 addition & 1 deletion tests/test_swaggerjmx_json_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from swaggerjmx.convert import conversion
from swaggerjmx.settings import Settings as ST

# swagger or yapi json
# swagger or yapi json path
ST.swagger_url_json_path = './data/swagger.json'
# report_path
ST.report_path = 'jmx'
Expand Down

0 comments on commit d93e38d

Please sign in to comment.