Featured image of post Running Claude Code in a VM for Automated Testing and Development

Running Claude Code in a VM for Automated Testing and Development

语速

Recently, I verified an effective AI development method that doesn’t affect the existing workflow. Here’s a summary.

Why Choose VM + Claude Code

  • Isolation: Avoid polluting the main system, can snapshot and rollback
  • Reproducibility: Team members can quickly replicate the same environment
  • Suitable for automated testing: Browser automation tools like Playwright require a desktop environment
  • Safety: Not too worried about agent generating rm -rf / commands. VM crashes don’t affect the virtualization platform; just recreate it.

Environment Setup

1. PVE Creates Ubuntu Desktop VM

  1. Download Ubuntu Desktop ISO, upload to PVE’s ISO storage
  2. Create VM:
    • CPU: host type, 2-4 cores
    • RAM: 4-8 GB
    • Disk: VirtIO SCSI, 40GB+
    • Network: VirtIO
  3. Mount ISO and start installation
  4. After installation, install qemu-guest-agent:
1
2
sudo apt install qemu-guest-agent
sudo systemctl enable --now qemu-guest-agent

2. Configure Xfce + xrdp Remote Desktop

Install xrdp and Xfce (lighter and more compatible than GNOME):

1
2
3
4
5
6
sudo apt install xrdp
sudo systemctl enable xrdp
sudo systemctl start xrdp

sudo apt install xfce4 xfce4-goodies
echo xfce4-session > ~/.xsession

During installation, when prompted to choose a display manager, select lightdm.

Solve Black Screen Issue

Edit xrdp startup script:

1
sudo nano /etc/xrdp/startwm.sh

Before the last two lines test -x and exec, add:

1
2
3
unset DBUS_SESSION_BUS_ADDRESS
unset XDG_RUNTIME_DIR
startxfce4

Restart xrdp:

1
sudo systemctl restart xrdp

Note: Don’t log in to the desktop locally before connecting, otherwise the same user will see a black screen.

Adjust Resolution/DPI

  • Before Windows remote desktop connection, lower the resolution in “Display Options” (e.g., 1920×1080)
  • Or in Xfce: Settings Manager → Appearance → Fonts → Increase DPI (e.g., 120)

Disable Crash Prompts

After switching desktops, there may be GNOME component crash prompts (doesn’t affect usage):

1
sudo systemctl disable apport

3. Install Claude Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Install Node.js (e.g., using nvm)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
source ~/.bashrc
nvm install --lts

# Install Claude Code
npm install -g @anthropic-ai/claude-code

# Or use official installation script
curl -fsSL https://claude.ai/install.sh | bash

Run claude command for the first time and follow prompts to log in and authenticate.

Automated Testing Workflow: MCP Configuration

Playwright MCP

MCPFeatures
@playwright/mcp (Microsoft official)Lightweight, based on accessibility tree
@executeautomation/playwright-mcp-server (community)More complete features, supports screenshots, JS execution
@agentdeskai/browser-tools-mcpConsole log monitoring, Lighthouse performance analysis

Claude Code Configuration

Create .mcp.json in project root:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
{
  "mcpServers": {
    "playwright": {
      "command": "npx",
      "args": ["-y", "@executeautomation/playwright-mcp-server"]
    },
    "browser-tools": {
      "command": "npx",
      "args": ["-y", "@agentdeskai/browser-tools-mcp"]
    }
  }
}

Or add via CLI:

1
claude mcp add playwright --scope project -- npx -y @executeautomation/playwright-mcp-server

OpenCode Configuration

If using OpenCode, the configuration format is different (opencode.json):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
{
  "$schema": "https://opencode.ai/config.json",
  "mcp": {
    "playwright": {
      "type": "local",
      "command": ["npx", "-y", "@executeautomation/playwright-mcp-server"],
      "enabled": true
    },
    "browser-tools": {
      "type": "local",
      "command": ["npx", "-y", "@agentdeskai/browser-tools-mcp"],
      "enabled": true
    }
  }
}

Configuration Comparison:

Configuration ItemClaude CodeOpenCode
Top-level keymcpServersmcp
typeNot neededRequired (local)
commandStringArray

Usage Example

After configuration, you can drive tests with natural language:

1
2
3
4
5
claude "Open localhost:3000, test the login flow, verify if it redirects to homepage"

claude "Screenshot and compare homepage layout under mobile/tablet/desktop sizes"

claude "Check if page console has any errors"

Results Display

VM remote desktop running Claude Code

Summary

The VM + Claude Code + Playwright MCP combination provides an isolated, reproducible automated development testing environment. The entire process:

  1. PVE creates Ubuntu Desktop VM
  2. Configure Xfce + xrdp remote access
  3. Install Claude Code / OpenCode
  4. Configure Playwright MCP
  5. Drive automated testing with natural language