Overview
LoadForge allows you to login to any website, and then browse the site as a logged in
user (or set of users).
This example test posts to a login page when it starts, then requests /profile and some static
content normally. This is a great example for logging into a site and
then requesting content after login.
The on_start
definition will be run once by each individual simulated user. So they all login first, and then
execute your various tests.
Sessions are automatic
LoadForge will automatically keep sessions, meaning you can login and then
browse the site like a browser would.
Basic code example
import time
from locust import HttpUser, task, between
class QuickstartUser(HttpUser):
def on_start(self):
# Form post to /login with email and password
self.client.post("/login", {
'email': 'user@domain.com',
'password': 'passw0rd'
})
@task(2)
def profiles(self):
self.client.get("/profile")
@task(1)
def statics(self):
self.client.get("/img/logo.png")
self.client.get("/css/styles.css")
self.client.get("/js/scripts.js")
Advanced code example
This test starts with a login and a request to the index page of the site. It then finds all
anchor tags (a href) and randomly browses through them.
import random
from locust import HttpUser, TaskSet, task
from pyquery import PyQuery
class AwesomeUser(HttpUser):
def login(self):
self.client.post("/login", {
"username":"EXAMPLE_USER",
"password":"PASSWORD"
})
def index_page(self):
r = self.client.get("/")
pq = PyQuery(r.content)
link_elements = pq("a")
self.toc_urls = []
for l in link_elements:
if "href" in l.attrib:
self.toc_urls.append(l.attrib["href"])
def on_start(self):
self.login()
self.index_page()
@task
def load_page(self):
url = random.choice(self.toc_urls)
r = self.client.get(url)
Using basic authentication
You can easily login using basic auth with any LoadForge request by adding the auth parameter to your
get or post request object. In this example below, we will try to login as user1 with password password1.
We have two examples below, one POST request with data and another GET request with just authentication.
import time
from locust import HttpUser, task, between
class QuickstartUser(HttpUser):
wait_time = between(3, 5)
@task
def login_samples(self):
self.client.get("/myUrl", auth=("user1", "password1"))
self.client.post("/myUrl", data={"item": "123"}, auth=("user1", "password1"))
In addition, you can also use the on_start definition to globally set them:
from locust import HttpUser, task, between
import base64
class WebsiteUser(HttpUser):
wait_time = between(1, 2)
def on_start(self):
username = "user1"
password = "password1"
credentials = f"{username}:{password}"
encoded_credentials = base64.b64encode(credentials.encode("utf-8")).decode("utf-8")
self.client.headers.update({
"Authorization": f"Basic {encoded_credentials}"
})
@task
def index(self):
self.client.get("/")
@task
def dashboard(self):
self.client.get("/dashboard")
CSRF tokens
We have a full guide available on handling login pages that require CSRF. Please continue
to Handling CSRF.
Locust Test Example
LoadForge is powered by locust, meaning open source locust users can copy this script as well.
If you are a locust user, consider importing your script to LoadForge to supercharge your testing!