File size: 3,455 Bytes
e596b50
 
 
 
 
a8db576
e596b50
 
 
 
 
 
 
 
 
 
 
 
 
a8db576
 
 
 
e596b50
 
a8db576
e596b50
 
 
 
 
 
 
a8db576
e596b50
a8db576
e596b50
 
a8db576
e596b50
 
 
 
 
 
 
 
 
 
a8db576
e596b50
 
 
 
 
 
 
 
 
 
 
 
a8db576
 
 
 
 
e596b50
 
a8db576
 
 
 
e596b50
 
 
 
 
 
a8db576
e596b50
a8db576
e596b50
a8db576
e596b50
 
 
 
 
 
 
 
557fd6f
e596b50
 
 
 
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
import gradio as gr
import cv2
from PIL import Image
import google.generativeai as genai
import os
from dotenv import load_dotenv

load_dotenv()
api_key = os.getenv('api_key')
genai.configure(api_key=api_key)

def extract_frames(video_path, fps=30):
    frames = []
    cap = cv2.VideoCapture(video_path)

    if not cap.isOpened():
        raise ValueError("Error: Unable to open video file.")

    original_fps = cap.get(cv2.CAP_PROP_FPS)
    if original_fps <= 0:
        raise ValueError("Error: Unable to retrieve valid FPS from video file.")

    frame_interval = max(1, int(original_fps // fps))  
    frame_count = 0
    success, frame = cap.read()

    while success:
        if frame_count % frame_interval == 0:
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            pil_image = Image.fromarray(frame)
            frames.append(pil_image)
        frame_count += 1
        success, frame = cap.read()

    cap.release()

    if len(frames) == 0:
        raise ValueError("Error: No frames extracted from the video.")

    return frames

def generate_prompt(button_list):
    button_descriptions = ", ".join(button_list)
    return (
        f"Generate structured test cases for the following UI button(s): {button_descriptions}.\n\n"
        "Functionality\n"
        "- What the button is supposed to do.\n\n"
        "Preconditions\n"
        "- Any requirements before interaction.\n\n"
        "Test Steps\n"
        "- Concise steps for executing the test.\n\n"
        "Expected Results\n"
        "- What the expected outcome should be.\n\n"
        "Automated Testing Tools\n"
        "- Recommended tools for testing.\n\n"
        "Format the output clearly for easy readability, using bullet points for key details."
    )

def process_video(video_file, buttons_to_test):
    try:
        video_path = "uploaded_video.mp4"
        with open(video_path, "wb") as f:
            f.write(video_file)

        frames = extract_frames(video_path, fps=15) 
        frames_to_pass = frames[::10]  # use every 10th frame

        button_list = [button.strip() for button in buttons_to_test.split(",")]
        prompt = generate_prompt(button_list)

        frame_prompts = [frame for frame in frames_to_pass]
        response = genai.GenerativeModel(model_name="gemini-1.5-pro-latest").generate_content([prompt] + frame_prompts)

        output_text = response.text if hasattr(response, 'text') else "Error: Invalid response from the model."
        output_text = output_text.replace("Functionality", "📋 Functionality")
        output_text = output_text.replace("Preconditions", "🔍 Preconditions")
        output_text = output_text.replace("Test Steps", "📝 Test Steps")
        output_text = output_text.replace("Expected Results", "✅ Expected Results")
        output_text = output_text.replace("Automated Testing Tools", "🛠️ Automated Testing Tools")

        return output_text

    except Exception as e:
        return f"Error occurred: {str(e)}"

iface = gr.Interface(
    fn=process_video,
    inputs=[
        gr.File(label="Upload Video", type="binary"),
        gr.Textbox(label="Buttons/Functions to Test (comma-separated)", placeholder="e.g., booking, cancel"),
    ],
    outputs="text",
    title="Video-to-Test Case Generator with Google Gemini",
    description="Upload a video and specify the buttons you want to test. The tool will generate structured test cases."
)

iface.launch(share=True)