Download docs/IMPLEMENTATION_GUIDE.md from fartec0/playwright-to-cypress-conversion-tool: direct link, hf CLI and curl.
- Browser
- Download file 16.9 kB
-
https://huggingface.co/spaces/fartec0/playwright-to-cypress-conversion-tool/resolve/main/docs/IMPLEMENTATION_GUIDE.md
- Command line
-
hf download hf://spaces/fartec0/playwright-to-cypress-conversion-tool/docs/IMPLEMENTATION_GUIDE.md
-
curl -L -o IMPLEMENTATION_GUIDE.md https://huggingface.co/spaces/fartec0/playwright-to-cypress-conversion-tool/resolve/main/docs/IMPLEMENTATION_GUIDE.md
A newer version of the Gradio SDK is available: 6.32.0
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:
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:
- Basic Syntax: Core command translations
- Assertions: Expectation and validation patterns
- Commands: Complex operations and utilities
Pattern Matching Algorithm
# 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:
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
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
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
- Real-time Conversion: Live code transformation
- Syntax Highlighting: JavaScript/TypeScript support
- Explanation Generation: Context-aware explanations
- Multiple Conversion Types: Categorized conversion patterns
๐ค AI-Powered Enhancement
AI Service Integration
Located in services/ai_service.py:
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
- Contextual Help: Smart assistance based on user queries
- Code Analysis: Understanding complex conversion scenarios
- Best Practice Suggestions: AI-driven recommendations
- Educational Explanations: Detailed learning content
๐ Progress Tracking System
Database Models
# 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
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:
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
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
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
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
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
- AST-based Conversion: More accurate code transformation
- Machine Learning: Pattern learning from user corrections
- IDE Integration: VS Code extension for live conversion
- Batch Processing: Convert entire project directories
- Quality Scoring: Rate conversion accuracy
- Custom Patterns: User-defined conversion rules
Extensibility Framework
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.