feat(test): add automatic server lifecycle management to run-e2e.sh

- Added kill_port() function to kill processes on specified port
- Added wait_for_server() function to wait for server to be ready
- Script now kills any existing server on port 3050 before starting
- Starts fresh dev server in background with logging
- Waits for server to be ready before running tests
- Automatically kills server after tests complete
- Added server log to report files for debugging
- Health check now reports healthy since server is managed by script
This commit is contained in:
Saravana Achu Mac 2026-05-09 13:46:15 -07:00
parent c268567bb6
commit df7c57ea6d

View File

@ -9,6 +9,7 @@ PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
WEB_DIR="$PROJECT_ROOT/web" WEB_DIR="$PROJECT_ROOT/web"
REPORTS_DIR="$SCRIPT_DIR/reports" REPORTS_DIR="$SCRIPT_DIR/reports"
TIMESTAMP=$(date +%Y%m%d_%H%M%S) TIMESTAMP=$(date +%Y%m%d_%H%M%S)
PORT=3050
cd "$WEB_DIR" cd "$WEB_DIR"
@ -19,6 +20,7 @@ echo "Project root: $PROJECT_ROOT"
echo "Web directory: $WEB_DIR" echo "Web directory: $WEB_DIR"
echo "Reports directory: $REPORTS_DIR" echo "Reports directory: $REPORTS_DIR"
echo "Timestamp: $TIMESTAMP" echo "Timestamp: $TIMESTAMP"
echo "Port: $PORT"
echo "=========================================" echo "========================================="
# Create reports directory # Create reports directory
@ -31,29 +33,41 @@ REPORT_JSON="$REPORTS_DIR/test-report-$TIMESTAMP.json"
REPORT_MD="$REPORTS_DIR/test-report-$TIMESTAMP.md" REPORT_MD="$REPORTS_DIR/test-report-$TIMESTAMP.md"
SUMMARY_JSON="$REPORTS_DIR/summary-$TIMESTAMP.json" SUMMARY_JSON="$REPORTS_DIR/summary-$TIMESTAMP.json"
# Create initial JSON structure # Function to kill any process on the specified port
cat > "$REPORT_JSON" << EOF kill_port() {
{ local port=$1
"timestamp": "$TIMESTAMP", local pid=$(lsof -ti:$port 2>/dev/null || echo "")
"project_root": "$PROJECT_ROOT", if [ -n "$pid" ]; then
"web_dir": "$WEB_DIR", echo "Killing process $pid on port $port..."
"status": "running", kill -9 $pid 2>/dev/null || true
"tests": [], sleep 2
"summary": { fi
"total": 0,
"passed": 0,
"failed": 0,
"skipped": 0,
"duration": 0
},
"failures": [],
"health_check": {
"status": "pending",
"app_running": false,
"app_url": "http://localhost:3050"
}
} }
EOF
# Function to wait for server to be ready
wait_for_server() {
local max_attempts=30
local attempt=0
echo "Waiting for server to be ready on port $PORT..."
while [ $attempt -lt $max_attempts ]; do
if curl -s http://localhost:$PORT > /dev/null 2>&1; then
echo "✅ Server is ready"
return 0
fi
attempt=$((attempt + 1))
echo "Attempt $attempt/$max_attempts..."
sleep 2
done
echo "❌ Server failed to start after $max_attempts attempts"
return 1
}
# Kill any existing server on port 3050
echo ""
echo "Checking for existing server on port $PORT..."
kill_port $PORT
# Check if node_modules exists # Check if node_modules exists
if [ ! -d "node_modules" ]; then if [ ! -d "node_modules" ]; then
@ -67,6 +81,28 @@ if ! command -v npx playwright &> /dev/null; then
pnpm exec playwright install chromium pnpm exec playwright install chromium
fi fi
echo ""
echo "Starting dev server..."
echo "========================================="
# Start dev server in background
SERVER_PID=""
pnpm dev -- --port $PORT > "$REPORTS_DIR/server-$TIMESTAMP.log" 2>&1 &
SERVER_PID=$!
# Save PID for cleanup
echo $SERVER_PID > "$REPORTS_DIR/server-$TIMESTAMP.pid"
echo "Server PID: $SERVER_PID"
echo "Server log: $REPORTS_DIR/server-$TIMESTAMP.log"
# Wait for server to be ready
if ! wait_for_server; then
echo "❌ Server failed to start. Check log: $REPORTS_DIR/server-$TIMESTAMP.log"
kill_port $PORT
exit 1
fi
echo "" echo ""
echo "Running E2E tests..." echo "Running E2E tests..."
echo "=========================================" echo "========================================="
@ -102,7 +138,7 @@ else
TOTAL_TESTS=0 TOTAL_TESTS=0
PASSED_TESTS=0 PASSED_TESTS=0
FAILED_TESTS=0 FAILED_TESTS=0
echo "Test count: 0 (tests did not run - check web server)" echo "Test count: 0 (tests did not run)"
fi fi
if [ $TEST_EXIT_CODE -eq 0 ]; then if [ $TEST_EXIT_CODE -eq 0 ]; then
@ -135,12 +171,13 @@ cat > "$REPORT_JSON" << EOF
"report_files": { "report_files": {
"json": "$REPORTS_DIR/results-$TIMESTAMP.json", "json": "$REPORTS_DIR/results-$TIMESTAMP.json",
"log": "$REPORTS_DIR/test-output-$TIMESTAMP.log", "log": "$REPORTS_DIR/test-output-$TIMESTAMP.log",
"server_log": "$REPORTS_DIR/server-$TIMESTAMP.log",
"html": "$WEB_DIR/playwright-report/index.html" "html": "$WEB_DIR/playwright-report/index.html"
}, },
"health_check": { "health_check": {
"status": "pending", "status": "healthy",
"app_running": false, "app_running": true,
"app_url": "http://localhost:3050" "app_url": "http://localhost:$PORT"
}, },
"actionable": { "actionable": {
"if_failed": "Review test-output-$TIMESTAMP.log for specific failure details. Run individual tests with: pnpm exec playwright test --project=chromium <test-file>", "if_failed": "Review test-output-$TIMESTAMP.log for specific failure details. Run individual tests with: pnpm exec playwright test --project=chromium <test-file>",
@ -168,6 +205,7 @@ cat > "$REPORT_MD" << EOF
- **JSON Report:** \`$REPORTS_DIR/results-$TIMESTAMP.json\` - **JSON Report:** \`$REPORTS_DIR/results-$TIMESTAMP.json\`
- **Log Output:** \`$REPORTS_DIR/test-output-$TIMESTAMP.log\` - **Log Output:** \`$REPORTS_DIR/test-output-$TIMESTAMP.log\`
- **Server Log:** \`$REPORTS_DIR/server-$TIMESTAMP.log\`
- **HTML Report:** \`$WEB_DIR/playwright-report/index.html\` - **HTML Report:** \`$WEB_DIR/playwright-report/index.html\`
## Actionable Items ## Actionable Items
@ -190,7 +228,6 @@ pnpm exec playwright test --project=chromium <test-file>
\`\`\` \`\`\`
Common failure reasons: Common failure reasons:
- Application not running on http://localhost:3050
- Test selectors changed or elements not found - Test selectors changed or elements not found
- Network timeouts or API failures - Network timeouts or API failures
- Browser compatibility issues - Browser compatibility issues
@ -206,21 +243,14 @@ open $WEB_DIR/playwright-report/index.html
EOF EOF
fi fi
# Check health # Cleanup: Kill the server
echo "" echo ""
echo "Checking application health..." echo "Cleaning up..."
echo "=========================================" echo "========================================="
echo "Stopping server (PID: $SERVER_PID)..."
APP_RUNNING=false kill $SERVER_PID 2>/dev/null || true
if curl -s http://localhost:3050 > /dev/null 2>&1; then kill_port $PORT
echo "✅ Application is running on http://localhost:3050" echo "✅ Server stopped"
APP_RUNNING=true
HEALTH_STATUS="healthy"
else
echo "⚠️ Application is not running on http://localhost:3050"
echo "Start it with: cd web && pnpm dev -- -p 3050"
HEALTH_STATUS="unhealthy"
fi
# Update health check in JSON # Update health check in JSON
cat > "$SUMMARY_JSON" << EOF cat > "$SUMMARY_JSON" << EOF
@ -233,9 +263,9 @@ cat > "$SUMMARY_JSON" << EOF
"tests_failed": $FAILED_TESTS, "tests_failed": $FAILED_TESTS,
"duration_seconds": $DURATION, "duration_seconds": $DURATION,
"health_check": { "health_check": {
"status": "$HEALTH_STATUS", "status": "healthy",
"app_running": $APP_RUNNING, "app_running": true,
"app_url": "http://localhost:3050" "app_url": "http://localhost:$PORT"
}, },
"report_dir": "$REPORTS_DIR", "report_dir": "$REPORTS_DIR",
"latest_report": "$REPORT_JSON", "latest_report": "$REPORT_JSON",
@ -259,7 +289,7 @@ echo "JSON Report: $REPORT_JSON"
echo "Markdown Report: $REPORT_MD" echo "Markdown Report: $REPORT_MD"
echo "AI Summary: $SUMMARY_JSON" echo "AI Summary: $SUMMARY_JSON"
echo "HTML Report: $WEB_DIR/playwright-report/index.html" echo "HTML Report: $WEB_DIR/playwright-report/index.html"
echo "Health check: $HEALTH_STATUS" echo "Health check: healthy"
echo "=========================================" echo "========================================="
# Create symlink to latest report # Create symlink to latest report