diff --git a/agent/cli.py b/agent/cli.py index 4b95da5..69ac16e 100644 --- a/agent/cli.py +++ b/agent/cli.py @@ -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() \ No newline at end of file diff --git a/agent/menu.py b/agent/menu.py index 7623c80..83f8cda 100644 --- a/agent/menu.py +++ b/agent/menu.py @@ -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", @@ -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() diff --git a/knowledge_base/history.json b/knowledge_base/history.json index db15140..97c4a70 100644 --- a/knowledge_base/history.json +++ b/knowledge_base/history.json @@ -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" } ] } \ No newline at end of file diff --git a/knowledge_base/test_cases/test_cases.json b/knowledge_base/test_cases/test_cases.json index 88a9f08..ddb8308 100644 --- a/knowledge_base/test_cases/test_cases.json +++ b/knowledge_base/test_cases/test_cases.json @@ -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." + } + ] } ] } \ No newline at end of file