HarmonyOS Getting Started
This guide walks you through everything needed to automate HarmonyOS devices with Midscene: connecting a real device via HDC, configuring model API keys, trying the zero-code Playground, and running your first JavaScript script.
Control HarmonyOS devices with JavaScript: https://github.com/web-infra-dev/midscene-example/blob/main/harmony/javascript-sdk-demo
Integrate Vitest for testing: https://github.com/web-infra-dev/midscene-example/tree/main/harmony/vitest-demo
Set up API keys for model
Set your model configs into the environment variables. You may refer to Model strategy for more details.
For more configuration details, please refer to Model strategy and Model configuration.
Prerequisites
Before writing scripts, verify that HDC can connect to your device and the device trusts the current computer.
Install HDC
HDC (HarmonyOS Device Connector) is a command-line tool provided by HarmonyOS for communicating with devices. Installation options:
- Via DevEco Studio (recommended)
- Via HarmonyOS command-line tools standalone installation
Verify HDC is installed:
A version number in the output confirms successful installation.
If hdc is not in your system PATH, set the HDC_HOME environment variable to the directory containing HDC:
Enable Developer Mode and Verify Device
In your HarmonyOS device settings, go to Developer Options and enable USB Debugging, then connect via USB cable.
Verify the connection:
A device ID in the output confirms a successful connection:
Try Playground (Zero Code)
Playground is the fastest way to verify your connection and observe the AI Agent, without writing any code. It shares the same code implementation as @midscene/harmony, so flows validated in Playground will work identically when run via scripts.
- Launch the Playground CLI:
- Click the gear button in the Playground window and paste your API Key configuration. If you don't have an API Key yet, go back to Model Configuration to get one.
Start Your Experience
After configuration, you can start using Midscene right away. It provides several key operation tabs:
- Act: interact with the page. This is Auto Planning, corresponding to
aiAct. For example:
- Query: extract JSON data from the interface, corresponding to
aiQuery.
Similar methods include aiBoolean(), aiNumber(), and aiString() for directly extracting booleans, numbers, and strings.
- Assert: understand the page and assert; if the condition is not met, throw an error, corresponding to
aiAssert.
- Tap: click on an element. This is Instant Action, corresponding to
aiTap.
For the difference between Auto Planning and Instant Action, see the API document.
Integrate Midscene Agent
Once Playground runs successfully, you can switch to reusable JavaScript scripts.
Step 1: Install Dependencies
Step 2: Write a Script
The following example opens the Settings app on the device and performs scrolling operations.
Step 3: Run the Example
Step 4: View the Report
After a successful run, the console outputs Midscene - report file updated: /path/to/report/some_id.html. Open this HTML file in a browser to replay each interaction, query, and assertion.
Advanced
When you need to customize device behavior, integrate Midscene into a standalone framework, or troubleshoot HDC issues, refer to this section. See API Reference (HarmonyOS) for more constructor parameters.
Extending Midscene on HarmonyOS
Use defineAction() to define custom gestures and pass them via customActions. Midscene appends custom actions to the planner, allowing AI to invoke your domain-specific action names.
For more details on custom actions and action schemas, see Integrate with Any Interface.
More
- View all Agent methods: API Reference (Common)
- HarmonyOS-specific parameters and interfaces: HarmonyOS Agent API
- Demo projects
- HarmonyOS JavaScript SDK demo: https://github.com/web-infra-dev/midscene-example/blob/main/harmony/javascript-sdk-demo
- HarmonyOS + Vitest demo: https://github.com/web-infra-dev/midscene-example/tree/main/harmony/vitest-demo
Complete example (Vitest + HarmonyAgent)
Merged reports are stored inside midscene_run/report by default. Override the directory with MIDSCENE_RUN_DIR when running in CI.

