[英]Issue with getting the response data using Locust
我想看看我是否能够获得响应数据,因为我正在尝试学习如何在 Locust 上使用正则表达式。 我正在尝试使用 Locust 从 JMeter 重现我的测试脚本。 这是我遇到问题的代码部分。
import time,csv,json
from locust import HttpUser, task,between,tag
class ResponseGet(HttpUser):
response_data= ""
wait_time= between (1,1.5)
host= "https://portal.com"
username= "NA"
password= "NA"
@task
def portal(self):
print("Portal Task")
response = self.client.post('/login', json={'username':'user','password':'123'})
print(response)
self.response_data = json.loads(response.text)
print(response_data)
我已经尝试过这个建议,但不知何故无法让它发挥作用。
我的想法是获取响应数据 > 使用正则表达式提取字符串 > 传递字符串以供下一个任务使用
例如:获取登录响应数据 > 使用正则表达式提取令牌 > 将令牌用于下一个任务。
有没有更好的方法来做到这一点?
你这样做的方式应该可行,但 Locust 的 HttpUser 客户端基于请求,所以如果你想以 JSON 形式访问响应数据,你应该能够只使用self.response_data = response.json()
来做到这一点。 但这只有在响应正文是有效的 JSON 时才有效。 如果响应正文不是 JSON,您的代码也会失败。
如果您的问题是将响应文本解析为 JSON,则响应可能不是 JSON,可能是因为您收到错误或其他原因。 您可以在尝试将其加载为 JSON 之前打印响应正文。 但是您当前的print(response)
不会这样做,因为它只会打印 Requests 返回的Response
对象。 您需要改为print(response.text())
。
至于正则表达式是否是获取响应中返回的令牌的正确解决方案,这将取决于响应的确切格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.