Testing form submissions is a crucial part of load testing. Many applications rely on forms for authentication, data collection, and business logic (e.g., logins, signups, contact forms).

There are multiple ways to submit form data, including traditional web forms and JSON-based API submissions. Below are examples of both methods.

Tip: Use LoadForge’s Browser Recorder to automatically capture and replay form submissions. This simplifies setting up real-world interactions.

Submitting Standard Web Forms

A standard HTML form submission involves sending form data via a POST request. Below is an example of a contact form submission in LoadForge:

Example: Contact Form Submission

from locust import HttpUser, task, between

class QuickstartUser(HttpUser):
    wait_time = between(3, 5)

    @task(2)
    def contact(self):
        self.client.post("/contact", {
            'email': 'user@domain.com',
            'subject': 'LoadForge Test',
            'message': 'This is a test form submission from LoadForge.'
        })

Handling Redirects

Many form submissions trigger redirects (e.g., login forms). If your test needs to follow a redirect, add allow_redirects=True:

self.client.post("/login", {"username": "test", "password": "secure"}, allow_redirects=True)

For more examples, see:

Submitting JSON Data

Modern applications often use APIs to handle form data as JSON payloads. Below is an example of submitting JSON data in LoadForge:

Example: JSON POST Request

import json
from locust import HttpUser, task, between

class QuickstartUser(HttpUser):
    wait_time = between(3, 5)

    @task(1)
    def post_json(self):
        payload = {
            'username': 'test_user',
            'password': 'secure_password'
        }
        headers = {'Content-Type': 'application/json'}
        self.client.post("/api/login", data=json.dumps(payload), headers=headers)

Common API Issues

If your JSON form submissions fail, check for:

  1. Incorrect Content-Type – Ensure Content-Type: application/json is set.
  2. Missing Headers – Some APIs require authentication tokens.
  3. CSRF Protection – Some web apps reject requests without a CSRF token.

For more details, see:

Debugging Form Submissions

If your test isn’t working, try:

  • Manually submitting the form in your browser and checking network requests in Developer Tools > Network Tab.
  • Using LoadForge’s Debugger to analyze failed requests.
  • Enabling logging to capture detailed request responses:
    response = self.client.post("/contact", data={...})
    print(response.status_code, response.text)
    

By mastering form submissions, you can simulate real user behavior, ensuring your application performs well under load. 🚀