Skip to content

Commit 84654d6

Browse files
quanruclaudeyuyutaotao
authored
feat(android): add runAdbCommand method to AndroidAgent (#1271)
* feat(android): add runAdbCommand method to AndroidAgent Add a new runAdbCommand method that allows users to execute custom adb shell commands directly through the AndroidAgent. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> * feat(android): update interface name --------- Co-authored-by: Claude <[email protected]> Co-authored-by: yutao <[email protected]>
1 parent ca45146 commit 84654d6

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

apps/site/docs/en/integrate-with-android.mdx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,40 @@ await agent.launch('com.android.settings'); // open a native page
164164
await agent.launch('com.android.settings/.Settings'); // open a native page
165165
```
166166

167+
#### `agent.runAdbShell()`
168+
169+
Execute a command through `adb shell` on the connected device.
170+
171+
> Note: This method wraps `adb shell` and forwards the command directly to the device.
172+
173+
- Type
174+
175+
```typescript
176+
function runAdbShell(command: string): Promise<string>;
177+
```
178+
179+
- Parameters:
180+
181+
- `command: string` - The adb shell command to execute.
182+
183+
- Return Value:
184+
185+
- `Promise<string>` - Returns a Promise that resolves to the command output.
186+
187+
- Examples:
188+
189+
```typescript
190+
import { AndroidAgent, AndroidDevice } from '@midscene/android';
191+
192+
const device = new AndroidDevice('s4ey59');
193+
const agent = new AndroidAgent(device);
194+
await device.connect();
195+
196+
const result = await agent.runAdbShell('dumpsys battery');
197+
// Equivalent to running `adb shell dumpsys battery`
198+
console.log(result);
199+
```
200+
167201
#### `agentFromAdbDevice()`
168202

169203
Create a AndroidAgent from a connected adb device.

apps/site/docs/zh/integrate-with-android.mdx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,40 @@ await agent.launch('com.android.settings'); // 打开系统设置 app(package na
163163
await agent.launch('com.android.settings/.Settings'); // 打开系统设置 app(package name) 的 .Settings(activity name) 页面
164164
```
165165

166+
#### `agent.runAdbShell()`
167+
168+
执行 `adb shell` 命令。
169+
170+
> 注意:该方法本质上是调用 `adb shell` 执行传入的命令。
171+
172+
- 类型
173+
174+
```typescript
175+
function runAdbShell(command: string): Promise<string>;
176+
```
177+
178+
- 参数:
179+
180+
- `command: string` - 要执行的 adb shell 命令
181+
182+
- 返回值:
183+
184+
- `Promise<string>` - 命令执行的输出结果
185+
186+
- 示例:
187+
188+
```typescript
189+
import { AndroidAgent, AndroidDevice } from '@midscene/android';
190+
191+
const page = new AndroidDevice('s4ey59');
192+
const agent = new AndroidAgent(page);
193+
await page.connect();
194+
195+
const result = await agent.runAdbShell('dumpsys battery');
196+
// 等同于执行 `adb shell dumpsys battery`
197+
console.log(result);
198+
```
199+
166200
#### `agentFromAdbDevice()`
167201

168202
从已连接的 adb 设备中,创建一个 AndroidAgent。

packages/android/src/agent.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ export class AndroidAgent extends PageAgent<AndroidDevice> {
1212
const device = this.page;
1313
await device.launch(uri);
1414
}
15+
16+
async runAdbShell(command: string): Promise<string> {
17+
const adb = await this.page.getAdb();
18+
return await adb.shell(command);
19+
}
1520
}
1621

1722
export async function agentFromAdbDevice(

0 commit comments

Comments
 (0)