简介

Android Debug Bridge 如其名,是连接Android Device/Emulator的桥梁,IDE如AS,ADT,Android测试框架Appium,Selendroid无不heavy的依靠adb。而ddmlib无疑是最好的adb api. 在刚使用时略复杂,好在有个lib让它的使用变得简单。

携程在构建无线测试框架的时候,非常重视ddmlib API的打造,充分参考Selendroid/Appium的实现,毕竟这是基础的基础,而不是各种乱造。

Quick Start

Maven

<dependency>
<groupId>com.github.cosysoft</groupId>
<artifactId>device-api</artifactId>
<version>0.9.1</version>
</dependency>

Gradle

dependencies {
compile 'com.github.cosysoft:device-api:0.9.1'
}

获取PC中连接的设备/模拟器

public class EchoTest {

@Test
public void takeDevices() {
TreeSet<AndroidDevice> devices = AndroidDeviceStore.getInstance()
.getDevices();
AndroidDevice device = devices.pollFirst();
System.out.println(device.getName());
}
}

截图 它可以直接在内存中,暴露restful服务有优势

BufferedImage image = device.takeScreenshot();
String imagePath = new File(System.getProperty("java.io.tmpdir"),
"screenshot.png").getAbsolutePath();
ImageUtils.writeToFile(image, imagePath);

安装/卸载

AndroidApp app = new DefaultAndroidApp(new File(
"d:\\uat\\com.android.chrome.apk"));
device.install(app);
if (device.isInstalled(app)) {
device.uninstall(app);
}

LogCat 过滤

final LogCatFilter filter = new LogCatFilter("", "", "com.android", "",
"", LogLevel.WARN);
final LogCatListener lcl = new LogCatListener() {
@Override
public void log(List<LogCatMessage> msgList) {
for (LogCatMessage msg : msgList) {
if (filter.matches(msg)) {
System.out.println(msg);
}
}
}
};

device.addLogCatListener(lcl);
Thread.sleep(60000);

List running client for app

List<ClientDataInfo> clientDataInfos = device.getClientDatasInfo();
for (ClientDataInfo client : clientDataInfos) {
System.out.println(client.getName());
System.out.println(client.getPid());
}

更多api可以参考其javadoc


↙↙↙阅读原文可查看相关链接,并与作者交流