File size: 14,916 Bytes
43932ea 035a63c 43932ea | 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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 | import gradio as gr
from datetime import datetime
from groq import Groq
import traceback
import json
# --- 1. الحصول على مفتاح API ---
import os
# تهيئة المكونات
api_key_coder= os.environ.get('Chat_with_Your_Context')
# تهيئة المكونات
# --- 2. تعريف عميل Groq مباشرة (بدون LangChain) ---
class GroqLLM:
def __init__(self, api_key, model="meta-llama/llama-4-scout-17b-16e-instruct", temperature=0.1):
self.client = Groq(api_key=api_key)
self.model = model
self.temperature = temperature
def invoke(self, prompt):
try:
response = self.client.chat.completions.create(
model=self.model,
messages=[{"role": "user", "content": prompt}],
temperature=self.temperature,
max_tokens=2000
)
return response.choices[0].message.content
except Exception as e:
return f"Error: {str(e)}"
# --- 3. تفعيل LLM ---
llm = GroqLLM(api_key=api_key_coder)
# --- 4. تعريف Dummy Runner و DOM Matcher ---
class DummyRunner:
def run(self, test_script):
# محاكاة لنتائج الاختبارات
if "fail" in test_script.lower() or "assert false" in test_script.lower():
return {
"status": "failed",
"error": "AssertionError: Expected True but got False",
"logs": "Stack trace: line 10 in test_function",
"dom": "<button id='submit-btn' class='btn'>Submit</button>"
}
return {"status": "passed", "message": "All tests passed successfully"}
class DOMMatcher:
def find_similar(self, dom, failed_locator):
# محاكاة لإيجاد locator بديل
return "button#submit-btn", 0.92
runner = DummyRunner()
dom_matcher = DOMMatcher()
# --- 5. تعريف وظائف الأدوات (بدون LangChain) ---
def detect_failure(test_script):
"""اكتشاف فشل الاختبار"""
result = runner.run(test_script)
return result
def analyze_root_cause(failure_data):
"""تحليل سبب الفشل باستخدام LLM"""
error = failure_data.get("error", "Unknown")
logs = failure_data.get("logs", "")
prompt = f"""
Analyze this test failure:
Error: {error}
Logs: {logs}
Provide:
1. Root cause analysis
2. Suggested fix
"""
analysis = llm.invoke(prompt)
return {"root_cause": analysis, "confidence": "high"}
def heal_locator(failure_data):
"""محاولة إصلاح الـ locator"""
dom = failure_data.get("dom", "")
error = failure_data.get("error", "")
new_locator, score = dom_matcher.find_similar(dom, error)
return {"suggested_locator": new_locator, "confidence": score}
def update_script(script_content, old_locator, new_locator):
"""تحديث السكربت بـ locator جديد"""
return script_content.replace(old_locator, new_locator)
def reexecute_test(test_script):
"""إعادة تشغيل الاختبار"""
return runner.run(test_script)
def generate_report(data):
"""توليد تقرير شامل"""
prompt = f"""
Generate a comprehensive QA report based on this data:
{json.dumps(data, indent=2)}
Include:
- Test Execution Summary
- Failures Detected
- Root Cause Analysis
- Healing Actions
- Final Results
- Recommendations
"""
return llm.invoke(prompt)
# --- 6. الدالة الرئيسية التي تنفذ كل الخطوات ---
def run_complete_analysis(test_script):
"""تنفيذ تحليل كامل للاختبار"""
report_data = {
"original_script": test_script,
"steps": [],
"final_result": {},
"healing_applied": False
}
# الخطوة 1: تشغيل الاختبار
result = detect_failure(test_script)
report_data["steps"].append({"step": "initial_execution", "result": result})
# إذا فشل الاختبار
if result["status"] == "failed":
report_data["healing_applied"] = True
# الخطوة 2: تحليل السبب
analysis = analyze_root_cause(result)
report_data["steps"].append({"step": "root_cause_analysis", "analysis": analysis})
# الخطوة 3: محاولة الإصلاح
healing = heal_locator(result)
report_data["steps"].append({"step": "healing_attempt", "healing": healing})
# الخطوة 4: تحديث السكربت
if "suggested_locator" in healing:
old = "button"
new = healing["suggested_locator"]
updated_script = update_script(test_script, old, new)
report_data["steps"].append({"step": "script_updated", "new_script": updated_script})
# الخطوة 5: إعادة التشغيل
final_result = reexecute_test(updated_script)
report_data["final_result"] = final_result
report_data["steps"].append({"step": "re_execution", "result": final_result})
else:
report_data["final_result"] = result
# الخطوة 6: توليد التقرير
report = generate_report(report_data)
report_data["full_report"] = report
return report_data
# SmartQA Full System with Multi-Source Test Generation + HealTest Integration
# Uses existing GroqLLM instance: llm = GroqLLM(api_key=api_key_coder)
# ==============================
# 1. Knowledge Input Definition
# ==============================
class KnowledgeInput:
def __init__(
self,
requirements=None,
dom=None,
api_spec=None,
user_flows=None,
source_code=None,
recording=None
):
self.requirements = requirements
self.dom = dom
self.api_spec = api_spec
self.user_flows = user_flows
self.source_code = source_code
self.recording = recording
# ==============================
# 2. Knowledge Processor
# ==============================
class KnowledgeProcessor:
def parse_requirements(self, text):
return text.strip()
def parse_dom(self, dom_text):
return dom_text[:4000]
def parse_api(self, api_text):
return api_text[:4000]
def parse_flows(self, flows_text):
return flows_text.strip()
def analyze_code(self, code_text):
return code_text[:4000]
def parse_recording(self, rec_text):
return rec_text.strip()
def process(self, knowledge: KnowledgeInput):
data = {}
if knowledge.requirements:
data["req"] = self.parse_requirements(knowledge.requirements)
if knowledge.dom:
data["ui"] = self.parse_dom(knowledge.dom)
if knowledge.api_spec:
data["api"] = self.parse_api(knowledge.api_spec)
if knowledge.user_flows:
data["flows"] = self.parse_flows(knowledge.user_flows)
if knowledge.source_code:
data["code"] = self.analyze_code(knowledge.source_code)
if knowledge.recording:
data["record"] = self.parse_recording(knowledge.recording)
return data
# ==============================
# 3. Test Generator (LLM-based)
# ==============================
class TestGenerator:
def __init__(self, llm):
self.llm = llm
def generate_req_tests(self, data):
prompt = f"""
Generate Python Selenium automated test scripts from requirements.
Requirements:\n{data['req']}
Include:
- pytest format
- locators placeholders
- assertions
"""
return self.llm.invoke(prompt)
def generate_ui_tests(self, data):
prompt = f"""
Generate Selenium UI tests from HTML DOM.
DOM:\n{data['ui']}
"""
return self.llm.invoke(prompt)
def generate_api_tests(self, data):
prompt = f"""
Generate Python API tests using requests from OpenAPI/Swagger spec.
Spec:\n{data['api']}
"""
return self.llm.invoke(prompt)
def generate_flow_tests(self, data):
prompt = f"""
Generate end-to-end Selenium tests from user flows.
Flows:\n{data['flows']}
"""
return self.llm.invoke(prompt)
def generate_code_tests(self, data):
prompt = f"""
Analyze source code and generate relevant automated tests.
Code:\n{data['code']}
"""
return self.llm.invoke(prompt)
def generate_record_tests(self, data):
prompt = f"""
Convert user interaction recording into Selenium test script.
Recording:\n{data['record']}
"""
return self.llm.invoke(prompt)
def generate(self, processed_data):
if "api" in processed_data:
return self.generate_api_tests(processed_data)
if "ui" in processed_data:
return self.generate_ui_tests(processed_data)
if "flows" in processed_data:
return self.generate_flow_tests(processed_data)
if "req" in processed_data:
return self.generate_req_tests(processed_data)
if "code" in processed_data:
return self.generate_code_tests(processed_data)
if "record" in processed_data:
return self.generate_record_tests(processed_data)
return "No valid input provided"
# ==============================
# 4. HealTest Engine (Wrapper)
# ==============================
class HealTestEngine:
def __init__(self):
pass
def run_complete_analysis(self, test_script):
# Uses existing functions from your notebook
result = detect_failure(test_script)
if result["status"] == "failed":
analysis = analyze_root_cause(result)
healed = heal_locator(result)
updated_script = update_script(
test_script,
result.get("failed_locator", "old_locator"),
healed.get("new_locator", "new_locator")
)
re_result = reexecute_test(updated_script)
else:
analysis = "No failure"
healed = {}
updated_script = test_script
re_result = result
report_data = {
"original": test_script,
"analysis": analysis,
"healing": healed,
"final_result": re_result
}
report = generate_report(report_data)
return {
"generated_test": test_script,
"updated_test": updated_script,
"initial_result": result,
"final_result": re_result,
"report": report
}
# ==============================
# 5. SmartQA System
# ==============================
class SmartQASystem:
def __init__(self, llm):
self.processor = KnowledgeProcessor()
self.generator = TestGenerator(llm)
self.healer = HealTestEngine()
def run(self, knowledge: KnowledgeInput):
processed = self.processor.process(knowledge)
generated_tests = self.generator.generate(processed)
results = self.healer.run_complete_analysis(generated_tests)
return results
# ==============================
# 6. Gradio Interface
# ==============================
import gradio as gr
# --- 3. تفعيل LLM ---
system = SmartQASystem(llm)
def run_smartqa(requirements, dom, api_spec, flows, code, recording):
knowledge = KnowledgeInput(
requirements=requirements,
dom=dom,
api_spec=api_spec,
user_flows=flows,
source_code=code,
recording=recording
)
result = system.run(knowledge)
return (
result["generated_test"],
result["updated_test"],
str(result["initial_result"]),
str(result["final_result"]),
result["report"]
)
with gr.Blocks() as demo:
gr.Markdown("# 🧠 SmartQA — Multi-Source AI Test Generation & Self-Healing")
gr.Markdown("Provide any knowledge source to generate and heal automated tests")
with gr.Tab("Requirements"):
req_input = gr.Textbox(lines=8, label="Requirements")
with gr.Tab("UI / DOM"):
dom_input = gr.Textbox(lines=12, label="HTML DOM")
with gr.Tab("API Spec"):
api_input = gr.Textbox(lines=12, label="OpenAPI / Swagger")
with gr.Tab("User Flows"):
flow_input = gr.Textbox(lines=8, label="User Flows")
with gr.Tab("Source Code"):
code_input = gr.Textbox(lines=12, label="Source Code")
with gr.Tab("Recording"):
rec_input = gr.Textbox(lines=8, label="Interaction Recording")
# --- صف الأزرار أفقيًا ---
with gr.Row():
run_btn = gr.Button("🚀 Generate & Heal Tests", variant="primary")
example_btn = gr.Button("📂 Load Example Data")
# --- النتائج ---
gr.Markdown("## Results")
gen_out = gr.Code(label="Generated Test")
upd_out = gr.Code(label="Healed Test")
init_out = gr.Textbox(label="Initial Execution Result")
final_out = gr.Textbox(label="Final Execution Result")
report_out = gr.Textbox(lines=12, label="QA Report")
# --- بيانات أمثلة ---
example_requirements = """
User can login with email and password
User can search for a product
User can add product to cart
"""
example_dom = """
<html>
<body>
<input id=\"email\" />
<input id=\"password\" />
<button id=\"login-btn\">Login</button>
<input id=\"search\" />
<button id=\"search-btn\">Search</button>
<button id=\"add-cart\">Add to Cart</button>
</body>
</html>
"""
example_api = """
POST /login
Body: { email, password }
GET /products
POST /cart
Body: { product_id }
"""
example_flows = """
Open login page
Enter email and password
Click login
Search product
Add to cart
"""
example_code = """
@app.route('/login', methods=['POST'])
def login():
email = request.json['email']
password = request.json['password']
if authenticate(email, password):
return {'status': 'ok'}
return {'status': 'fail'}, 401
"""
example_recording = """
User navigates to /login
Types email test@mail.com
Types password 123456
Clicks Login button
Navigates to /products
Clicks Add to Cart
"""
def load_examples():
return (
example_requirements,
example_dom,
example_api,
example_flows,
example_code,
example_recording
)
# --- ربط الأزرار ---
run_btn.click(
fn=run_smartqa,
inputs=[req_input, dom_input, api_input, flow_input, code_input, rec_input],
outputs=[gen_out, upd_out, init_out, final_out, report_out]
)
example_btn.click(
fn=load_examples,
inputs=[],
outputs=[req_input, dom_input, api_input, flow_input, code_input, rec_input]
)
demo.launch(debug=True)
|