Skip to content

Commit 50cb89e

Browse files
mvanhornclaude
andcommitted
fix(test-browser): detect dev server port from project config
Replace all hardcoded localhost:3000 references with dynamic port detection. The command now checks (in priority order): explicit --port argument, CLAUDE.md config, package.json scripts, .env files, then falls back to 3000. Closes #164 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 69f2a96 commit 50cb89e

1 file changed

Lines changed: 67 additions & 13 deletions

File tree

plugins/compound-engineering/commands/test-browser.md

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
name: test-browser
33
description: Run browser tests on pages affected by current PR or branch
4-
argument-hint: "[PR number, branch name, or 'current' for current branch]"
4+
argument-hint: "[PR number, branch name, 'current', or --port PORT]"
55
---
66

77
# Browser Test Command
@@ -122,45 +122,96 @@ Build a list of URLs to test based on the mapping.
122122

123123
</file_to_route_mapping>
124124

125-
### 4. Verify Server is Running
125+
### 4. Detect Dev Server Port
126+
127+
<detect_port>
128+
129+
Determine the dev server port using this priority order:
130+
131+
**Priority 1: Explicit argument**
132+
If the user passed a port number (e.g., `/test-browser 5000` or `/test-browser --port 5000`), use that port directly.
133+
134+
**Priority 2: CLAUDE.md / project instructions**
135+
```bash
136+
# Check CLAUDE.md for port references
137+
grep -Eio '(port\s*[:=]\s*|localhost:)([0-9]{4,5})' CLAUDE.md 2>/dev/null | grep -Eo '[0-9]{4,5}' | head -1
138+
```
139+
140+
**Priority 3: package.json scripts**
141+
```bash
142+
# Check dev/start scripts for --port flags
143+
grep -Eo '\-\-port[= ]+[0-9]{4,5}' package.json 2>/dev/null | grep -Eo '[0-9]{4,5}' | head -1
144+
```
145+
146+
**Priority 4: Environment files**
147+
```bash
148+
# Check .env, .env.local, .env.development for PORT=
149+
grep -h '^PORT=' .env .env.local .env.development 2>/dev/null | tail -1 | cut -d= -f2
150+
```
151+
152+
**Priority 5: Default fallback**
153+
If none of the above yields a port, default to `3000`.
154+
155+
Store the result in a `PORT` variable for use in all subsequent steps.
156+
157+
```bash
158+
# Combined detection (run this)
159+
PORT="${EXPLICIT_PORT:-}"
160+
if [ -z "$PORT" ]; then
161+
PORT=$(grep -Eio '(port\s*[:=]\s*|localhost:)([0-9]{4,5})' CLAUDE.md 2>/dev/null | grep -Eo '[0-9]{4,5}' | head -1)
162+
fi
163+
if [ -z "$PORT" ]; then
164+
PORT=$(grep -Eo '\-\-port[= ]+[0-9]{4,5}' package.json 2>/dev/null | grep -Eo '[0-9]{4,5}' | head -1)
165+
fi
166+
if [ -z "$PORT" ]; then
167+
PORT=$(grep -h '^PORT=' .env .env.local .env.development 2>/dev/null | tail -1 | cut -d= -f2)
168+
fi
169+
PORT="${PORT:-3000}"
170+
echo "Using dev server port: $PORT"
171+
```
172+
173+
</detect_port>
174+
175+
### 5. Verify Server is Running
126176

127177
<check_server>
128178

129-
Before testing, verify the local server is accessible:
179+
Before testing, verify the local server is accessible using the detected port:
130180

131181
```bash
132-
agent-browser open http://localhost:3000
182+
agent-browser open http://localhost:${PORT}
133183
agent-browser snapshot -i
134184
```
135185

136186
If server is not running, inform user:
137187
```markdown
138-
**Server not running**
188+
**Server not running on port ${PORT}**
139189

140190
Please start your development server:
141191
- Rails: `bin/dev` or `rails server`
142192
- Node/Next.js: `npm run dev`
193+
- Custom port: `/test-browser --port <your-port>`
143194

144195
Then run `/test-browser` again.
145196
```
146197

147198
</check_server>
148199

149-
### 5. Test Each Affected Page
200+
### 6. Test Each Affected Page
150201

151202
<test_pages>
152203

153204
For each affected route, use agent-browser CLI commands (NOT Chrome MCP):
154205

155206
**Step 1: Navigate and capture snapshot**
156207
```bash
157-
agent-browser open "http://localhost:3000/[route]"
208+
agent-browser open "http://localhost:${PORT}/[route]"
158209
agent-browser snapshot -i
159210
```
160211

161212
**Step 2: For headed mode (visual debugging)**
162213
```bash
163-
agent-browser --headed open "http://localhost:3000/[route]"
214+
agent-browser --headed open "http://localhost:${PORT}/[route]"
164215
agent-browser --headed snapshot -i
165216
```
166217

@@ -185,7 +236,7 @@ agent-browser screenshot --full page-name-full.png # Full page
185236

186237
</test_pages>
187238

188-
### 6. Human Verification (When Required)
239+
### 7. Human Verification (When Required)
189240

190241
<human_verification>
191242

@@ -214,7 +265,7 @@ Did it work correctly?
214265

215266
</human_verification>
216267

217-
### 7. Handle Failures
268+
### 8. Handle Failures
218269

219270
<failure_handling>
220271

@@ -253,7 +304,7 @@ When a test fails:
253304

254305
</failure_handling>
255306

256-
### 8. Test Summary
307+
### 9. Test Summary
257308

258309
<test_summary>
259310

@@ -263,7 +314,7 @@ After all tests complete, present summary:
263314
## Browser Test Results
264315

265316
**Test Scope:** PR #[number] / [branch name]
266-
**Server:** http://localhost:3000
317+
**Server:** http://localhost:${PORT}
267318

268319
### Pages Tested: [count]
269320

@@ -295,14 +346,17 @@ After all tests complete, present summary:
295346
## Quick Usage Examples
296347

297348
```bash
298-
# Test current branch changes
349+
# Test current branch changes (auto-detects port)
299350
/test-browser
300351

301352
# Test specific PR
302353
/test-browser 847
303354

304355
# Test specific branch
305356
/test-browser feature/new-dashboard
357+
358+
# Test on a specific port
359+
/test-browser --port 5000
306360
```
307361

308362
## agent-browser CLI Reference

0 commit comments

Comments
 (0)