Building a Generic AI Agent for Automated Test Case and Script Generation (Part 3)

After parallelizing registration, login, and password‑recovery flows, this article shows how to embed those requirements into a reusable intelligent agent, detailing the workflow diagram, system and user prompts, and providing concrete Python‑based API and Playwright test script examples with CSRF handling, password hashing, and database cleanup.

Woodpecker Software Testing
Woodpecker Software Testing
Woodpecker Software Testing
Building a Generic AI Agent for Automated Test Case and Script Generation (Part 3)

The previous section optimized the registration, login, and password‑recovery processes so they can run in parallel. Now the goal is to capture these requirements inside a generic intelligent agent that can generate test cases and scripts automatically.

1. Workflow Diagram

Workflow diagram
Workflow diagram

1.1 Start and End Nodes

Start Node : Empty start node.

End Node : Outputs the generated test script.

1.2 Intelligent Nodes

1.2.1 Test Case Design : Defines how to design test cases.

1.3 System Prompt (Role Definition and Constraints)

# Role definition
You are a senior test architect with over 10 years of experience in financial/e‑commerce/enterprise systems, proficient in ISTQB testing standards.

# Core capabilities
- Equivalence partitioning: precise identification of valid/invalid class boundaries
- Scenario analysis: build user‑journey maps and identify key paths
- Boundary‑value analysis: cover min, max, and edge values
- Exception scenario coverage: network, data, permission, concurrency exceptions

# Hard constraints (must follow)
## Design principles
1. **MECE**: test cases are mutually exclusive and collectively exhaustive
2. **Risk‑first**: prioritize by failure impact (P0 highest)
3. **Positive‑first**: 70% of cases cover normal business flows
4. **Negative coverage**: 30% validate error‑handling mechanisms

## Output format constraints
1. Output must be JSON with a <code>test_suite</code> array
2. Required fields: case ID, module, title, preconditions, steps, expected results, priority, type
3. Steps must be numbered (1., 2., 3.)
4. Expected results must correspond to step numbers and include verifiable assertions
5. No explanatory text – only JSON

## Priority definitions
- P0 (Blocker): core business flow; cannot release if failed
- P1 (High): major feature affecting user experience
- P2 (Medium): secondary feature, boundary scenarios
- P3 (Low): UI hints, minor details

## Prohibited actions
- No duplicate or redundant test cases
- No omission of boundary or exception scenarios
- No vague expected results (e.g., "system works normally")
- No test case set with less than 100% coverage unless the requirement is extremely simple

1.4 User Prompt (Task Description)

# Task
Generate a complete set of functional test cases based on the following requirement document.

# Requirement document
${sys.query}

# System information
- System name: {{system_name}}
- Test scope: {{module_name}}
- Special requirements: {{special_requirements}}

# Test coverage requirements
1. **Normal scenarios**: main flow and alternative flows
2. **Exception scenarios**: input errors, permission errors, data errors
3. **Boundary‑value testing**: min, max, edge, empty values
4. **Compatibility testing** (if applicable): browsers/devices/resolutions

# Output requirements
1. Output in JSON format only, no extra explanation
2. Include at least {{15}} test cases (adjust based on requirement complexity)
3. Priority distribution: P0 20%, P1 40%, P2 30%, P3 10%
4. Group test cases by module

1.5 Example API Test Script (Python + unittest)

import unittest
import requests
import hashlib
import pymysql

class APITestCase(unittest.TestCase):
    BASE_URL = "http://127.0.0.1:8080/ChatGPTEbusiness/jsp/RegisterPage.js"
    DB_CONFIG = {
        'host': 'localhost',
        'user': 'root',
        'password': 'password',
        'database': 'chatgptebusiness',
        'charset': 'utf8mb4'
    }

    @classmethod
    def setUpClass(cls):
        cls.session = requests.Session()
        cls.db_connection = pymysql.connect(**cls.DB_CONFIG)

    def setUp(self):
        self.csrf_token = self.get_csrf_token()

    def get_csrf_token(self):
        try:
            response = self.session.get(self.BASE_URL)
            token = response.cookies.get('csrftoken') or response.cookies.get('csrf_token')
            if not token:
                import re
                match = re.search(r'name="csrf_token"\s+value="([^"]*)"', response.text)
                token = match.group(1) if match else ""
            return token
        except Exception as e:
            print(f"Failed to get CSRF token: {e}")
            return ""

    def hash_password(self, password):
        """SHA256 hash of the password"""
        return hashlib.sha256(password.encode()).hexdigest()

    def tearDown(self):
        cursor = self.db_connection.cursor()
        try:
            cursor.execute("SET FOREIGN_KEY_CHECKS = 0")
            cursor.execute("DELETE FROM user WHERE username LIKE %s OR email LIKE %s", ("test_user_%", "test_email_%"))
            cursor.execute("SET FOREIGN_KEY_CHECKS = 1")
            self.db_connection.commit()
        except Exception as e:
            print(f"Error during cleanup: {e}")
            self.db_connection.rollback()
        finally:
            cursor.close()

    def test_register_success(self):
        """Successful user registration"""
        username = "test_user_" + str(hash(self._testMethodName))[-8:]
        password = "test123"
        phone = "13800138000"
        email = "test_email_" + str(hash(self._testMethodName))[-8:] + "@example.com"
        hashed_password = self.hash_password(password)
        payload = {
            'username': username,
            'password': hashed_password,
            'phone': phone,
            'email': email,
            'csrf_token': self.csrf_token
        }
        headers = {
            'Content-Type': 'application/x-www-form-urlencoded',
            'X-CSRFToken': self.csrf_token
        }
        response = self.session.post(self.BASE_URL, data=payload, headers=headers)
        context = f"URL: {self.BASE_URL}, Payload: {payload}, Status: {response.status_code}, Response: {response.text[:200]}"
        self.assertEqual(response.status_code, 200, f"Register success failed. Context: {context}")
        self.assertIn('success', response.text.lower(), f"Register success message not found. Context: {context}")
        # Verify database insertion
        cursor = self.db_connection.cursor(pymysql.cursors.DictCursor)
        try:
            cursor.execute("SELECT * FROM user WHERE username = %s AND email = %s", (username, email))
            user_record = cursor.fetchone()
            self.assertIsNotNone(user_record, f"User not found in DB. Context: {context}")
            self.assertEqual(user_record['username'], username, f"Username mismatch in DB. Context: {context}")
        finally:
            cursor.close()

    # Additional test cases (duplicate username, invalid data, CSRF protection, etc.) follow the same pattern and are omitted for brevity.

if __name__ == '__main__':
    unittest.main()

1.6 Example Playwright Test Script (Python + pytest)

import pytest
from playwright.sync_api import sync_playwright
import hashlib
import pymysql

@pytest.fixture(scope="function")
def browser_context():
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=False)
        context = browser.new_context()
        yield context
        browser.close()

def sha256_hash(password):
    return hashlib.sha256(password.encode()).hexdigest()

def get_csrf_token(page):
    try:
        return page.input_value('input[name="csrfmiddlewaretoken"]')
    except:
        return page.evaluate('() => document.querySelector("meta[name=csrf-token]")?.content')

def register_user(page, username, password, phone, email, csrf_token):
    hashed = sha256_hash(password)
    data = {
        'username': username,
        'password': hashed,
        'confirm_password': hashed,
        'phone': phone,
        'email': email,
        'csrfmiddlewaretoken': csrf_token
    }
    return page.request.post('/register', data=data)

def verify_database(sql, params=None):
    conn = pymysql.connect(host='localhost', port=3306, user='root', password='123456', database='chatgptebusiness')
    try:
        with conn.cursor(pymysql.cursors.DictCursor) as cur:
            cur.execute(sql, params)
            return cur.fetchall()
    finally:
        conn.close()

@pytest.fixture(autouse=True)
def setup_database():
    conn = pymysql.connect(host='localhost', port=3306, user='root', password='123456', database='chatgptebusiness')
    try:
        with conn.cursor() as cur:
            cur.execute('SET FOREIGN_KEY_CHECKS = 0')
            cur.execute('DELETE FROM user')
            cur.execute('SET FOREIGN_KEY_CHECKS = 1')
            conn.commit()
    finally:
        conn.close()

def test_register_valid_user_success(browser_context):
    page = browser_context.new_page()
    page.goto('http://localhost:8000/register')
    csrf = get_csrf_token(page)
    response = register_user(page, 'testuser', 'Pass@123', '13800138000', '[email protected]', csrf)
    assert response.status == 200
    assert 'success' in response.text().lower()
    result = verify_database('SELECT * FROM user WHERE username=%s', ('testuser',))
    assert len(result) == 1
    assert result[0]['email'] == '[email protected]'

# Additional test cases (duplicate username, duplicate phone, missing fields, invalid CSRF, etc.) follow the same structure.

The article concludes that with well‑crafted system and user prompts, the intelligent agent can handle a wide range of testing scenarios, automatically producing JSON‑formatted test cases and executable scripts for both API and UI testing.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

test automationCSRFAI testingPlaywrightAPI testingunittestpassword hashingLLM prompts
Woodpecker Software Testing
Written by

Woodpecker Software Testing

The Woodpecker Software Testing public account shares software testing knowledge, connects testing enthusiasts, founded by Gu Xiang, website: www.3testing.com. Author of five books, including "Mastering JMeter Through Case Studies".

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.