Skip to content

Playwright Testing with Localhost

The firewall makes it easy to test local web applications with Playwright using the localhost keyword.

Test a local development server with Playwright:

Terminal window
# Start your dev server (e.g., npm run dev on port 3000)
# Run Playwright tests through the firewall
sudo awf --allow-domains localhost,playwright.dev -- npx playwright test

The localhost keyword automatically configures everything needed for local testing - no manual setup required.

When you include localhost in --allow-domains, awf automatically:

  1. Enables host access - Activates --enable-host-access flag
  2. Maps to host.docker.internal - Replaces localhost with Docker’s host gateway
  3. Allows development ports - Opens common dev ports: 3000, 3001, 4000, 4200, 5000, 5173, 8000, 8080, 8081, 8888, 9000, 9090

This means your Playwright tests inside the container can reach services running on your host machine’s localhost.

The localhost keyword preserves HTTP/HTTPS protocol prefixes:

Terminal window
# HTTP only
sudo awf --allow-domains http://localhost -- npx playwright test
# HTTPS only
sudo awf --allow-domains https://localhost -- npx playwright test
# Both HTTP and HTTPS (default)
sudo awf --allow-domains localhost -- npx playwright test

Override the default port list with --allow-host-ports:

Terminal window
# Allow only specific ports
sudo awf \
--allow-domains localhost \
--allow-host-ports 3000,8080 \
-- npx playwright test
# Allow a port range
sudo awf \
--allow-domains localhost \
--allow-host-ports 3000,8080-8090 \
-- npx playwright test
Terminal window
# Terminal 1: Start Next.js dev server
npm run dev
# Server runs on http://localhost:3000
# Terminal 2: Run Playwright tests through firewall
sudo awf \
--allow-domains localhost,vercel.app,next.js.org \
-- npx playwright test

Your Playwright tests can now access http://localhost:3000 and also fetch from vercel.app and next.js.org.

Example: Testing a React App with External APIs

Section titled “Example: Testing a React App with External APIs”
Terminal window
# Start React dev server on port 3000
npm start
# Run tests with access to localhost and external APIs
sudo awf \
--allow-domains localhost,api.github.com,cdn.example.com \
-- npx playwright test

Before the localhost keyword, you had to manually configure host access:

Terminal window
# Old way (still works)
sudo awf \
--enable-host-access \
--allow-domains host.docker.internal \
--allow-host-ports 3000,8080 \
-- npx playwright test

The localhost keyword eliminates this boilerplate.

When localhost is specified:

  • Containers can access ANY service on the specified ports on your host machine
  • This includes local databases, development servers, and other services
  • This is safe for local development but should not be used in production

If Playwright can’t connect to your local server:

  1. Verify server is running: Check that your dev server is actually running on the host
  2. Check the port: Ensure the port is in the allowed list (default: 3000, 3001, 4000, 4200, 5000, 5173, 8000, 8080, 8081, 8888, 9000, 9090)
  3. Use custom ports: If using a different port, specify it with --allow-host-ports

If you see DNS resolution errors:

  • Use localhost (not 127.0.0.1 or your machine’s hostname)
  • The localhost keyword maps to host.docker.internal which resolves to the host

Some dev servers bind only to 127.0.0.1. To make them accessible from Docker containers:

Terminal window
# Bind to 0.0.0.0 instead of 127.0.0.1
npm run dev -- --host 0.0.0.0
# Or for Vite/Vue
npm run dev -- --host
# Or for Next.js
npm run dev -- -H 0.0.0.0