android怎么调试,如何使用和调试 android UIAutomator ?

本文介绍了如何使用maven-android-sdk-deployer库来构建Android UIAutomator测试项目,包括设置环境、配置pom.xml、编写测试代码,并通过Android Device Monitor进行远程调试。提供了一步步的操作指南,帮助开发者更便捷地进行Android应用自动化测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先介绍一个 maven 依赖库: maven-android-sdk-deployer

A tool to install components of the Android SDK to use with the Maven Android Plugin.

The Maven Android SDK Deployer is a helper maven project that can be used to install the libraries necessary to build Android applications with Maven and the Android Maven Plugin directly from your local Android SDK installation.

简单来说,就是整个东西可以帮助你使用 maven 来构建 android sdk 相关的项目,解决依赖。Appium 的 bootstrap 就是用了这个依赖库。

如何使用?

确保设置了 ANDROID_HOME 和安装了 maven 3.1

确保你安装了对应的 android sdk 和 api

在 maven-android-sdk-deployer 目录底下运行 mvn install -P 4.4

这个过程中,可能你的 sdk 有些包没安装,比如 google maps,它会告诉你 build fail,没关系,别管他,除非你想要 maps,那么你先得在 android 的 sdk manager 里面安装它。

mvn install 好之后,我们就可以在 pom.xml 里面大胆的使用:

android

android

4.4.2_r3

android.test.uiautomator

uiautomator

4.4.2_r3

那我们创建一个新的 UIAutomator 的测试类

我使用的是 Intellij IDEA

选择 maven 项目,如下图,之后的 groupid 和 Artifact id 就随便填写吧。

115fbb439ca932464aea3eee86fb0c81.png

目录结构如下:

6ec6996d7411086a095c479e7419fc0e.png

pom.xml 修改为如下:

注: 按官方文档应该用 junit 3, 不过试验下来用 junit4 也没出问题。

内容不要尽抄。

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.testerhome

uiautomator

1.0-SNAPSHOT

android

android

4.4.2_r3

android.test.uiautomator

uiautomator

4.4.2_r3

junit

junit

4.11

测试代码如下:

package com.testerhome.uiautomator;// Import the uiautomator libraries

import com.android.uiautomator.core.UiObject;

import com.android.uiautomator.core.UiObjectNotFoundException;

import com.android.uiautomator.core.UiScrollable;

import com.android.uiautomator.core.UiSelector;

import com.android.uiautomator.testrunner.UiAutomatorTestCase;

public class LaunchSettings extends UiAutomatorTestCase {

public void testDemo() throws UiObjectNotFoundException {

getUiDevice().pressHome();

UiObject allAppsButton = new UiObject(new UiSelector()

.description("Apps"));

allAppsButton.clickAndWaitForNewWindow();

// 进入设置菜单

UiObject settingApp = new UiObject(new UiSelector().text("Settings"));

settingApp.click();

//休眠3秒

try {

Thread.sleep(3000);

} catch (InterruptedException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

// 进入语言和输入法设置

UiScrollable settingItems = new UiScrollable(new UiSelector().scrollable(true));

UiObject languageAndInputItem = settingItems.getChildByText(

new UiSelector().text("Language & input"), "Language & input", true);

languageAndInputItem.clickAndWaitForNewWindow();

}

}

然后根据 UIAutomator 的教程生成 jar 包

android create uitest-project -n uiautomator-example -t 1 -p uiautomator-example

ant build

adb push bin/uiautomator-example.jar /data/local/tmp/

adb shell uiautomator runtest uiautomator-example.jar -c com.testerhome.uiautomator.LaunchSettings

基本上测试脚本就运行起来了。

那么如何调试?

打开 Android Device Monitor, DDMS 的继任。

The standalone version of DDMS is deprecated.

Please use Android Device Monitor (tools/monitor) instead.

d73b7f6029cb242ad8dd6b5cc318f038.png

然后使用 debug 模式重新运行:

adb shell uiautomator runtest uiautomator-example.jar -c com.testerhome.uiautomator.LaunchSettings -e debug true

➜ uiautomator-example adb shell uiautomator runtest uiautomator-example.jar -c com.testerhome.uiautomator.LaunchSettings -e debug trueSending WAIT chunk

再看 monitor,你会发现有个红色虫子的进程,那个端口就是 debug 监听的端口。

e91895f5163a928ca790120cfd6ba5b4.png

我们再打开, intellij idea 的 debug 配置,添加一个 remote 调试,

21aa2e71f342d41f06f16d5a7656d26a.png

更改端口为之前拿到的端口,然后 module classpath 设置为要 debug 的模块,保存

9eb83afa5403c6f6f93dca18954c3478.png

在主窗口,选择刚刚保存的 remote 配置,运行 debug,

8b7a044dbe5264e5477b7a15049089c9.png

顺利进入 debug 模式:

f21c84ca26c4bfc1bf8ac1cd35f8dc3b.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值