方式 A:嵌入 crate
直接驱动设备、编排并执行 Flow
在自有 Rust 程序里直接构造驱动、跑 Flow。有两种构造驱动的方式:用 DeviceManager::connect(platform, config) 按平台一把构造,或直接 new 具体驱动类型。
DeviceManager::connect
use aixautosdk::core::prelude::*;
#[tokio::main]
async fn main() -> aixautosdk::core::Result<()> {
// 按 platform + DeviceConfig 构造真实驱动
let device = DeviceManager::connect("android", DeviceConfig {
serial: Some("emulator-5554".into()),
..Default::default()
}).await?;
// "ios" → 需 config.wda_url
// "macos"/"windows"/"desktop" → Python sidecar(config.python_path 可选)
// 设备操作(DeviceDriver trait)
let png: Vec<u8> = device.screenshot().await?;
let tree: UITreeNode = device.capture_ui_tree().await?;
let btn: Element = device.find_element(&Locator::text("登录")).await?;
device.tap(btn.rect.center_x(), btn.rect.center_y()).await?;
device.input_text("hello").await?;
device.press_key(KeyCode::Enter).await?;
// 智能等待
let wait = WaitEngine::new(10.0);
let _ = wait.for_element(device.as_ref(), &Locator::text("首页"), None).await?;
Ok(())
}直接构造具体驱动
需要更细的连接参数时:
use aixautosdk::core::prelude::*;
use aixautosdk::core::device::{android::AndroidDevice, ios::IOSDevice, desktop::DesktopDriver};
// Android:原生 adb(input/screencap),或 uiautomator2 server:
let a = AndroidDevice::new_raw_adb("emulator-5554")?; // adb 直驱
let a2 = AndroidDevice::new("127.0.0.1", 6790, "emulator-5554")?; // u2 server
// iOS:连接 WebDriverAgent
let i = IOSDevice::connect("http://localhost:8100").await?;
// Desktop:Python sidecar(None = 默认 python)
let d = DesktopDriver::new(Platform::MacOS, None).await?;用 Flow 描述并执行脚本
use aixautosdk::core::prelude::*;
use aixautosdk::core::script::{builder::FlowBuilder, executor::ScriptExecutor, events::ExecutionEvent};
use tokio::sync::mpsc;
let flow = FlowBuilder::new("登录测试", Platform::Android)
.tap(Locator::text("登录"))
.input_text(Locator::label("用户名"), "admin")
.input_text(Locator::label("密码"), "secret")
.tap(Locator::text("确定"))
.assert_visible(Locator::text("首页"))
.build();
let (tx, mut rx) = mpsc::unbounded_channel::<ExecutionEvent>();
tokio::spawn(async move { while let Some(ev) = rx.recv().await { println!("{ev:?}"); } });
// ScriptExecutor 是无状态单元类型
let result = ScriptExecutor.execute(device.as_ref(), &flow, tx).await;
assert_eq!(result.status, FlowStatus::Passed);实际可用的
FlowBuilder链式方法与Step变体以aixautosdk::core::script模块为准(cargo doc -p aixautosdk --open)。完整接口见 API 参考。