← Load Test Directory

Advanced E-Commerce Test Example

Authentication, Adding to cart, browsing products and going to check out

You are now browsing the LoadForge locust test directory. You can use these tests as a starting point for your own tests, or use our AI wizard to generate one automatically.

World

Overview

This enhanced LoadForge locust script simulates a more comprehensive user journey on an e-commerce website. It includes advanced behaviors such as user authentication, browsing through a variety of products, adding items to the cart, viewing the cart, and proceeding to checkout. The script is designed to mimic realistic user actions and gather detailed performance metrics.

Code

The below code can be run directly on LoadForge to test at scale.

from locust import HttpUser, task, between
import random

class EcommerceUser(HttpUser):
    wait_time = between(1, 5)
    product_ids = [101, 102, 103, 104, 105]  # Example product IDs
    cart_id = random.randint(1000, 9999)

    def on_start(self):
        self.user_login()

    def user_login(self):
        # Implement login logic here
        response = self.client.post("/login", {"username": "user", "password": "password"})
        if response.status_code != 200:
            print("Failed to log in")

    @task(1)
    def view_products(self):
        self.client.get("/products")

    @task(2)
    def view_product_details(self):
        product_id = random.choice(self.product_ids)
        self.client.get(f"/products/{product_id}", name="/products/:id")

    @task(3)
    def add_to_cart(self):
        product_id = random.choice(self.product_ids)
        self.client.post("/cart", {"product_id": product_id, "quantity": 1})

    @task(4)
    def view_cart(self):
        self.client.get(f"/cart/{self.cart_id}")

    @task(5)
    def checkout(self):
        self.client.post("/checkout", {"cart_id": self.cart_id})

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!

Ready to run your test?
Launch your locust test at scale.