File size: 9,349 Bytes
00daf25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Screenshot capture tool for ChatUI Helper documentation
Uses Playwright to automate browser interactions and capture screenshots
"""

import asyncio
import os
from pathlib import Path
from playwright.async_api import async_playwright
import logging
from datetime import datetime

# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s')
logger = logging.getLogger(__name__)

# Screenshot configurations for each documentation step
SCREENSHOT_CONFIGS = [
    {
        "name": "main_interface",
        "url": "http://localhost:7860",
        "description": "Main interface with all tabs",
        "actions": [],
        "wait_time": 2000,
        "selector": None,  # Full page
        "output": "img/main_interface.png"
    },
    {
        "name": "config_tab",
        "url": "http://localhost:7860",
        "description": "Configuration tab",
        "actions": [
            {"type": "click", "selector": "button:has-text('Configure Space')"}
        ],
        "wait_time": 1000,
        "selector": None,
        "output": "img/config_tab.png"
    },
    {
        "name": "template_dropdown",
        "url": "http://localhost:7860",
        "description": "Template selection dropdown",
        "actions": [
            {"type": "click", "selector": "button:has-text('Configure Space')"},
            {"type": "click", "selector": "div[data-testid='dropdown']", "nth": 0}
        ],
        "wait_time": 1000,
        "selector": "div.dropdown-menu",
        "output": "img/template_dropdown.png"
    },
    {
        "name": "preview_tab",
        "url": "http://localhost:7860",
        "description": "Preview tab with chat interface",
        "actions": [
            {"type": "click", "selector": "button:has-text('Preview')"}
        ],
        "wait_time": 2000,
        "selector": None,
        "output": "img/preview_tab.png"
    },
    {
        "name": "preview_chat_example",
        "url": "http://localhost:7860",
        "description": "Preview tab with example conversation",
        "actions": [
            {"type": "click", "selector": "button:has-text('Preview')"},
            {"type": "wait", "time": 2000},
            {"type": "type", "selector": "textarea[placeholder*='Type a message']", "text": "Hello! Can you help me understand quantum mechanics?"},
            {"type": "click", "selector": "button:has-text('Send')"},
            {"type": "wait", "time": 3000}
        ],
        "wait_time": 1000,
        "selector": None,
        "output": "img/preview_chat_example.png"
    },
    {
        "name": "docs_tab",
        "url": "http://localhost:7860",
        "description": "Documentation tab",
        "actions": [
            {"type": "click", "selector": "button:has-text('Docs')"}
        ],
        "wait_time": 1000,
        "selector": None,
        "output": "img/docs_tab.png"
    },
    {
        "name": "docs_quickstart",
        "url": "http://localhost:7860",
        "description": "Documentation quick start guide",
        "actions": [
            {"type": "click", "selector": "button:has-text('Docs')"},
            {"type": "wait", "time": 1000},
            {"type": "click", "selector": "button:has-text('πŸ“– Quick Start Guide')"}
        ],
        "wait_time": 1000,
        "selector": None,
        "output": "img/docs_quickstart.png"
    },
    {
        "name": "config_system_prompt",
        "url": "http://localhost:7860",
        "description": "System configuration section",
        "actions": [
            {"type": "click", "selector": "button:has-text('Configure Space')"},
            {"type": "wait", "time": 1000},
            {"type": "scroll", "y": 500}
        ],
        "wait_time": 1000,
        "selector": None,
        "output": "img/config_system_prompt.png"
    },
    {
        "name": "config_api_section",
        "url": "http://localhost:7860",
        "description": "API configuration section",
        "actions": [
            {"type": "click", "selector": "button:has-text('Configure Space')"},
            {"type": "wait", "time": 1000},
            {"type": "scroll", "y": 1200}
        ],
        "wait_time": 1000,
        "selector": None,
        "output": "img/config_api_section.png"
    },
    {
        "name": "generate_button",
        "url": "http://localhost:7860",
        "description": "Generate deployment package button",
        "actions": [
            {"type": "click", "selector": "button:has-text('Configure Space')"},
            {"type": "wait", "time": 1000},
            {"type": "scroll", "y": 2000}
        ],
        "wait_time": 1000,
        "selector": "button:has-text('πŸ—³οΈ Generate Deployment Package')",
        "output": "img/generate_button.png"
    }
]

async def capture_screenshot(page, config):
    """Capture a single screenshot based on configuration"""
    logger.info(f"Capturing screenshot: {config['name']} - {config['description']}")
    
    try:
        # Navigate to URL
        await page.goto(config['url'])
        
        # Execute actions
        for action in config.get('actions', []):
            if action['type'] == 'click':
                selector = action['selector']
                nth = action.get('nth', 0)
                if nth > 0:
                    await page.locator(selector).nth(nth).click()
                else:
                    await page.click(selector)
                await page.wait_for_timeout(500)
                
            elif action['type'] == 'type':
                await page.fill(action['selector'], action['text'])
                
            elif action['type'] == 'wait':
                await page.wait_for_timeout(action['time'])
                
            elif action['type'] == 'scroll':
                await page.evaluate(f"window.scrollBy(0, {action['y']})")
                await page.wait_for_timeout(500)
        
        # Wait for any animations to complete
        await page.wait_for_timeout(config.get('wait_time', 1000))
        
        # Take screenshot
        output_path = Path(config['output'])
        output_path.parent.mkdir(parents=True, exist_ok=True)
        
        if config.get('selector'):
            # Screenshot of specific element
            element = page.locator(config['selector'])
            await element.screenshot(path=str(output_path))
        else:
            # Full page screenshot
            await page.screenshot(path=str(output_path), full_page=False)
            
        logger.info(f"βœ… Saved screenshot to {output_path}")
        return True
        
    except Exception as e:
        logger.error(f"❌ Failed to capture {config['name']}: {str(e)}")
        return False

async def capture_all_screenshots():
    """Capture all configured screenshots"""
    logger.info("Starting screenshot capture process...")
    
    # Ensure the app is running
    logger.info("Make sure the Gradio app is running on http://localhost:7860")
    
    async with async_playwright() as p:
        # Launch browser
        browser = await p.chromium.launch(headless=False)  # Set to True for headless mode
        context = await browser.new_context(
            viewport={'width': 1280, 'height': 800},
            device_scale_factor=2  # Higher quality screenshots
        )
        page = await context.new_page()
        
        # Capture each screenshot
        success_count = 0
        for config in SCREENSHOT_CONFIGS:
            if await capture_screenshot(page, config):
                success_count += 1
            await page.wait_for_timeout(1000)  # Brief pause between screenshots
        
        # Clean up
        await browser.close()
        
    logger.info(f"\nCapture complete! {success_count}/{len(SCREENSHOT_CONFIGS)} screenshots captured successfully.")
    
    # Create a summary report
    report_path = Path("img/screenshot_report.txt")
    with open(report_path, 'w') as f:
        f.write(f"Screenshot Capture Report\n")
        f.write(f"Generated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")
        f.write(f"Total Screenshots: {len(SCREENSHOT_CONFIGS)}\n")
        f.write(f"Successful: {success_count}\n\n")
        
        f.write("Screenshots Generated:\n")
        for config in SCREENSHOT_CONFIGS:
            f.write(f"- {config['output']}: {config['description']}\n")
    
    logger.info(f"Report saved to {report_path}")

async def capture_single_screenshot(name):
    """Capture a single screenshot by name"""
    config = next((c for c in SCREENSHOT_CONFIGS if c['name'] == name), None)
    if not config:
        logger.error(f"No configuration found for screenshot: {name}")
        return
    
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=False)
        context = await browser.new_context(
            viewport={'width': 1280, 'height': 800},
            device_scale_factor=2
        )
        page = await context.new_page()
        
        await capture_screenshot(page, config)
        
        await browser.close()

def main():
    """Main entry point"""
    import sys
    
    if len(sys.argv) > 1:
        # Capture specific screenshot
        asyncio.run(capture_single_screenshot(sys.argv[1]))
    else:
        # Capture all screenshots
        asyncio.run(capture_all_screenshots())

if __name__ == "__main__":
    main()