dongtang1997 2017-11-15 15:19
浏览 61
已采纳

在Laravel应用程序上进行Locust登录测试

Trying to login to laravel application using Locust. My code is following:

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

However I got error - Code 419 Content The page has expired due to inactivity. Could anyone advise me how to login to Laravel application using Locust

  • 写回答

1条回答 默认 最新

  • doujindou4356 2017-11-16 05:55
    关注

    Found the answer:

    Reason is that I'm passing wrong value for _token field. Instead, if you face same task, you could try following:

    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
    

    Hope that will help someone in future :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化