File size: 8,850 Bytes
12aabae
e677444
40ce582
e677444
 
40ce582
e677444
 
 
 
 
12aabae
e677444
40ce582
e677444
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40ce582
e677444
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40ce582
 
12aabae
 
40ce582
 
 
12aabae
 
40ce582
 
 
 
 
 
 
07aeca6
40ce582
 
 
 
 
 
e677444
 
40ce582
e677444
40ce582
e677444
40ce582
 
 
e677444
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40ce582
 
 
 
 
 
 
1ab4db2
40ce582
 
12aabae
40ce582
 
 
 
12aabae
40ce582
c9b80e5
40ce582
 
 
 
 
 
 
12aabae
 
e677444
40ce582
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import gradio as gr
from openai import OpenAI
import os
from typing import List, Tuple
import time

# Initialize OpenAI client with Hugging Face endpoint
client = OpenAI(
    base_url="https://api-inference.huggingface.co/v1/",
    api_key=os.getenv("HF_API_TOKEN")
)

MODEL_ID = "Qwen/Qwen2.5-Coder-32B-Instruct"

DEFAULT_SYSTEM_PROMPT = """
You are an expert software testing agent specializing in designing comprehensive test strategies and writing high-quality automated test scripts. Your role is to assist developers, product managers, and quality assurance teams by analyzing features, branch names, or explanations to produce detailed, effective test cases. You excel in identifying edge cases, ensuring robust test coverage, and delivering Playwright test scripts in JavaScript.

Capabilities:
Feature Understanding:

Analyze the feature description, branch name, or user explanation to extract its purpose, expected behavior, and key functionality.
Infer implicit requirements and edge cases that might not be explicitly mentioned.
Test Case Generation:

Design manual test cases for functional, non-functional, and exploratory testing. These should include:
Positive test cases (expected behavior).
Negative test cases (handling invalid inputs or unexpected conditions).
Edge cases (extreme or boundary conditions).
Performance and security-related scenarios, if applicable.
Write automated test cases in Playwright using JavaScript that adhere to modern testing standards.
Playwright Expertise:

Generate Playwright test scripts with modular, reusable code that follows best practices for maintainability and readability.
Use robust selectors (data attributes preferred) and implement techniques like handling asynchronous operations, mocking API responses, and parameterized testing where applicable.
Write test scripts with proper comments, error handling, and clear structure.
Coverage Prioritization:

Focus on high-priority areas like critical user flows, core functionality, and areas prone to failure.
Ensure comprehensive coverage for edge cases to make the system resilient.
Response Guidelines:
Context Analysis:

If the user provides a branch name, infer the feature or functionality it relates to and proceed to generate test cases.
If the user provides a feature explanation, ensure your test cases align with the described functionality and its goals.
Ask clarifying questions if necessary to improve your understanding before generating test cases.
Structured Output:

Start with a brief summary of the feature or inferred functionality based on the input.
Present manual test cases first, with a clear numbering format and detailed steps for testers to follow.
Follow with automated Playwright test scripts, formatted with proper indentation and ready for execution.
Test Cases Format:

Manual Test Cases:
ID: Test case identifier (e.g., TC001).
Title: Clear and descriptive title.
Precondition(s): Any setup required before execution.
Steps: Step-by-step instructions for execution.
Expected Result: The expected outcome of the test.
Playwright Automated Test Cases:
Include setup (browser context and page), reusable utility functions, and parameterized test cases where applicable.
Ensure clear commenting for each section of the script.
Best Practices:

Recommend improvements to testability if the input feature is unclear or incomplete.
Provide tips for maintaining the test suite, such as organizing tests by feature or tagging tests for easy execution.
Sample Output Template:
Feature Summary:

A concise summary of the feature or inferred functionality based on the user input.
Manual Test Cases:

vbnet
Copy
Edit
TC001: Verify successful login with valid credentials  
Precondition(s): The user must have a valid account.  
Steps:  
  1. Navigate to the login page.  
  2. Enter valid username and password.  
  3. Click on the "Login" button.  
Expected Result: The user is redirected to the dashboard.  
Automated Playwright Test Case (JavaScript):

javascript
Copy
Edit
const { test, expect } = require('@playwright/test');

test.describe('Login Feature Tests', () => {
  test('Verify successful login with valid credentials', async ({ page }) => {
    // Navigate to the login page
    await page.goto('https://example.com/login');
    
    // Enter credentials
    await page.fill('#username', 'testuser');
    await page.fill('#password', 'password123');
    
    // Click the login button
    await page.click('button#login');
    
    // Assert redirection to dashboard
    await expect(page).toHaveURL('https://example.com/dashboard');
  });

  test('Verify login fails with invalid credentials', async ({ page }) => {
    // Navigate to the login page
    await page.goto('https://example.com/login');
    
    // Enter invalid credentials
    await page.fill('#username', 'invaliduser');
    await page.fill('#password', 'wrongpassword');
    
    // Click the login button
    await page.click('button#login');
    
    // Assert error message is displayed
    const errorMessage = await page.locator('.error-message');
    await expect(errorMessage).toHaveText('Invalid username or password.');
  });
});
With this structure, you’ll provide detailed, high-quality test plans that are both actionable and easy to implement. Let me know if you'd like additional examples or refinements!

"""

CSS = """
.gr-chatbot { min-height: 500px; border-radius: 15px; }
.special-tag { color: #2ecc71; font-weight: 600; }
footer { display: none !important; }
"""

def format_response(text: str) -> str:
    """Format the response by adding HTML styling to special tags."""
    return text.replace("[Understand]", '\n<strong class="special-tag">[Understand]</strong>\n') \
              .replace("[Plan]", '\n<strong class="special-tag">[Plan]</strong>\n') \
              .replace("[Conclude]", '\n<strong class="special-tag">[Conclude]</strong>\n') \
              .replace("[Reason]", '\n<strong class="special-tag">[Reason]</strong>\n') \
              .replace("[Verify]", '\n<strong class="special-tag">[Verify]</strong>\n')

def generate_response(
    message: str,
    chat_history: List[Tuple[str, str]],
    system_prompt: str,
    temperature: float,
    max_tokens: int
):
    """Generate a response using the OpenAI-compatible Hugging Face API."""
    # Create conversation history
    messages = [{"role": "system", "content": system_prompt}]
    for user_msg, bot_msg in chat_history:
        messages.extend([
            {"role": "user", "content": user_msg},
            {"role": "assistant", "content": bot_msg}
        ])
    messages.append({"role": "user", "content": message})

    # Create new chat history with empty response
    new_history = chat_history + [(message, "")]
    partial_message = ""

    try:
        # Create streaming completion
        stream = client.chat.completions.create(
            model=MODEL_ID,
            messages=messages,
            temperature=temperature,
            max_tokens=max_tokens,
            stream=True
        )

        # Process the stream
        for chunk in stream:
            if chunk.choices[0].delta.content is not None:
                partial_message += chunk.choices[0].delta.content
                formatted = format_response(partial_message)
                new_history[-1] = (message, formatted + "▌")
                yield new_history
                time.sleep(0.01)  # Small delay to prevent UI lag

        # Final update without cursor
        new_history[-1] = (message, format_response(partial_message))
        yield new_history

    except Exception as e:
        error_message = f"Error: {str(e)}"
        new_history[-1] = (message, error_message)
        yield new_history

# Create Gradio interface
with gr.Blocks(css=CSS, theme=gr.themes.Soft()) as demo:
    gr.Markdown("""
    <h1 align="center">🧠 AI Reasoning Assistant</h1>
    <p align="center">Ask me Hard questions</p>
    """)
    
    chatbot = gr.Chatbot(label="Conversation", elem_id="chatbot")
    msg = gr.Textbox(label="Your Question", placeholder="Type your question...")
    
    with gr.Accordion("⚙️ Settings", open=False):
        system_prompt = gr.TextArea(value=DEFAULT_SYSTEM_PROMPT, label="System Instructions")
        temperature = gr.Slider(0, 1, value=0.5, label="Creativity")
        max_tokens = gr.Slider(128, 4096, value=2048, label="Max Response Length")

    clear = gr.Button("Clear History")
    
    msg.submit(
        generate_response,
        [msg, chatbot, system_prompt, temperature, max_tokens],
        [chatbot],
        show_progress=True
    )
    clear.click(lambda: None, None, chatbot, queue=False)

if __name__ == "__main__":
    if not os.getenv("HF_API_TOKEN"):
        print("Please set your Hugging Face API token as environment variable HF_API_TOKEN")
    else:
        demo.queue().launch()