Simple Interface Recording Tool#
A simple interface recording tool was completed in one day, with the main functions including:
- Recording API requests to a database, which can be either sqlite or postgresql.
- When manually recording APIs, the browser proxy is manually set, and it can be completed with one click using the command line or UI.
The recorded API requests are mainly used to facilitate the generation of API test code, which can be used by both testing and development. Select the desired API and save it to the database for easy future use.
The advantage of this tool is that it has a very low cost. The most necessary functions were implemented in just one day.
Command Line Tool Features#
- Start mitmproxy and custom plugin to record API requests
poetry run qacli capture start --name="scenario_name"
- The websites whose requests are recorded can be configured in: configs/settings.toml
mitm = { recorded_url = "https://www.baidu.com,https://www.bing.com" }
The above example indicates that requests to all baidu and bing addresses will be captured and saved to the database.
- Database table structure:
class ApiMonitorRecord(SQLModel, table=True):
__tablename__ = "api_monitor_record"
id: Optional[int] = Field(default=None, primary_key=True)
app: Optional[str] = None
service: Optional[str] = None
api: Optional[str] = None
path: Optional[str] = None
request_url: Optional[str] = None
method: Optional[str] = None
request_headers: Optional[str] = None
request_body: Optional[str] = None
response_headers: Optional[str] = None
status_code: int
response_body: Optional[str] = None
scenario_name: Optional[str] = None
- Turn on/off proxy for MAC: No need for browser operation, it can be done through the command line
poetry run qacli mac-proxy --help
Usage: qacli mac-proxy [OPTIONS] COMMAND [ARGS]...
╭─ Options ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --help Show this message and exit. │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Commands ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ off disable api capture proxy │
│ on enable api capture proxy
UI Interface Features#
Run UI:
poetry run qaui
UI Function Operations:
- Enter the name of the recording scenario: Any name is acceptable.
- Perform operations on relevant pages, and API interfaces will be recorded.
- Query the database, and all API requests with this scenario name will be recorded.
select * from api_monitor_record where scenario_name=<your_record_name>
- The saved data can be batch converted into the automation testing or data preparation tools you need.
The tool is very rudimentary but simple enough, with very little code. It can record and, combined with other tools, can basically meet the requirements for quickly building daily API access code generation and data preparation.
Python Dependencies Used#
The main ones include:
- typer: command line building tool
- gradio: UI building tool
- Database operations: previously existing access tool based on sqlmodel
- poetry: Python project management, packaging/dependency management