File size: 16,932 Bytes
5c3112b | 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 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 | # Implementation Guide: Conversion Logic in Playwright Studies Portal
## ๐ Overview
This document explains how the conversion from Cypress to Playwright is actually implemented in the Playwright Studies Portal codebase. It covers the technical implementation details, code structure, and algorithms used for the conversion process.
## ๐๏ธ Architecture Overview
```
Playwright Studies Portal
โโโ main.py # Main Gradio application
โโโ components/
โ โโโ interfaces.py # UI components and conversion logic
โโโ data/
โ โโโ migration_guide.py # Conversion examples and patterns
โ โโโ best_practices.py # Best practices data
โ โโโ oop_principles.py # OOP principles examples
โโโ services/
โ โโโ ai_service.py # AI-powered assistance
โโโ models/
โ โโโ database.py # SQLAlchemy models for progress tracking
โโโ utils/
โโโ helpers.py # Utility functions
```
## ๐ง Conversion Engine Implementation
### Core Conversion Function
Located in `components/interfaces.py`, the main conversion logic:
```python
def convert_cypress_code(cypress_code: str, conversion_type: str) -> Tuple[str, str]:
"""Convert Cypress code to Playwright"""
# Basic conversion patterns
conversions = {
"basic_syntax": {
"cy.get(": "page.locator(",
"cy.visit(": "await page.goto(",
".type(": ".fill(",
".click()": ".click()",
".should('be.visible')": "await expect(...).toBeVisible()",
".should('contain',": "await expect(...).toContainText(",
"describe(": "test.describe(",
"it(": "test(",
"beforeEach(": "test.beforeEach(async ({ page }) => {",
},
"assertions": {
".should('be.visible')": "await expect(locator).toBeVisible()",
".should('not.exist')": "await expect(locator).not.toBeVisible()",
".should('contain',": "await expect(locator).toContainText(",
".should('have.text',": "await expect(locator).toHaveText(",
".should('have.value',": "await expect(locator).toHaveValue(",
"cy.url().should('include',": "await expect(page).toHaveURL(",
},
"commands": {
"cy.get(": "page.locator(",
"cy.visit(": "await page.goto(",
"cy.wait(": "await page.waitForTimeout(",
"cy.intercept(": "await page.route(",
"cy.fixture(": "// Use test data factory instead",
}
}
converted_code = cypress_code
explanation_parts = []
# Apply conversions based on type
if conversion_type in conversions:
for cypress_pattern, playwright_pattern in conversions[conversion_type].items():
if cypress_pattern in converted_code:
converted_code = converted_code.replace(cypress_pattern, playwright_pattern)
explanation_parts.append(f"โข {cypress_pattern} โ {playwright_pattern}")
# Add async/await if not present
if "await " not in converted_code and ("page." in converted_code or "expect(" in converted_code):
converted_code = f"// Add async/await syntax\n{converted_code}"
explanation_parts.append("โข Added async/await syntax requirement")
explanation = "**Conversion Changes:**\n" + "\n".join(explanation_parts) if explanation_parts else "No direct conversions needed."
return converted_code, explanation
```
### Conversion Categories
The system categorizes conversions into three main types:
1. **Basic Syntax**: Core command translations
2. **Assertions**: Expectation and validation patterns
3. **Commands**: Complex operations and utilities
### Pattern Matching Algorithm
```python
# Simple string replacement approach
for cypress_pattern, playwright_pattern in conversions[conversion_type].items():
if cypress_pattern in converted_code:
converted_code = converted_code.replace(cypress_pattern, playwright_pattern)
explanation_parts.append(f"โข {cypress_pattern} โ {playwright_pattern}")
```
**Pros:**
- Simple and fast
- Easy to understand and maintain
- Good for basic conversions
**Limitations:**
- No context awareness
- May produce invalid code in complex scenarios
- Requires manual pattern definition
## ๐ Data Structure: Migration Examples
### Core Examples Storage
Located in `data/migration_guide.py`:
```python
SYNTAX_COMPARISONS = [
{
"title": "Basic Test Structure",
"cypress_code": """// Cypress
describe('Login Tests', () => {
beforeEach(() => {
cy.visit('/login');
});
it('should login successfully', () => {
cy.get('[data-testid="username"]').type('user@example.com');
cy.get('[data-testid="password"]').type('password123');
cy.get('[data-testid="login-button"]').click();
cy.url().should('include', '/dashboard');
});
});""",
"playwright_code": """// Playwright
import { test, expect } from '@playwright/test';
test.describe('Login Tests', () => {
test.beforeEach(async ({ page }) => {
await page.goto('/login');
});
test('should login successfully', async ({ page }) => {
await page.getByTestId('username').fill('user@example.com');
await page.getByTestId('password').fill('password123');
await page.getByTestId('login-button').click();
await expect(page).toHaveURL(/.*dashboard/);
});
});""",
"explanation": """Key differences:
1. Playwright uses async/await syntax
2. Page object is passed as parameter
3. Different selector methods (getByTestId vs get)
4. Different assertion syntax (expect vs should)"""
}
]
```
### Configuration Examples
```python
CONFIG_MIGRATION = {
"cypress_config": """// cypress.config.js
const { defineConfig } = require('cypress');
module.exports = defineConfig({
e2e: {
baseUrl: 'http://localhost:3000',
viewportWidth: 1280,
viewportHeight: 720,
defaultCommandTimeout: 10000,
// ... more config
},
});""",
"playwright_config": """// playwright.config.js
import { defineConfig, devices } from '@playwright/test';
export default defineConfig({
testDir: './tests',
timeout: 30 * 1000,
use: {
baseURL: 'http://localhost:3000',
actionTimeout: 10000,
// ... more config
},
projects: [
{ name: 'chromium', use: { ...devices['Desktop Chrome'] } },
{ name: 'firefox', use: { ...devices['Desktop Firefox'] } },
{ name: 'webkit', use: { ...devices['Desktop Safari'] } },
],
});""",
"explanation": """Key differences:
1. Playwright uses projects for multi-browser testing
2. Built-in parallel execution support
3. Different timeout configurations"""
}
```
## ๐จ UI Implementation
### Gradio Interface Structure
```python
def create_migration_interface() -> gr.Interface:
"""Create the Cypress to Playwright migration interface"""
with gr.Row():
with gr.Column(scale=1):
# Input section
cypress_input = gr.Code(
label="Cypress Code",
language="javascript",
lines=10
)
conversion_type = gr.Dropdown(
choices=["basic_syntax", "assertions", "commands"],
value="basic_syntax",
label="Conversion Type"
)
convert_btn = gr.Button("Convert to Playwright", variant="primary")
with gr.Column(scale=1):
# Output section
playwright_output = gr.Code(
label="Playwright Code",
language="javascript",
lines=10
)
explanation_output = gr.Markdown(label="Conversion Explanation")
# Event handlers
convert_btn.click(
fn=convert_cypress_code,
inputs=[cypress_input, conversion_type],
outputs=[playwright_output, explanation_output]
)
return interface
```
### Interactive Features
1. **Real-time Conversion**: Live code transformation
2. **Syntax Highlighting**: JavaScript/TypeScript support
3. **Explanation Generation**: Context-aware explanations
4. **Multiple Conversion Types**: Categorized conversion patterns
## ๐ค AI-Powered Enhancement
### AI Service Integration
Located in `services/ai_service.py`:
```python
class KimiAIService:
def __init__(self):
self.model_name = "moonshotai/Kimi-K2-Instruct"
# Initialize model if available
def generate_response(self, query: str, context: str = "") -> str:
"""Generate AI response for conversion queries"""
prompt = self._format_educational_prompt(query, context)
try:
# Use AI model for advanced conversion assistance
response = self._query_model(prompt)
return response
except Exception as e:
# Fallback to rule-based response
return self._get_fallback_response(query)
def _format_educational_prompt(self, query: str, context: str) -> str:
"""Format prompt for educational context"""
return f"""
You are an expert in test automation, specifically helping with Cypress to Playwright migration.
Context: {context}
Query: {query}
Provide practical, educational guidance with code examples.
Focus on best practices and common migration patterns.
"""
```
### AI-Enhanced Features
1. **Contextual Help**: Smart assistance based on user queries
2. **Code Analysis**: Understanding complex conversion scenarios
3. **Best Practice Suggestions**: AI-driven recommendations
4. **Educational Explanations**: Detailed learning content
## ๐ Progress Tracking System
### Database Models
```python
# models/database.py
class StudyModule(Base):
__tablename__ = "study_modules"
id = Column(Integer, primary_key=True)
title = Column(String(200), nullable=False)
description = Column(Text)
module_type = Column(String(50)) # migration, best_practices, etc.
content = Column(JSON) # Structured content data
created_at = Column(DateTime, default=datetime.utcnow)
class StudyProgress(Base):
__tablename__ = "study_progress"
id = Column(Integer, primary_key=True)
user_id = Column(Integer, ForeignKey("users.id"))
module_id = Column(Integer, ForeignKey("study_modules.id"))
completed = Column(Boolean, default=False)
completion_date = Column(DateTime)
notes = Column(Text)
```
### Progress Integration
```python
def track_conversion_usage(user_id: int, conversion_type: str):
"""Track user's conversion tool usage"""
session = get_db()
try:
# Find or create progress record
progress = session.query(StudyProgress).filter_by(
user_id=user_id,
module_type="migration",
conversion_type=conversion_type
).first()
if not progress:
progress = StudyProgress(
user_id=user_id,
module_type="migration",
conversion_type=conversion_type,
usage_count=1
)
session.add(progress)
else:
progress.usage_count += 1
progress.last_used = datetime.utcnow()
session.commit()
finally:
session.close()
```
## ๐ง Advanced Conversion Features
### Pattern Recognition Engine
For future enhancement, a more sophisticated pattern recognition system:
```python
import re
from typing import Dict, List, Tuple
class AdvancedConverter:
def __init__(self):
self.patterns = self._load_conversion_patterns()
def convert_with_context(self, code: str) -> str:
"""Convert code considering context and structure"""
# Parse code structure
ast = self._parse_javascript(code)
# Apply contextual conversions
converted_ast = self._apply_conversions(ast)
# Generate converted code
return self._generate_code(converted_ast)
def _parse_javascript(self, code: str):
"""Parse JavaScript/TypeScript code into AST"""
# Implementation would use a JS parser
pass
def _apply_conversions(self, ast):
"""Apply conversion rules based on AST context"""
# Context-aware conversions
pass
```
### Configuration Generator
```python
def generate_playwright_config(cypress_config: Dict) -> str:
"""Generate Playwright config from Cypress config"""
playwright_config = {
"testDir": "./tests",
"timeout": cypress_config.get("defaultCommandTimeout", 30000),
"use": {
"baseURL": cypress_config.get("baseUrl"),
"viewport": {
"width": cypress_config.get("viewportWidth", 1280),
"height": cypress_config.get("viewportHeight", 720)
}
},
"projects": [
{"name": "chromium", "use": "...devices['Desktop Chrome']"},
{"name": "firefox", "use": "...devices['Desktop Firefox']"},
{"name": "webkit", "use": "...devices['Desktop Safari']"}
]
}
return format_config_as_javascript(playwright_config)
```
## ๐ Performance Optimizations
### Caching Strategy
```python
from functools import lru_cache
@lru_cache(maxsize=1000)
def convert_code_cached(code: str, conversion_type: str) -> Tuple[str, str]:
"""Cached version of code conversion"""
return convert_cypress_code(code, conversion_type)
# Clear cache when patterns are updated
def update_conversion_patterns():
convert_code_cached.cache_clear()
# Update patterns
```
### Parallel Processing
```python
import asyncio
from concurrent.futures import ThreadPoolExecutor
async def convert_multiple_files(file_paths: List[str]) -> List[str]:
"""Convert multiple files in parallel"""
with ThreadPoolExecutor(max_workers=4) as executor:
tasks = [
asyncio.get_event_loop().run_in_executor(
executor, convert_file, path
) for path in file_paths
]
results = await asyncio.gather(*tasks)
return results
```
## ๐ Analytics and Insights
### Usage Tracking
```python
class ConversionAnalytics:
def track_conversion(self, user_id: int, conversion_data: Dict):
"""Track conversion usage for analytics"""
analytics_data = {
"user_id": user_id,
"timestamp": datetime.utcnow(),
"conversion_type": conversion_data["type"],
"input_length": len(conversion_data["input"]),
"output_length": len(conversion_data["output"]),
"patterns_matched": conversion_data["patterns_matched"],
"success": conversion_data["success"]
}
# Store in analytics database or send to analytics service
self._store_analytics(analytics_data)
def get_popular_patterns(self) -> List[Dict]:
"""Get most commonly used conversion patterns"""
# Query analytics data
pass
```
## ๐ฎ Future Enhancements
### Planned Improvements
1. **AST-based Conversion**: More accurate code transformation
2. **Machine Learning**: Pattern learning from user corrections
3. **IDE Integration**: VS Code extension for live conversion
4. **Batch Processing**: Convert entire project directories
5. **Quality Scoring**: Rate conversion accuracy
6. **Custom Patterns**: User-defined conversion rules
### Extensibility Framework
```python
class ConversionPlugin:
"""Base class for conversion plugins"""
def __init__(self, name: str):
self.name = name
def can_convert(self, code: str, context: Dict) -> bool:
"""Check if plugin can handle this conversion"""
raise NotImplementedError
def convert(self, code: str, context: Dict) -> str:
"""Perform the conversion"""
raise NotImplementedError
class PluginManager:
def __init__(self):
self.plugins = []
def register_plugin(self, plugin: ConversionPlugin):
self.plugins.append(plugin)
def convert_with_plugins(self, code: str, context: Dict) -> str:
for plugin in self.plugins:
if plugin.can_convert(code, context):
return plugin.convert(code, context)
# Fall back to default conversion
return self.default_convert(code, context)
```
---
This implementation guide demonstrates how the Playwright Studies Portal provides a comprehensive, extensible platform for Cypress to Playwright conversion, combining rule-based transformation with AI assistance and user progress tracking.
|