Skip to content

Instantly share code, notes, and snippets.

@algotrader-dotcom
Forked from acrollet/locustfile.py
Created August 9, 2018 03:10
Show Gist options
  • Save algotrader-dotcom/15259b8273e2f264e9c94683a09bde82 to your computer and use it in GitHub Desktop.
Save algotrader-dotcom/15259b8273e2f264e9c94683a09bde82 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")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment