> ## Documentation Index
> Fetch the complete documentation index at: https://docs.loadforge.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Login Examples

> Test that logs in and then requests a profile page.

## 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.

<Note title="Sessions are automatic">
  LoadForge will automatically keep sessions, meaning you can login and then
  browse the site like a browser would.
</Note>

## Basic code example

```python theme={null}
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.

```python theme={null}
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. This example demonstrates logging in as user1 with password password1.

This guide includes two examples: a POST request with data and a GET request with authentication.

```python theme={null}
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:

```python theme={null}
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

For login pages that require CSRF protection, see the guide on [Handling CSRF](/docs/tests-csrf).

***

<Note title="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](https://loadforge.com) to supercharge your testing!
</Note>
