File size: 7,733 Bytes
6f38c76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Setup Guide - UI Regression Testing System

Complete setup instructions for local development and HF Spaces deployment.

## Local Development Setup

### Prerequisites

- Python 3.8 or higher
- pip (Python package manager)
- Git
- 2GB RAM minimum

### Step 1: Clone Repository

```bash
git clone https://github.com/YOUR_USERNAME/ui-regression-testing.git
cd ui-regression-testing
```

### Step 2: Create Virtual Environment

```bash
# Create virtual environment
python -m venv venv

# Activate it
# On macOS/Linux:
source venv/bin/activate

# On Windows:
venv\Scripts\activate
```

### Step 3: Install Dependencies

```bash
pip install -r requirements.txt
```

### Step 4: Install Playwright Browsers

```bash
playwright install
```

### Step 5: Create .env File

Create a `.env` file in the project root:

```bash
cat > .env << 'EOF'
FIGMA_ACCESS_TOKEN=your_figma_token_here
FIGMA_FILE_KEY=your_figma_file_id_here
WEBSITE_URL=https://your-website.com
HUGGINGFACE_TOKEN=your_hf_token_here
EOF
```

### Step 6: Run the Application

**Option A: Run Gradio UI (Recommended)**

```bash
python app.py
```

Then open http://localhost:7860 in your browser.

**Option B: Run CLI Version**

```bash
python main.py --execution-id test_001 --output-dir reports
```

## Configuration

### Environment Variables

| Variable | Required | Description |
|----------|----------|-------------|
| `FIGMA_ACCESS_TOKEN` | Yes | Figma API token |
| `FIGMA_FILE_KEY` | Yes | Figma file ID |
| `WEBSITE_URL` | Yes | Website URL to test |
| `HUGGINGFACE_TOKEN` | No | HF token for vision models |

### Project Structure

```
ui-regression-testing/
β”œβ”€β”€ app.py                          # Gradio UI application
β”œβ”€β”€ main.py                         # CLI entry point
β”œβ”€β”€ requirements.txt                # Python dependencies
β”œβ”€β”€ Dockerfile                      # Docker configuration
β”œβ”€β”€ README.md                       # Documentation
β”œβ”€β”€ SETUP.md                        # This file
β”œβ”€β”€ DEPLOYMENT_GUIDE.md             # HF Spaces deployment
β”œβ”€β”€ .env                            # Environment variables (local only)
β”œβ”€β”€ .gitignore                      # Git ignore rules
β”‚
β”œβ”€β”€ state_schema.py                 # Workflow state definition
β”œβ”€β”€ workflow.py                     # LangGraph workflow
β”œβ”€β”€ report_generator.py             # Report generation
β”œβ”€β”€ screenshot_annotator.py         # Screenshot annotation
β”œβ”€β”€ image_comparison_enhanced.py    # Image comparison
β”‚
β”œβ”€β”€ agents/                         # AI agents
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ agent_0_super_agent.py      # Test planning
β”‚   β”œβ”€β”€ agent_1_design_inspector.py # Figma capture
β”‚   β”œβ”€β”€ agent_2_website_inspector.py # Website capture
β”‚   └── agent_3_difference_analyzer.py # Difference detection
β”‚
β”œβ”€β”€ utils/                          # Utility modules
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ figma_client.py             # Figma API client
β”‚   └── website_capturer.py         # Website screenshot
β”‚
β”œβ”€β”€ data/                           # Data directory
β”‚   β”œβ”€β”€ figma/                      # Figma screenshots
β”‚   └── website/                    # Website screenshots
β”‚
└── reports/                        # Generated reports
    β”œβ”€β”€ report_summary.md
    β”œβ”€β”€ report_detailed.md
    └── report_json.json
```

## Development Workflow

### 1. Make Changes

Edit files as needed:

```bash
# Edit an agent
nano agents/agent_1_design_inspector.py

# Edit the UI
nano app.py
```

### 2. Test Locally

```bash
# Run the app
python app.py

# Or run tests
python -m pytest tests/
```

### 3. Commit Changes

```bash
git add .
git commit -m "Description of changes"
git push origin main
```

### 4. Deploy to HF Spaces

The space will automatically rebuild when you push to GitHub.

## Troubleshooting

### Issue: "ModuleNotFoundError: No module named 'playwright'"

**Solution:**
```bash
pip install playwright
playwright install
```

### Issue: "Figma API Key is invalid"

**Solution:**
1. Verify token starts with `figd_`
2. Check token hasn't expired
3. Generate a new token from https://www.figma.com/developers/api#access-tokens

### Issue: "Website URL is not accessible"

**Solution:**
1. Verify URL is correct and publicly accessible
2. Check internet connection
3. Try opening URL in browser manually

### Issue: "No frames found in Figma file"

**Solution:**
1. Verify Figma File ID is correct
2. Ensure file contains frames
3. Check file sharing permissions

### Issue: "Screenshots are blank or gray"

**Solution:**
1. Verify website loads correctly
2. Check for JavaScript errors
3. Ensure website doesn't require authentication
4. Try with a different website

### Issue: "Out of memory error"

**Solution:**
1. Reduce image resolution
2. Process smaller batches
3. Close other applications
4. Upgrade system RAM

## Performance Optimization

### 1. Reduce Image Size

Edit `image_comparison_enhanced.py`:

```python
# Resize images for faster processing
img = img.resize((img.width // 2, img.height // 2))
```

### 2. Cache Results

```python
from functools import lru_cache

@lru_cache(maxsize=10)
def expensive_operation(key):
    # Your code here
    pass
```

### 3. Async Processing

```python
import asyncio

async def process_images():
    # Async code here
    pass
```

## Debugging

### Enable Debug Logging

```python
import logging

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
logger.debug("Debug message")
```

### Check Logs

```bash
# View app logs
tail -f app.log

# View error logs
tail -f error.log
```

### Test Individual Components

```bash
# Test Figma client
python -c "from utils.figma_client import FigmaClient; print('OK')"

# Test website capturer
python -c "from utils.website_capturer import capture_website_sync; print('OK')"
```

## Advanced Configuration

### Custom Figma Frames

Edit `agents/agent_1_design_inspector.py`:

```python
# Specify custom frame names
frame_names = ["CustomFrame1", "CustomFrame2"]
```

### Custom Website Viewports

Edit `agents/agent_2_website_inspector.py`:

```python
# Add custom viewport sizes
viewports = {
    "desktop": {"width": 1920, "height": 1080},
    "tablet": {"width": 768, "height": 1024},
    "mobile": {"width": 375, "height": 667}
}
```

### Custom Comparison Thresholds

Edit `image_comparison_enhanced.py`:

```python
# Adjust sensitivity
threshold = 0.90  # Higher = more sensitive
```

## Testing

### Run Unit Tests

```bash
python -m pytest tests/ -v
```

### Run Integration Tests

```bash
python -m pytest tests/integration/ -v
```

### Test with Sample Data

```bash
python main.py --test-mode --sample-data
```

## Deployment Checklist

Before deploying to HF Spaces:

- [ ] All dependencies listed in requirements.txt
- [ ] .env file not committed to git
- [ ] Dockerfile builds successfully
- [ ] App runs locally without errors
- [ ] README.md is up to date
- [ ] DEPLOYMENT_GUIDE.md is followed
- [ ] All credentials are environment variables
- [ ] Tests pass
- [ ] No hardcoded secrets in code

## Resources

- [Python Documentation](https://docs.python.org/3/)
- [Gradio Documentation](https://www.gradio.app/)
- [Playwright Documentation](https://playwright.dev/)
- [LangGraph Documentation](https://langchain-ai.github.io/langgraph/)
- [Figma API Documentation](https://www.figma.com/developers/api)
- [HF Spaces Documentation](https://huggingface.co/docs/hub/spaces)

## Support

For issues or questions:

1. Check the troubleshooting section above
2. Review the documentation
3. Open an issue on GitHub
4. Contact the maintainers

---

**Happy coding! πŸš€**