UiAutomator 如何使用和调试 android UIAutomator ?

恒温 · 2014年09月03日 · 最后由 懿航 回复于 2016年09月21日 · 3784 次阅读
本帖已被设为精华帖!

首先介绍一个 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 就是用了这个依赖库。

如何使用?

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

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

<dependencies>
       <dependency>
           <groupId>android</groupId>
           <artifactId>android</artifactId>
           <version>4.4.2_r3</version>
       </dependency>

       <dependency>
           <groupId>android.test.uiautomator</groupId>
           <artifactId>uiautomator</artifactId>
           <version>4.4.2_r3</version>
       </dependency>
   </dependencies>

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

我使用的是 Intellij IDEA

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

目录结构如下:

  • pom.xml 修改为如下:

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

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.testerhome</groupId>
    <artifactId>uiautomator</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>android</groupId>
            <artifactId>android</artifactId>
            <version>4.4.2_r3</version>
        </dependency>

        <dependency>
            <groupId>android.test.uiautomator</groupId>
            <artifactId>uiautomator</artifactId>
            <version>4.4.2_r3</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
    </dependencies>

</project>

测试代码如下:

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.

然后使用 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 true
Sending WAIT chunk

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

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

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

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

顺利进入 debug 模式:

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 18 条回复 时间 点赞
baxia 修改 Bootstrap 源码,添加自定义监听代码实现 中提及了此贴 06月22日 14:51

大大的精华

学习了!!

你这教程都可以拆成 2 个了, 写的很赞.

赞~给了一条扩展 uiautomator 的明路

#5 楼 @xiaorudk 啥异常啊? 加个头像吧

学习啦~

再顶下我的文章。

顶,刚刚好找 UIautomator 的方法

不理解 o(╯□╰) o mark~

恒温 #11 · 2015年04月13日 Author

#10 楼 @jennyhui 那么好理解的文章。。。

#11 楼 @lihuazhang 呃呃呃 到时候跟着实践一下 今天先忙

不错不错

这么做的好处是什么?为什么不按官方的方法来做呢?

恒温 #16 · 2015年12月17日 Author

#15 楼 @actionwind 有些人的项目用的是 maven,使用 maven 集成会比较一致。

运行 adb shell uiautomator runtest uiautomator-example.jar -c com.testerhome.uiautomator.LaunchSettings -e debug true 后,程序呼呼地跑起来了, 没有出现 “ Sending WAIT chunk”,这是为什么呢?

我发现加入-e debug true 参数之后,有的可以进入调试模式,有的则不行。看教程是需要在 root 模式下进行,而我的确每次都是 root 的。请问进入 “ Sending WAIT chunk” 调试模式的关键是什么啊?有么有什么通用的 uiautomator 单点调试方法?谢谢!

恒温 [该话题已被删除] 中提及了此贴 08月03日 10:42

uiautomator2 在 eclipse 中 怎么使用 楼主 可曾用过?能否写个教程?

恒温 UIAutomator2.0 简介 中提及了此贴 12月09日 15:55
5楼 已删除
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册