Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions agent/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,5 +485,97 @@ def export_tests_history():

click.echo(f"\n✅ Test cases history exported to knowledge_base/test_cases_history.md\n")

# Command: generate test case with AI
@cli.command()
def generate_test():
"""Generate a test case automatically using AI."""
import ollama

click.echo("\n🤖 AI Test Case Generator\n")

feature = click.prompt(" Describe the feature to test")
area = click.prompt(" Area (ex: Project/Login)")
assigned_to = click.prompt(" Assigned to")

prompt = f"""
You are a QA expert. Generate a test case for the following feature:

Feature: {feature}
Area: {area}

Respond ONLY in this exact JSON format, nothing else:
{{
"title": "test case title",
"steps": [
{{
"step": 1,
"action": "action description",
"expected": "expected result"
}}
]
}}

Generate between 3 and 5 steps. Be specific and technical.
"""

click.echo("\n⏳ Generating test case...\n")

response = ollama.chat(
model="llama3.2",
messages=[{"role": "user", "content": prompt}]
)

import json as json_module
try:
raw = response["message"]["content"]
start = raw.find("{")
end = raw.rfind("}") + 1
generated = json_module.loads(raw[start:end])
except Exception:
click.echo("\n❌ Error generating test case. Please try again.\n")
return

click.echo(f"\n📋 Generated Test Case:\n")
click.echo(f" Title: {generated['title']}")
click.echo(f"\n Steps:")
for step in generated["steps"]:
click.echo(f"\n {step['step']}. {step['action']}")
click.echo(f" ✅ Expected: {step['expected']}")

click.echo("")
confirm = click.confirm(" Save this test case?", default=True)

if not confirm:
click.echo("\n❌ Test case not saved.\n")
return

with open(TEST_CASES_PATH, "r") as f:
data = json.load(f)

existing_ids = [tc["id"] for tc in data["test_cases"]]
numbers = [int(id.replace("TC-", "")) for id in existing_ids if id.startswith("TC-")]
next_id = f"TC-{str(max(numbers) + 1).zfill(3)}" if numbers else "TC-001"

new_test = {
"id": next_id,
"work_item_type": "Test Case",
"title": generated["title"],
"area_path": area,
"assigned_to": assigned_to,
"state": "Active",
"playwright_file": "",
"steps": generated["steps"]
}

data["test_cases"].append(new_test)

with open(TEST_CASES_PATH, "w") as f:
json.dump(data, f, indent=2, ensure_ascii=False)

log_event("test_created", next_id, f"Test case '{generated['title']}' generated by AI")

click.echo(f"\n✅ Test case '{next_id}' saved successfully!\n")


if __name__ == "__main__":
cli()
5 changes: 5 additions & 0 deletions agent/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def test_cases_menu():
"👁️ View Test Cases",
"🔍 Search Test Cases",
"➕ Create Test Case",
"🤖 Generate Test Case with AI",
"✏️ Update Test Case",
"👤 Assign Test Case",
"🎭 Run Playwright Test",
Expand Down Expand Up @@ -70,6 +71,10 @@ def test_cases_menu():
elif choice == "➕ Create Test Case":
run_command(["add-test"])
wait_for_menu()

elif "Generate Test Case with AI" in choice:
run_command(["generate-test"])
wait_for_menu()

elif choice == "✏️ Update Test Case":
test_id = questionary.text("Test Case ID (e.g. TC-001):").ask()
Expand Down
6 changes: 6 additions & 0 deletions knowledge_base/history.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@
"type": "test_run",
"test_id": "TC-001",
"details": "Playwright test ran — result: Passed"
},
{
"timestamp": "2026-08-11 10:31",
"type": "test_created",
"test_id": "TC-003",
"details": "Test case 'Log in Test Case' generated by AI"
}
]
}
26 changes: 26 additions & 0 deletions knowledge_base/test_cases/test_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,32 @@
"expected": "Error message is displayed"
}
]
},
{
"id": "TC-003",
"work_item_type": "Test Case",
"title": "Log in Test Case",
"area_path": "login",
"assigned_to": "Diogo",
"state": "Active",
"playwright_file": "",
"steps": [
{
"step": 1,
"action": "Open the login page of the application in a web browser.",
"expected": "The login page should be displayed with the expected fields (username and password)"
},
{
"step": 2,
"action": "Enter valid credentials ('username' and 'password') into the respective fields on the login page.",
"expected": "A 'Login Successful' message should be displayed after clicking the submit button."
},
{
"step": 3,
"action": "Enter invalid credentials ('wrong username' and 'wrong password') into the respective fields on the login page.",
"expected": "An error message ('Invalid credentials') should be displayed next to the submit button."
}
]
}
]
}