HarmonyOS
Midscene connects to HarmonyOS NEXT devices through HarmonyOS Device Connector (HDC) to automate apps and system interfaces.
This guide covers device connection, model configuration, Playground, and JavaScript SDK integration with @midscene/harmony.
See it in action
Prompt: Open Settings, find About phone, and view the device information.
View the full report, or explore more Midscene showcases.
Get started
Prepare your HarmonyOS device
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 the 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:
Launch Playground
Playground is the fastest way to validate the connection and try core capabilities such as aiAct, aiQuery, and aiAssert without writing code. It shares the same core as @midscene/harmony, so anything that works here will behave the same once scripted.
- Launch the Playground CLI:
- Click the gear button in the Playground window and paste your API Key configuration. See Supported models and setup if you still need a model configuration.
Use the JavaScript SDK
Once Playground runs successfully, you can switch to reusable JavaScript scripts.
Configure the model
Set the model configuration through environment variables. For supported models and copyable setup examples, see Supported models and setup.
For all configuration options, see Model configuration.
Install dependencies
Write a script
The following example opens the Settings app on the device and performs scrolling operations.
Run the script
After the script finishes, you should see Midscene - report file updated: /path/to/report/some_id.html in the console. Open the generated HTML file in a browser to replay every interaction, query, and assertion.
Advanced
Use this section to customize device behavior, integrate Midscene into a standalone framework, or troubleshoot HDC issues. See the HarmonyOS section of the API reference for more constructor parameters.
Extending Midscene on HarmonyOS
Use defineAction() to define custom actions. When constructing HarmonyDevice, pass these actions through customActions. Midscene appends these actions to the planner so the Agent can call the domain-specific actions you define.
For more details on custom actions and action schemas, see Integrate with Any Interface.
FAQ
Keyboard is not dismissed or the page goes back after typing
Midscene automatically dismisses the keyboard after entering text. By default, HarmonyOS uses the ESC key so the current page is less likely to navigate back. If ESC does not close the keyboard in your app, switch to Back first:
If your input field listens for Back and clears or closes in response, disable auto keyboard dismiss:
With auto dismiss disabled, the keyboard will remain visible. You can use aiAct to manually dismiss it, e.g. await agent.aiAct('dismiss the keyboard').
How to use a custom HDC path?
Set the HDC_HOME environment variable to point to the HDC directory:
Or pass it via the constructor:
More
- View all Agent methods: API Reference (Common)
- HarmonyOS-specific parameters and interfaces: API reference (HarmonyOS)
- Use YAML automation scripts and command-line tools.
- 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

