Laravel 应用上的 Locust 登录测试

Locust login testing on Laravel app

正在尝试使用 Locust 登录 laravel 应用程序。 我的代码如下:

from locust import HttpLocust, TaskSet, task

class UserBehavior(TaskSet):
  def on_start(self):
    self.login()

  def login(self):
    response = self.client.get("/en/login")
    csrftoken = response.cookies['XSRF-TOKEN']
    session = response.cookies['bitrent_session']
    post_data = {'username': "user@user.com", 'password': '1234', "_token":     csrftoken }
    with self.client.post('/en/login', post_data,
                    catch_response=True) as response:
      print("Code: ", response.status_code)
      print("Content: ", response.content

  @task()
  def index(self):
    self.client.get("/")

class WebsiteUser(HttpLocust):
  task_set = UserBehavior
  min_wait = 5000
  max_wait = 9000

但是我收到错误 - 代码 419 内容该页面因不活动而已过期。 谁能告诉我如何使用 Locust

登录 Laravel 应用程序

找到答案:

原因是我为 _token 字段传递了错误的值。 相反,如果您面临同样的任务,您可以尝试以下操作:

import re
from locust import HttpLocust, TaskSet, task

class UserBehavior(TaskSet):
  def on_start(self):
    self.login()

  def login(self):
    response = self.client.get("/en/login")
    print("Content: ", response.content)
    csrftoken = re.search('meta name="csrf-token" content="(.+?)"', response.text).group(1)

    print("token: ", csrftoken)
    post_data = {'username': "user@user.com", 'password': '1234', "_token": csrftoken }
    with self.client.post('/en/login', post_data,
                catch_response=True) as response:
      print("Code: ", response.status_code)
      print("Content: ", response.content)


  @task()
  def index(self):
    self.client.get("/")

class WebsiteUser(HttpLocust):
  task_set = UserBehavior
  min_wait = 5000
  max_wait = 9000

希望对以后的人有所帮助:)