File size: 2,415 Bytes
0b1188e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# ai_test_generator/utils.py
import json
import pandas as pd
import logging
import os

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
OUTPUT_DIR = "outputs" # Define output directory

def save_elements_to_json(elements: list[dict], filename: str = "elements.json") -> str:
    """Saves extracted elements to a JSON file."""
    if not os.path.exists(OUTPUT_DIR):
        os.makedirs(OUTPUT_DIR)
    filepath = os.path.join(OUTPUT_DIR, filename)
    try:
        with open(filepath, "w", encoding="utf-8") as f:
            json.dump(elements, f, indent=4)
        logging.info(f"Successfully saved elements to {filepath}")
        return filepath
    except Exception as e:
        logging.error(f"Error saving elements to JSON file {filepath}: {e}", exc_info=True)
        return ""

def save_test_cases_to_excel(test_cases_df: pd.DataFrame, filename: str = "test_cases.xlsx") -> str:
    """Saves test cases DataFrame to an Excel file."""
    if not os.path.exists(OUTPUT_DIR):
        os.makedirs(OUTPUT_DIR)
    filepath = os.path.join(OUTPUT_DIR, filename)
    try:
        test_cases_df.to_excel(filepath, index=False, engine='openpyxl')
        logging.info(f"Successfully saved test cases to {filepath}")
        return filepath
    except Exception as e:
        logging.error(f"Error saving test cases to Excel file {filepath}: {e}", exc_info=True)
        return ""

def save_scripts_to_excel(scripts_data: list[dict], filename: str = "test_scripts.xlsx") -> str:
    """Saves generated scripts along with Test Case IDs to an Excel file."""
    if not os.path.exists(OUTPUT_DIR):
        os.makedirs(OUTPUT_DIR)
    filepath = os.path.join(OUTPUT_DIR, filename)
    try:
        df = pd.DataFrame(scripts_data)
        # Ensure consistent column order
        if not df.empty:
             df = df[['Test Case ID', 'Python Selenium Code']]
        else:
             # Create empty DataFrame with columns if no scripts were generated
             df = pd.DataFrame(columns=['Test Case ID', 'Python Selenium Code'])

        df.to_excel(filepath, index=False, engine='openpyxl')
        logging.info(f"Successfully saved scripts to {filepath}")
        return filepath
    except Exception as e:
        logging.error(f"Error saving scripts to Excel file {filepath}: {e}", exc_info=True)
        return ""