4

我正在尝试使用 locust 登录我的 Web 应用程序。我刚开始使用蝗虫。

我正在使用以下代码登录应用程序。

post_data = {'username': username, 'password': password,'Submit':'Login' }
with self.client.post('/mylogin-url/', post_data,
                                  catch_response=True) as response:
     print response.code
     print response.content

这部分总是返回状态码200但登录不成功,因为登录后source的响应内容与实际的不一样

我的 Web 应用程序创建 cookie 并在登录后重定向到基于 cookie 的 URL。我试图了解登录操作是从 locust 自动创建此 cookie,还是我需要在脚本本身中添加 cookie 创建逻辑的那部分。

非常感谢您对此的任何帮助。

谢谢

4

2 回答 2

1

你可能需要看看这个

在您的with块中,您可以解析响应并检查您是否得到正确的响应。

假设如果您登录成功,您应该在响应 cookie 中获取 cookie 字段,那么您可以这样做:

post_data = {'username': username, 'password': password,'Submit':'Login' }
with self.client.post('/mylogin-url/', post_data, \
    catch_response=True) as response:
    if 'cookie' not in respone.cookies:
        response.failure('login failed')
于 2017-08-01T06:29:16.620 回答
0

似乎 html 内容中的响应告诉用户尚未登录。在这种情况下,您可以检查响应文本是否包含失败消息

post_data = {'username': username, 'password': password,'Submit':'Login' }
with self.client.post('/mylogin-url/', post_data,
                                  catch_response=True) as response:
     if response.text.contains("Login failed"):
       response.failure('login failed')
于 2017-10-11T12:48:39.750 回答