Skip to content

Instantly share code, notes, and snippets.

@acrollet
Created April 6, 2017 21:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save acrollet/ebe54e7f4dc9146ebd9ed62155f19a21 to your computer and use it in GitHub Desktop.
Save acrollet/ebe54e7f4dc9146ebd9ed62155f19a21 to your computer and use it in GitHub Desktop.
Log in to Drupal 8 with locust.io
from locust import HttpLocust, TaskSet, task
from bs4 import BeautifulSoup
class WebsiteTasks(TaskSet):
def on_start(self):
"""
on_start is called when a Locust start before,
any task is scheduled
"""
self.login()
def login(self):
# Get form build ID to pass back to Drupal on login.
resp = self.client.get("/user")
parsed_html = BeautifulSoup(resp.content)
form_build_id = parsed_html.body.find('input', {'name': 'form_build_id'})['value']
self.client.post("/user/login", {
"name": "LoadTester",
"pass": "test",
"form_id": "user_login_form",
"form_build_id": form_build_id,
"op": "Log in"
})
# Add some pages to load.
@task
def index(self):
self.client.get("/")
@task
def search(self):
self.client.get("/another/page")
@frazras
Copy link

frazras commented Sep 13, 2023

I needed to add this at the bottom for it to work

class WebsiteUser(HttpUser):
    tasks = [WebsiteTasks]
    wait_time = between(5, 10)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment