【问题标题】:Issue with getting the response data using Locust使用 Locust 获取响应数据的问题
【发布时间】:2021-06-25 05:04:55
【问题描述】:

我在尝试学习如何在 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)     

I've tried this 的建议,但我无法让它发挥作用。

我的想法是获取响应数据 > 使用正则表达式提取字符串 > 将字符串传递给下一个任务使用

例如: 获取登录响应数据 > 使用正则表达式提取令牌 > 将令牌用于下一个任务。

有没有更好的方法来做到这一点?

【问题讨论】:

    标签: python locust


    【解决方案1】:

    你这样做的方式应该可以工作,但是 Locust 的 HttpUser 的客户端是基于请求的,所以如果你想以 JSON 的形式访问响应数据,你应该能够只用self.response_data = response.json() 来做到这一点。但这只有在响应正文是有效的 JSON 时才有效。如果响应正文不是 JSON,您的代码也会失败。

    如果您的问题在于将响应文本解析为 JSON,则响应可能不是 JSON,可能是因为您遇到错误或其他原因。您可以在尝试将其加载为 JSON 之前打印响应正文。但是您当前的print(response) 不会这样做,因为它只会打印请求返回的Response 对象。您需要改为print(response.text())

    至于正则表达式是否是获取响应中返回的令牌的正确解决方案,这将取决于响应的格式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-12
      • 1970-01-01
      • 2019-02-07
      • 2019-07-16
      • 1970-01-01
      • 1970-01-01
      • 2019-04-20
      • 1970-01-01
      相关资源
      最近更新 更多