After connecting the Android device with adb, you can use Midscene javascript SDK to control Android devices.
Control Android devices with javascript: https://github.com/web-infra-dev/midscene-example/blob/main/android/javascript-sdk-demo
Integrate Vitest for testing: https://github.com/web-infra-dev/midscene-example/tree/main/android/vitest-demoInstall Node.js 18 or above globally.
Prepare an API key from a visual-language (VL) model. You will use it later.
You can check the supported models in Choose a model
adb
is a command-line tool that allows you to communicate with an Android device. There are two ways to install adb
:
Verify adb is installed successfully:
When you see the following output, adb is installed successfully:
Reference Android environment variables, set the environment variable ANDROID_HOME
.
Verify the ANDROID_HOME
variable is set successfully:
When the command has any output, the ANDROID_HOME
variable is set successfully:
In the developer options of the system settings, enable the 'USB debugging' of the Android device, if the 'USB debugging (secure settings)' exists, also enable it, then connect the Android device with a USB cable
Verify the connection:
When you see the following output, the connection is successful:
Set your model configs into the environment variables. You may refer to choose a model for more details.
Let's take a simple example: search for headphones on eBay using the browser in the Android device. (Of course, you can also use any other apps on the Android device.)
Write the following code, and save it as ./demo.ts
Using tsx
to run
After a while, you will see the following output:
After the above command executes successfully, the console will output: Midscene - report file updated: /path/to/report/some_id.html
. You can open this file in a browser to view the report.
AndroidDevice
constructorThe AndroidDevice constructor supports the following parameters:
deviceId: string
- The device idopts?: AndroidDeviceOpt
- Optional, the options for the AndroidDevice
autoDismissKeyboard?: boolean
- Optional, whether to dismiss the keyboard after inputting. (Default: true)androidAdbPath?: string
- Optional, the path to the adb executable.remoteAdbHost?: string
- Optional, the remote adb host.remoteAdbPort?: number
- Optional, the remote adb port.Except the common agent interfaces in API Reference, AndroidAgent also provides some other interfaces:
agent.launch()
Launch a webpage or native page.
Parameters:
uri: string
- The uri to open, can be a webpage url or a native app's package name or activity name, if the activity name exists, it should be separated by / (e.g. com.android.settings/.Settings).Return Value:
Examples:
agentFromAdbDevice()
Create a AndroidAgent from a connected adb device.
Parameters:
deviceId?: string
- Optional, the adb device id to connect. If not provided, the first connected device will be used.opts?: PageAgentOpt & AndroidDeviceOpt
- Optional, the options for the AndroidAgent, PageAgentOpt refer to constructor, AndroidDeviceOpt refer to AndroidDevice constructor.Return Value:
Promise<AndroidAgent>
Returns a Promise that resolves to an AndroidAgent.Examples:
getConnectedDevices()
Get all connected Android devices.
Return Value:
Promise<Device[]>
Returns a Promise that resolves to an array of Device.Examples:
Please check if the device is unlocked in the developer options of the system settings.
You can use the MIDSCENE_ADB_PATH
environment variable to specify the path to the adb executable, MIDSCENE_ADB_REMOTE_HOST
environment variable to specify the remote adb host, MIDSCENE_ADB_REMOTE_PORT
environment variable to specify the remote adb port.
Additionally, you can also specify the adb path, remote adb host and port through the AndroidDevice constructor.