| import time |
| import json |
| import os |
| from playwright.sync_api import sync_playwright |
|
|
| |
| BASE_URL = "http://localhost:8080" |
| |
| TEST_EMAIL = "kaifulimaan@gmail.com" |
| TEST_PASSWORD = "Kk123456," |
| REAL_IMAGE_PATH = r"D:\Projects\CytoSight\cytosight\test_sampe.png" |
| EVIDENCE_DIR = "test_evidence" |
|
|
| if not os.path.exists(EVIDENCE_DIR): |
| os.makedirs(EVIDENCE_DIR) |
|
|
|
|
| def run_test(): |
| if not os.path.exists(REAL_IMAGE_PATH): |
| print(f"β ERROR: Real image not found at {REAL_IMAGE_PATH}") |
| return |
|
|
| with sync_playwright() as p: |
| print("\n" + "="*50) |
| print("π STARTING CYTOSIGHT EVIDENCE-BASED PERFORMANCE TEST") |
| print("="*50 + "\n") |
| |
| browser = p.chromium.launch(headless=False, slow_mo=500) |
| context = browser.new_context() |
| page = context.new_page() |
|
|
| |
| request_start_times = {} |
|
|
| def handle_request(request): |
| if "/api/" in request.url: |
| request_start_times[request.url] = time.time() |
|
|
| def handle_response(response): |
| url = response.url |
| if "/api/" in url and response.status in [200, 201]: |
| try: |
| |
| start_time = request_start_times.get(url) |
| duration = round(time.time() - start_time, 2) if start_time else None |
| |
| |
| data = response.json() |
| |
| |
| data["_performance_metrics"] = { |
| "total_response_time_seconds": duration, |
| "captured_at": time.strftime("%Y-%m-%d %H:%M:%S") |
| } |
|
|
| |
| category = "API_RESPONSE" |
| if "auth/login" in url: |
| category = "LOGIN" |
| elif "diagnosis/history" in url: |
| category = "HISTORY_LOAD" |
| elif "upload" in url: |
| category = "IMAGE_UPLOAD" |
| elif "diagnosis/predict" in url: |
| category = "DIAGNOSIS_MODEL" |
| elif "segmentation/predict" in url: |
| category = "SEGMENTATION_MODEL" |
| elif "explain" in url: |
| category = "EXPLAINABLE_AI" |
|
|
| filename = f"{EVIDENCE_DIR}/{category}_{int(time.time())}.json" |
| with open(filename, "w") as f: |
| json.dump(data, f, indent=4) |
| |
| print(f" π₯ Captured {category} Response ({duration}s): {filename}") |
| except Exception: |
| pass |
|
|
| page.on("request", handle_request) |
| page.on("response", handle_response) |
|
|
| try: |
| |
| print(f"π STEP 1: Logging in as {TEST_EMAIL}...") |
| page.goto(f"{BASE_URL}/login") |
| |
| page.fill('input[name="email"]', TEST_EMAIL) |
| page.fill('input[name="password"]', TEST_PASSWORD) |
| |
| page.click('button[type="submit"]') |
| |
| |
| page.wait_for_selector('text=Welcome back', timeout=30000) |
| print(" β
Login Successful!\n") |
|
|
| |
| print("π STEP 2: Loading History Page...") |
| page.click('text=View Past Diagnosis') |
| page.wait_for_selector('text=Diagnosis History', timeout=30000) |
| |
| time.sleep(2) |
| print(" β
History Loaded.\n") |
|
|
| |
| print("π€ STEP 3: Running Diagnosis...") |
| page.goto(f"{BASE_URL}/upload") |
| |
| page.set_input_files('input[type="file"]', REAL_IMAGE_PATH) |
| print(f" β
Image Selected: {REAL_IMAGE_PATH}") |
| |
| page.click('button:has-text("Disease Diagnosis")') |
| print(" β³ Processing Diagnosis...") |
| |
| page.wait_for_selector('text=Diagnosis Completed', timeout=120000) |
| print(" β
Diagnosis Completed.\n") |
|
|
| |
| print("π€ STEP 4: Running Segmentation...") |
| page.goto(f"{BASE_URL}/upload") |
| |
| page.set_input_files('input[type="file"]', REAL_IMAGE_PATH) |
| page.click('button:has-text("Binary Segmentation")') |
| print(" β³ Processing Segmentation...") |
| |
| page.wait_for_selector('text=Segmentation Results', timeout=120000) |
| print(" β
Segmentation Completed.\n") |
|
|
| print("*"*50) |
| print("π ALL TESTS PASSED. PERFORMANCE DATA SAVED.") |
| print(f"π Evidence available in: {os.path.abspath(EVIDENCE_DIR)}") |
| print("*"*50 + "\n") |
| |
| except Exception as e: |
| print(f"\nβ TEST FAILED: {str(e)}") |
| |
| |
| raise e |
| |
| finally: |
| print("π§Ή Cleaning up...") |
| time.sleep(2) |
| browser.close() |
|
|
| if __name__ == "__main__": |
| run_test() |
|
|