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(l):
l.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",
{"item": "123", "button": "submit"},
auth=("user1", "password1")
)
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!