前言
v1.4.2 版本支持 websocket 协议
python 操作websocket 协议
环境准备
pip3 install websocket-client
pip3 install websockets
基本代码示例
from websocket import create_connection
import json
url = 'ws://localhost:8081/ws'
ws = create_connection(url, timeout=10) # 创建链接
print(ws.getstatus()) # 状态码
ws.send('hello world') # 发送文本消息
print(ws.recv()) # 接收返回内容
# 发送json消息
body = {
"name": "yoyo",
"email": "[email protected]"
}
ws.send(json.dumps(body))
print(ws.getstatus())
print(ws.recv())
ws.shutdown() # 关闭连接
运行结果
101
接受到的消息是: hello world
101
接受到的消息是: {"name": "yoyo", "email": "[email protected]"}
yaml 中 webscoket 用例实现
ws关键字相当于create_connection(url, timeout=10)
创建连接
send 关键字相当于 ws.send('hello')
发送文本消息`
test_x1:
ws:
url: ws://localhost:8081/ws
timeout: 10
send: hello
validate:
- eq: [status, 101]
- eq: [text, '接受到的消息是: hello']
运行结果
collected 1 item
a_ws/test_ws.yml::test_x1
---------------------------------------------------- live log call -----------------------------------------------------
2023-07-17 19:44:52 [INFO]: 执行文件-> test_ws.yml
2023-07-17 19:44:52 [INFO]: base_url-> http://124.70.221.221:8201
2023-07-17 19:44:52 [INFO]: config variables-> {}
2023-07-17 19:44:52 [INFO]: 运行用例-> test_x1
2023-07-17 19:44:53 [INFO]: 创建 websocket 链接: ws://localhost:8081/ws
2023-07-17 19:44:53 [INFO]: websocket send: hello
2023-07-17 19:44:53 [INFO]: websocket recv: 接受到的消息是: hello
2023-07-17 19:44:53 [INFO]: validate 校验内容-> [{'eq': ['status', 101]}, {'eq': ['text', '接受到的消息是: hello']}]
2023-07-17 19:44:53 [INFO]: validate 校验结果-> eq: [101, 101]
2023-07-17 19:44:53 [INFO]: validate 校验结果-> eq: [接受到的消息是: hello, 接受到的消息是: hello]
PASSED
websocket 一次连接可以发送多个请求,所以可以在一个用例中有多个send请求
test_x2:
-
ws:
url: ws://localhost:8081/ws
timeout: 10
-
send: hello
validate:
- eq: [status, 101]
- eq: [text, '接受到的消息是: hello']
-
send:
name: yoyo
email: [email protected]
validate:
- eq: [status, 101]
- eq: [text, '接受到的消息是: {"name": "yoyo", "email": "[email protected]"}']
多个连接地址测试
一个yaml 文件中可以有多个连接
test_x1:
-
ws:
url: ws://localhost:8081/ws
timeout: 10
-
send: hello
validate:
- eq: [status, 101]
- eq: [text, '接受到的消息是: hello']
- eq: [body, '接受到的消息是: hello']
test_x2:
ws:
url: ws://localhost:8081/json
timeout: 10
send:
name: xx
email: [email protected]
validate:
- eq: [status, 101]
- eq: [$.name, xx]
多个url地址公共环境地址 base_url 也可以写的config中
config:
base_url: ws://localhost:8081
test_x1:
-
ws:
url: /ws
timeout: 10
-
send: hello
validate:
- eq: [status, 101]
- eq: [text, '接受到的消息是: hello']
- eq: [body, '接受到的消息是: hello']
test_x2:
ws:
url: /json
timeout: 10
send:
name: xx
email: [email protected]
validate:
- eq: [status, 101]
- eq: [$.name, xx]
执行结果
collected 2 items
a_ws/test_ws1.yml::test_x1
-------------------------------live log call ---------------------------------------
2023-07-17 19:50:07 [INFO]: 执行文件-> test_ws1.yml
2023-07-17 19:50:07 [INFO]: base_url-> ws://localhost:8081
2023-07-17 19:50:07 [INFO]: config variables-> {}
2023-07-17 19:50:08 [INFO]: 创建 websocket 链接: ws://localhost:8081/ws
2023-07-17 19:50:08 [INFO]: websocket send: hello
2023-07-17 19:50:08 [INFO]: websocket recv: 接受到的消息是: hello
2023-07-17 19:50:08 [INFO]: validate 校验内容-> [{'eq': ['status', 101]}, {'eq': ['text', '接受到的消息是: hello']}, {'eq
': ['body', '接受到的消息是: hello']}]
2023-07-17 19:50:08 [INFO]: validate 校验结果-> eq: [101, 101]
2023-07-17 19:50:08 [INFO]: validate 校验结果-> eq: [接受到的消息是: hello, 接受到的消息是: hello]
2023-07-17 19:50:08 [INFO]: validate 校验结果-> eq: [接受到的消息是: hello, 接受到的消息是: hello]
PASSED [ 50%]
----------------- live log call -------------------------------------
2023-07-17 19:50:08 [INFO]: 执行文件-> test_ws1.yml
2023-07-17 19:50:08 [INFO]: base_url-> ws://localhost:8081
2023-07-17 19:50:08 [INFO]: config variables-> {}
2023-07-17 19:50:08 [INFO]: 运行用例-> test_x2
2023-07-17 19:50:09 [INFO]: 创建 websocket 链接: ws://localhost:8081/json
2023-07-17 19:50:09 [INFO]: websocket send: {'name': 'xx', 'email': '[email protected]'}
2023-07-17 19:50:09 [INFO]: websocket recv: {"name": "xx", "email": "[email protected]", "code": 0, "time": 1689594609.5537014}
2023-07-17 19:50:09 [INFO]: validate 校验内容-> [{'eq': ['status', 101]}, {'eq': ['$.name', 'xx']}]
2023-07-17 19:50:09 [INFO]: validate 校验结果-> eq: [101, 101]
2023-07-17 19:50:09 [INFO]: validate 校验结果-> eq: [xx, xx]
PASSED [100%]
================== 2 passed in 2.47s ============================
标签:INFO,websocket,07,17,19,52,yaml,ws,eq
From: https://www.cnblogs.com/yoyoketang/p/17561017.html