Skip to content

Commit c660da2

Browse files
etewiahclaude
andcommitted
Fix server detection in E2E test runner
The check_server function was incorrectly detecting the server as running due to curl output handling. Changed to return "up"/"down" status and use proper exit code checking with connect timeout. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 3978f03 commit c660da2

File tree

1 file changed

+246
-0
lines changed

1 file changed

+246
-0
lines changed

scripts/run-e2e-tests.sh

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
#!/bin/bash
2+
#
3+
# E2E Test Runner for PropertyWebBuilder
4+
#
5+
# This script sets up the e2e environment and runs Playwright tests.
6+
# It handles database setup, server startup, and test execution.
7+
#
8+
# Usage:
9+
# ./scripts/run-e2e-tests.sh [playwright-args]
10+
#
11+
# Examples:
12+
# ./scripts/run-e2e-tests.sh # Run all tests
13+
# ./scripts/run-e2e-tests.sh --ui # Run with Playwright UI
14+
# ./scripts/run-e2e-tests.sh tests/e2e/public/ # Run specific tests
15+
# ./scripts/run-e2e-tests.sh --headed # Run with browser visible
16+
#
17+
# Options:
18+
# --reset Force reset the e2e database before running tests
19+
# --no-server Don't start the server (assume it's already running)
20+
# --bypass-auth Start server with admin auth bypass enabled
21+
#
22+
23+
set -e
24+
25+
# Colors for output
26+
RED='\033[0;31m'
27+
GREEN='\033[0;32m'
28+
YELLOW='\033[1;33m'
29+
BLUE='\033[0;34m'
30+
NC='\033[0m' # No Color
31+
32+
# Configuration
33+
E2E_PORT=3001
34+
E2E_HOST="tenant-a.e2e.localhost"
35+
E2E_URL="http://${E2E_HOST}:${E2E_PORT}"
36+
SERVER_PID_FILE="/tmp/pwb-e2e-server.pid"
37+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
38+
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
39+
40+
# Parse our custom arguments
41+
FORCE_RESET=false
42+
NO_SERVER=false
43+
BYPASS_AUTH=false
44+
PLAYWRIGHT_ARGS=()
45+
46+
for arg in "$@"; do
47+
case $arg in
48+
--reset)
49+
FORCE_RESET=true
50+
;;
51+
--no-server)
52+
NO_SERVER=true
53+
;;
54+
--bypass-auth)
55+
BYPASS_AUTH=true
56+
;;
57+
*)
58+
PLAYWRIGHT_ARGS+=("$arg")
59+
;;
60+
esac
61+
done
62+
63+
cd "$PROJECT_ROOT"
64+
65+
echo -e "${BLUE}======================================${NC}"
66+
echo -e "${BLUE}PropertyWebBuilder E2E Test Runner${NC}"
67+
echo -e "${BLUE}======================================${NC}"
68+
echo ""
69+
70+
# Function to check if server is running (returns "up" or "down")
71+
check_server() {
72+
local status
73+
status=$(curl -s -o /dev/null -w "%{http_code}" --connect-timeout 2 "$E2E_URL" 2>/dev/null)
74+
if [ $? -eq 0 ] && [ "$status" != "000" ] && [ -n "$status" ]; then
75+
echo "up"
76+
else
77+
echo "down"
78+
fi
79+
}
80+
81+
# Function to wait for server to be ready
82+
wait_for_server() {
83+
echo -e "${YELLOW}Waiting for server to be ready...${NC}"
84+
local max_attempts=60
85+
local attempt=1
86+
87+
while [ $attempt -le $max_attempts ]; do
88+
local status=$(check_server)
89+
if [ "$status" = "up" ]; then
90+
echo -e "${GREEN}Server is ready!${NC}"
91+
return 0
92+
fi
93+
echo -n "."
94+
sleep 1
95+
attempt=$((attempt + 1))
96+
done
97+
98+
echo ""
99+
echo -e "${RED}Server failed to start within ${max_attempts} seconds${NC}"
100+
return 1
101+
}
102+
103+
# Function to stop server
104+
stop_server() {
105+
if [ -f "$SERVER_PID_FILE" ]; then
106+
local pid=$(cat "$SERVER_PID_FILE")
107+
if kill -0 "$pid" 2>/dev/null; then
108+
echo -e "${YELLOW}Stopping e2e server (PID: $pid)...${NC}"
109+
kill "$pid" 2>/dev/null || true
110+
sleep 2
111+
# Force kill if still running
112+
if kill -0 "$pid" 2>/dev/null; then
113+
kill -9 "$pid" 2>/dev/null || true
114+
fi
115+
fi
116+
rm -f "$SERVER_PID_FILE"
117+
fi
118+
}
119+
120+
# Function to start server
121+
start_server() {
122+
echo -e "${YELLOW}Starting e2e server on port ${E2E_PORT}...${NC}"
123+
124+
# Stop any existing server
125+
stop_server
126+
127+
# Also kill any process on the port
128+
lsof -ti:${E2E_PORT} | xargs kill -9 2>/dev/null || true
129+
130+
# Start the server in background
131+
if [ "$BYPASS_AUTH" = true ]; then
132+
echo -e "${YELLOW}Starting with BYPASS_ADMIN_AUTH=true${NC}"
133+
RAILS_ENV=e2e BYPASS_ADMIN_AUTH=true bundle exec rails server -p ${E2E_PORT} -d -P "$SERVER_PID_FILE" 2>/dev/null
134+
else
135+
RAILS_ENV=e2e bundle exec rails server -p ${E2E_PORT} -d -P "$SERVER_PID_FILE" 2>/dev/null
136+
fi
137+
138+
# Wait a moment for the PID file to be created
139+
sleep 2
140+
141+
if [ -f "$SERVER_PID_FILE" ]; then
142+
echo -e "${GREEN}Server started (PID: $(cat $SERVER_PID_FILE))${NC}"
143+
fi
144+
}
145+
146+
# Function to check e2e database
147+
check_database() {
148+
echo -e "${YELLOW}Checking e2e database...${NC}"
149+
150+
local result=$(RAILS_ENV=e2e bundle exec rails runner "puts Pwb::Website.where(subdomain: 'tenant-a').exists?" 2>/dev/null || echo "error")
151+
152+
if [ "$result" = "true" ]; then
153+
echo -e "${GREEN}E2E database is ready${NC}"
154+
return 0
155+
else
156+
echo -e "${YELLOW}E2E database needs setup${NC}"
157+
return 1
158+
fi
159+
}
160+
161+
# Function to setup database
162+
setup_database() {
163+
echo -e "${YELLOW}Setting up e2e database...${NC}"
164+
echo -e "${YELLOW}Running: RAILS_ENV=e2e bin/rails playwright:reset${NC}"
165+
166+
RAILS_ENV=e2e bundle exec rails playwright:reset
167+
168+
if [ $? -eq 0 ]; then
169+
echo -e "${GREEN}Database setup complete${NC}"
170+
else
171+
echo -e "${RED}Database setup failed${NC}"
172+
exit 1
173+
fi
174+
}
175+
176+
# Cleanup function
177+
cleanup() {
178+
echo ""
179+
echo -e "${YELLOW}Cleaning up...${NC}"
180+
stop_server
181+
}
182+
183+
# Set trap to cleanup on exit
184+
trap cleanup EXIT
185+
186+
# Main execution
187+
echo -e "${BLUE}Step 1: Database Setup${NC}"
188+
echo "------------------------"
189+
190+
if [ "$FORCE_RESET" = true ]; then
191+
echo -e "${YELLOW}Force reset requested${NC}"
192+
setup_database
193+
elif ! check_database; then
194+
setup_database
195+
fi
196+
197+
echo ""
198+
echo -e "${BLUE}Step 2: Server Setup${NC}"
199+
echo "------------------------"
200+
201+
if [ "$NO_SERVER" = true ]; then
202+
echo -e "${YELLOW}Skipping server start (--no-server)${NC}"
203+
echo -e "${YELLOW}Assuming server is already running at ${E2E_URL}${NC}"
204+
205+
# Verify server is running
206+
if [ "$(check_server)" = "down" ]; then
207+
echo -e "${RED}Error: Server is not running at ${E2E_URL}${NC}"
208+
echo -e "${YELLOW}Start the server manually:${NC}"
209+
echo " RAILS_ENV=e2e bin/rails playwright:server"
210+
exit 1
211+
fi
212+
else
213+
# Check if server is already running
214+
if [ "$(check_server)" = "up" ]; then
215+
echo -e "${YELLOW}Server already running at ${E2E_URL}${NC}"
216+
else
217+
start_server
218+
wait_for_server || exit 1
219+
fi
220+
fi
221+
222+
echo ""
223+
echo -e "${BLUE}Step 3: Running Playwright Tests${NC}"
224+
echo "------------------------"
225+
226+
echo -e "${YELLOW}Running: npx playwright test ${PLAYWRIGHT_ARGS[*]}${NC}"
227+
echo ""
228+
229+
# Run playwright tests
230+
npx playwright test "${PLAYWRIGHT_ARGS[@]}"
231+
TEST_EXIT_CODE=$?
232+
233+
echo ""
234+
if [ $TEST_EXIT_CODE -eq 0 ]; then
235+
echo -e "${GREEN}======================================${NC}"
236+
echo -e "${GREEN}All tests passed!${NC}"
237+
echo -e "${GREEN}======================================${NC}"
238+
else
239+
echo -e "${RED}======================================${NC}"
240+
echo -e "${RED}Some tests failed (exit code: $TEST_EXIT_CODE)${NC}"
241+
echo -e "${RED}======================================${NC}"
242+
echo ""
243+
echo -e "${YELLOW}View the report: npx playwright show-report${NC}"
244+
fi
245+
246+
exit $TEST_EXIT_CODE

0 commit comments

Comments
 (0)