home / skills / openharmonyinsight / openharmony-skills / arkweb-app-debug

arkweb-app-debug skill

/skills/arkweb-app-debug

This skill helps you debug HarmonyOS ArkWeb apps by automating DevTools setup, port forwarding, and webview inspection.

npx playbooks add skill openharmonyinsight/openharmony-skills --skill arkweb-app-debug

Review the files below or copy the command above to add this skill to your agents.

Files (6)
SKILL.md
6.8 KB
---
name: arkweb-app-debug
description: "Debug HarmonyOS ArkWeb applications using Chrome DevTools Protocol. Use when Claude needs to: (1) Start ArkWeb debugging sessions, (2) Connect Chrome DevTools to HarmonyOS apps via hdc, (3) Inspect webview elements, console, and network, (4) Test ArkWeb applications with Chrome DevTools MCP, (5) Troubleshoot webview debugging issues, or (6) Manage port forwarding and debugging sessions. Automatically detects project configuration, device connection, and debugging sockets. Works with ohos-app-build-debug skill for complete workflow."
allowed-tools:
  - Bash
  - Read
  - Glob
  - Grep
---

# ArkWeb App Debug

## When to Use This Skill

Use this skill when the user needs to debug HarmonyOS ArkWeb (webview) applications:

- **Debugging**: "Debug my webview", "Connect DevTools to app", "Inspect webview"
- **Testing**: "Test the web page", "Automate webview testing", "Check webview elements"
- **Troubleshooting**: "Webview not working", "Can't connect DevTools", "Socket not found"
- **Inspection**: "Show me webview console", "Inspect network requests", "Check web elements"

## Quick Start

Start an ArkWeb debugging session:

```bash
# Using the debug script (auto-configures environment)
$SKILL_DIR/scripts/start-debug.sh

# Or with explicit package name
$SKILL_DIR/scripts/start-debug.sh --package com.example.app
```

## Bundled Resources

This skill includes debugging scripts in `scripts/`:

| Script | Purpose |
|--------|---------|
| `start-debug.sh` | Start debugging session (macOS/Linux, auto-configures environment) |
| `start-debug.py` | Start debugging session (cross-platform Python) |
| `start-debug.bat` | Start debugging session (Windows, auto-configures environment) |

All scripts automatically:
- Detect DevEco Studio installation (via ohos-app-build-debug skill)
- Configure environment for hdc and other tools
- Find HarmonyOS project configuration
- Connect to device and create port forwarding
- Open Chrome DevTools on http://127.0.0.1:9222

For detailed documentation, see [references/](references/).

## Common Workflows

### Start Debugging Session

```bash
# Run the debug script from your HarmonyOS project directory
cd /path/to/harmonyos/project
$SKILL_DIR/scripts/start-debug.sh

# The script automatically:
# - Detects project configuration (package name, HAP path)
# - Connects to device
# - Creates port forwarding to webview_devtools socket
# - Opens Chrome DevTools
```

### Integration with ohos-app-build-debug

For complete development workflow:

```bash
# 1. Build and install app (using ohos-app-build-debug skill)
$OHOS_SKILL_DIR/scripts/build.py
$OHOS_SKILL_DIR/scripts/install.py -f entry/build/default/outputs/default/entry-default-signed.hap
$OHOS_SKILL_DIR/scripts/launch.py

# 2. Start debugging (this skill)
$SKILL_DIR/scripts/start-debug.sh
```

### AI Automated Testing (with MCP)

When Chrome DevTools MCP is configured, Claude can automatically test webview functionality:

```
User: Please test the login functionality in my webview
Claude: (Automatically uses Chrome DevTools MCP)
  - Opens DevTools connection
  - Navigates to login page
  - Fills out form
  - Submits and verifies results
```

See [references/mcp-guide.md](references/mcp-guide.md) for MCP setup and available tools.

## How It Works

### Automatic Detection

The debug scripts automatically:

1. **Detect DevEco Studio**: Uses ohos-app-build-debug skill to find DevEco Studio and configure hdc
2. **Find Project**: Searches upward for HarmonyOS project root (max 5 levels)
3. **Read Configuration**: Extracts package name from `AppScope/app.json5`
4. **Connect Device**: Uses hdc to connect to HarmonyOS device
5. **Find Socket**: Dynamically locates webview_devtools socket (handles PID changes)
6. **Create Port Forward**: Sets up port forwarding from device to localhost:9222
7. **Open DevTools**: Launches Chrome/Chromium with DevTools

### Critical Timing

Based on real debugging experience:
- **After app start**: Wait 10 seconds for Web component to create debugging socket
- **After port forward**: Wait 2 seconds for socket initialization
- **Socket finding**: Retry every 2 seconds for up to 15 seconds

The scripts handle these timings automatically.

## Response Guidelines

When helping users with ArkWeb debugging:

1. **Check Environment First**
   - Verify hdc is available (should be auto-detected by scripts)
   - Check device connection: `hdc list targets`
   - Ensure app has `setWebDebuggingAccess(true)` in `aboutToAppear()`

2. **Use Scripts**
   - Call scripts in `scripts/` directory
   - Replace `$SKILL_DIR` with actual skill installation path
   - Replace `$OHOS_SKILL_DIR` with ohos-app-build-debug skill path

3. **Handle Common Issues**
   - **Socket not found**: Wait 10-15s for app initialization, ensure Web component rendered
   - **Port in use**: Previous session may be running, check with `hdc fport list`
   - **Device not found**: Check USB connection and authorization
   - **hdc not found**: Scripts should auto-detect via ohos-app-build-debug

4. **Coordinate with ohos-app-build-debug**
   - For build/install issues, use ohos-app-build-debug skill
   - This skill focuses on debugging only

5. **Show Actual Commands**
   - Display the hdc commands being executed for transparency
   - Show port forwarding setup
   - Indicate DevTools URL

## Application Requirements

ArkWeb applications **must** enable debugging in `aboutToAppear()`:

```typescript
import { webview } from '@kit.ArkWeb';

@Entry
@Component
struct Index {
  controller: webview.WebviewController = new webview.WebviewController();

  aboutToAppear() {
    // CRITICAL: Enable debugging BEFORE Web component renders
    webview.WebviewController.setWebDebuggingAccess(true);
  }

  build() {
    Web({ src: this.currentUrl, controller: this.controller })
  }
}
```

## Error Handling

If DevEco Studio/hdc is not detected:
- Verify DevEco Studio is installed
- Ensure ohos-app-build-debug skill is available
- Scripts will attempt to auto-configure environment

If device is not connected:
- Check USB cable connection
- Verify USB debugging is enabled on device
- Authorize USB debugging on device when prompted

If socket is not found:
- Ensure app has `setWebDebuggingAccess(true)` in code
- Wait 10-15 seconds for app to fully initialize
- Verify Web component has rendered (navigate to a URL)
- Check if app crashed: `hdc shell ps -A | grep <package>`

For detailed troubleshooting, see [references/troubleshooting.md](references/troubleshooting.md).

## Prerequisites

- **DevEco Studio** 3.1+ (4.0+ recommended) OR **hdc** in PATH
- **HarmonyOS device** with USB debugging enabled
- **ArkWeb app** with `setWebDebuggingAccess(true)` enabled
- **Python** 3.8+ (for start-debug.py script)
- **Chrome/Chromium** browser (for DevTools)

Optional: **Chrome DevTools MCP** for AI automated testing

Download DevEco Studio: https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/ide-download

Overview

This skill debugs HarmonyOS ArkWeb (webview) applications by connecting Chrome DevTools to running apps via the hdc tool. It automatically detects project configuration, device connection, and webview debugging sockets, then sets up port forwarding and opens DevTools for inspection. The skill integrates with a build/install workflow to provide a complete debug loop for ArkWeb development.

How this skill works

The debug scripts locate DevEco Studio or hdc, find the HarmonyOS project root and package configuration, and use hdc to connect to the device. They search for the webview_devtools socket, create a port forward to localhost:9222, and launch Chrome DevTools or return the DevTools URL. The scripts handle timing and retries (waits after app start, retries socket discovery) and expose the hdc commands used so you can verify actions.

When to use it

  • Start an interactive debugging session for an ArkWeb webview
  • Connect Chrome DevTools to a HarmonyOS app to inspect DOM, console, and network
  • Troubleshoot inability to attach DevTools or missing webview sockets
  • Run automated webview tests through Chrome DevTools MCP
  • Manage port forwarding and multiple debugging sessions across devices

Best practices

  • Enable web debugging early in aboutToAppear() with WebviewController.setWebDebuggingAccess(true)
  • Build and install the app first, then start the debug script from the project root
  • Verify hdc is available and device shows in hdc list targets before running scripts
  • Wait 10–15 seconds after launching the app for the webview socket to appear
  • Inspect the printed hdc commands and forwarded port to diagnose connection issues

Example use cases

  • Open DevTools to inspect and live-edit a page rendered in an ArkWeb webview
  • Capture network requests from the webview to troubleshoot failing API calls
  • Use Chrome DevTools MCP to automate form submission and verify UI flows
  • Recover from a stuck debug session by listing forwarded ports and restarting forwarding
  • Validate that setWebDebuggingAccess(true) is called and the socket becomes available

FAQ

What if DevTools doesn’t open automatically?

Check that the script printed a localhost:9222 URL, verify the forwarded port is free, and open the URL manually in Chrome/Chromium.

Socket not found—what should I check?

Ensure the app enabled web debugging, wait 10–15s after app start, confirm the Web component rendered, and verify the app process is running via hdc shell ps.