learning_ai_invt_trdg/scripts/tests/run-e2e.sh
Saravana Achu Mac df7c57ea6d 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
2026-05-09 13:46:15 -07:00

304 lines
8.3 KiB
Bash
Executable File

#!/bin/bash
set -e
# Test runner script for E2E tests
# Runs Playwright tests, builds reports, and checks health
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
WEB_DIR="$PROJECT_ROOT/web"
REPORTS_DIR="$SCRIPT_DIR/reports"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
PORT=3050
cd "$WEB_DIR"
echo "========================================="
echo "E2E Test Runner"
echo "========================================="
echo "Project root: $PROJECT_ROOT"
echo "Web directory: $WEB_DIR"
echo "Reports directory: $REPORTS_DIR"
echo "Timestamp: $TIMESTAMP"
echo "Port: $PORT"
echo "========================================="
# Create reports directory
mkdir -p "$REPORTS_DIR"
mkdir -p playwright-report
mkdir -p test-results
# Initialize report files
REPORT_JSON="$REPORTS_DIR/test-report-$TIMESTAMP.json"
REPORT_MD="$REPORTS_DIR/test-report-$TIMESTAMP.md"
SUMMARY_JSON="$REPORTS_DIR/summary-$TIMESTAMP.json"
# Function to kill any process on the specified port
kill_port() {
local port=$1
local pid=$(lsof -ti:$port 2>/dev/null || echo "")
if [ -n "$pid" ]; then
echo "Killing process $pid on port $port..."
kill -9 $pid 2>/dev/null || true
sleep 2
fi
}
# 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
if [ ! -d "node_modules" ]; then
echo "Installing dependencies..."
pnpm install
fi
# Check if Playwright is installed
if ! command -v npx playwright &> /dev/null; then
echo "Playwright not found, installing browsers..."
pnpm exec playwright install chromium
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 "Running E2E tests..."
echo "========================================="
# Run Playwright tests with JSON reporter for machine-readable output
START_TIME=$(date +%s)
pnpm exec playwright test \
--reporter=json \
--reporter=list \
--output="$REPORTS_DIR/results-$TIMESTAMP.json" \
2>&1 | tee "$REPORTS_DIR/test-output-$TIMESTAMP.log"
TEST_EXIT_CODE=$?
END_TIME=$(date +%s)
DURATION=$((END_TIME - START_TIME))
echo ""
echo "========================================="
echo "Test Results"
echo "========================================="
# Parse JSON results if available
if [ -f "$REPORTS_DIR/results-$TIMESTAMP.json" ]; then
# Extract summary from Playwright JSON output
TOTAL_TESTS=$(cat "$REPORTS_DIR/results-$TIMESTAMP.json" | grep -o '"expected":' | wc -l || echo "0")
PASSED_TESTS=$(cat "$REPORTS_DIR/results-$TIMESTAMP.json" | grep -o '"status":"passed"' | wc -l || echo "0")
FAILED_TESTS=$(cat "$REPORTS_DIR/results-$TIMESTAMP.json" | grep -o '"status":"failed"' | wc -l || echo "0")
echo "Total tests: $TOTAL_TESTS"
echo "Passed: $PASSED_TESTS"
echo "Failed: $FAILED_TESTS"
echo "Duration: ${DURATION}s"
else
TOTAL_TESTS=0
PASSED_TESTS=0
FAILED_TESTS=0
echo "Test count: 0 (tests did not run)"
fi
if [ $TEST_EXIT_CODE -eq 0 ]; then
echo "✅ All tests passed"
STATUS="passed"
else
echo "❌ Some tests failed"
STATUS="failed"
fi
echo ""
echo "Report location: $REPORTS_DIR"
echo "========================================="
# Update JSON report with results
cat > "$REPORT_JSON" << EOF
{
"timestamp": "$TIMESTAMP",
"project_root": "$PROJECT_ROOT",
"web_dir": "$WEB_DIR",
"status": "$STATUS",
"exit_code": $TEST_EXIT_CODE,
"tests": {
"total": $TOTAL_TESTS,
"passed": $PASSED_TESTS,
"failed": $FAILED_TESTS,
"skipped": 0,
"duration": $DURATION
},
"report_files": {
"json": "$REPORTS_DIR/results-$TIMESTAMP.json",
"log": "$REPORTS_DIR/test-output-$TIMESTAMP.log",
"server_log": "$REPORTS_DIR/server-$TIMESTAMP.log",
"html": "$WEB_DIR/playwright-report/index.html"
},
"health_check": {
"status": "healthy",
"app_running": true,
"app_url": "http://localhost:$PORT"
},
"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_passed": "All tests passed. Review HTML report for detailed timing and coverage: $WEB_DIR/playwright-report/index.html"
}
}
EOF
# Generate markdown report for human readability
cat > "$REPORT_MD" << EOF
# E2E Test Report
**Timestamp:** $TIMESTAMP
**Status:** $STATUS
**Exit Code:** $TEST_EXIT_CODE
## Test Summary
- **Total Tests:** $TOTAL_TESTS
- **Passed:** $PASSED_TESTS
- **Failed:** $FAILED_TESTS
- **Duration:** ${DURATION}s
## Report Files
- **JSON Report:** \`$REPORTS_DIR/results-$TIMESTAMP.json\`
- **Log Output:** \`$REPORTS_DIR/test-output-$TIMESTAMP.log\`
- **Server Log:** \`$REPORTS_DIR/server-$TIMESTAMP.log\`
- **HTML Report:** \`$WEB_DIR/playwright-report/index.html\`
## Actionable Items
EOF
if [ $TEST_EXIT_CODE -ne 0 ]; then
cat >> "$REPORT_MD" << EOF
### ❌ Tests Failed
Review the log output for specific failure details:
\`\`\`bash
cat $REPORTS_DIR/test-output-$TIMESTAMP.log
\`\`\`
To debug individual tests:
\`\`\`bash
cd $WEB_DIR
pnpm exec playwright test --project=chromium <test-file>
\`\`\`
Common failure reasons:
- Test selectors changed or elements not found
- Network timeouts or API failures
- Browser compatibility issues
EOF
else
cat >> "$REPORT_MD" << EOF
### ✅ All Tests Passed
Review the HTML report for detailed timing and coverage:
\`\`\`bash
open $WEB_DIR/playwright-report/index.html
\`\`\`
EOF
fi
# Cleanup: Kill the server
echo ""
echo "Cleaning up..."
echo "========================================="
echo "Stopping server (PID: $SERVER_PID)..."
kill $SERVER_PID 2>/dev/null || true
kill_port $PORT
echo "✅ Server stopped"
# Update health check in JSON
cat > "$SUMMARY_JSON" << EOF
{
"timestamp": "$TIMESTAMP",
"test_status": "$STATUS",
"test_exit_code": $TEST_EXIT_CODE,
"tests_total": $TOTAL_TESTS,
"tests_passed": $PASSED_TESTS,
"tests_failed": $FAILED_TESTS,
"duration_seconds": $DURATION,
"health_check": {
"status": "healthy",
"app_running": true,
"app_url": "http://localhost:$PORT"
},
"report_dir": "$REPORTS_DIR",
"latest_report": "$REPORT_JSON",
"latest_markdown": "$REPORT_MD",
"for_ai_agents": {
"quick_summary": "$STATUS - $PASSED_TESTS/$TOTAL_TESTS tests passed in ${DURATION}s",
"if_failed_check": "$REPORTS_DIR/test-output-$TIMESTAMP.log",
"html_report": "$WEB_DIR/playwright-report/index.html",
"next_action": $([ $TEST_EXIT_CODE -ne 0 ] && echo "\"Review failures in log and fix broken selectors or API issues\"" || echo "\"Review HTML report for performance optimization\"")
}
}
EOF
echo ""
echo "========================================="
echo "Summary"
echo "========================================="
echo "Tests run: Complete"
echo "Status: $STATUS"
echo "JSON Report: $REPORT_JSON"
echo "Markdown Report: $REPORT_MD"
echo "AI Summary: $SUMMARY_JSON"
echo "HTML Report: $WEB_DIR/playwright-report/index.html"
echo "Health check: healthy"
echo "========================================="
# Create symlink to latest report
ln -sf "$REPORT_JSON" "$REPORTS_DIR/latest.json"
ln -sf "$REPORT_MD" "$REPORTS_DIR/latest.md"
ln -sf "$SUMMARY_JSON" "$REPORTS_DIR/latest-summary.json"
echo "Latest reports linked to: $REPORTS_DIR/latest.{json,md,summary.json}"
echo "========================================="
exit $TEST_EXIT_CODE