使用 IntelliJ IDEA 创建 Android UiAutomator 测试工程

UPDATE: 做了个模板,放在了 GitHub 上,有需要请自取。
UPDATE2: 又做了一个 Gradle 版的模板:UITest1.0

UiAutomator 是 Android 官方提供的一个功能非常强大的测试框架,利用该框架可以非常方便的测试应用的稳定性。官方也有详细的教程指导我们使用 Eclipse 创建 UiAutomator 测试工程的说明。

然而,随着 Android 推出新一代的开发 IDE——Android Studio,笔者也渐渐喜欢上了 JetBrains 家的产品,于是决定将 UiAutomator 测试工程搬到 IntelliJ IDEA 上来。下面我们将简单讲解一下使用 IDEA 创建 UiAutomator 测试工程的步骤。

说明:本文基于 Mac 环境讲解,在 Windows 和 Linux 环境上可能稍有不同,请知晓。

0x00 打开 IDEA 并选择 “ Create New Project”

创建新工程

0x01 依次选择 “Android”--> “Empty Module”,然后点击 “Next”

选择工程类型

0x02 配置项目信息(项目名,路径等),然后点击“Finish”

配置项目信息

0x03 添加测试依赖库

点击界面右上角的图标,如下图所示,也可以通过点击 “File”-->“Project Structure...”完成此操作


打开项目结构配置界面

依次选择“Libraries”-->“+”-->“Java”


添加依赖库

在文件选择窗口中选择 [你的 Android sdk 目录 ]/platforms/android-xx 文件夹下的 android.jaruiautomotor.jar,然后点击“OK”

选择依赖库

在弹出的“Choose Modules” 窗口中选择我们刚创建的工程,这里为 UiAutoTest,点击“OK”

Choose Modules

如果一切正常的话,你会看到如下界面,点击“OK”完成依赖库配置


完成依赖库配置

0x04 添加 build.xml 文件

由于 UiAutomator 工程使用 Ant 构建,所以还需要在工程中添加用于 Ant 构建的 build.xml 文件,这里直接拷贝了之前工程中的 build.xml 文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<project name="Test" default="build">

    <!-- The local.properties file is created and updated by the 'android' tool.
         It contains the path to the SDK. It should *NOT* be checked into
         Version Control Systems. -->
    <property file="local.properties" />

    <!-- The ant.properties file can be created by you. It is only edited by the
         'android' tool to add properties to it.
         This is the place to change some Ant specific build properties.
         Here are some properties you may want to change/update:

         source.dir
             The name of the source directory. Default is 'src'.
         out.dir
             The name of the output directory. Default is 'bin'.

         For other overridable properties, look at the beginning of the rules
         files in the SDK, at tools/ant/build.xml

         Properties related to the SDK location or the project target should
         be updated using the 'android' tool with the 'update' action.

         This file is an integral part of the build system for your
         application and should be checked into Version Control Systems.

         -->
    <property file="ant.properties" />

    <!-- if sdk.dir was not set from one of the property file, then
         get it from the ANDROID_HOME env var.
         This must be done before we load project.properties since
         the proguard config can use sdk.dir -->
    <property environment="env" />
    <condition property="sdk.dir" value="${env.ANDROID_HOME}">
        <isset property="env.ANDROID_HOME" />
    </condition>

    <!-- The project.properties file is created and updated by the 'android'
         tool, as well as ADT.

         This contains project specific properties such as project target, and library
         dependencies. Lower level build properties are stored in ant.properties
         (or in .classpath for Eclipse projects).

         This file is an integral part of the build system for your
         application and should be checked into Version Control Systems. -->
    <loadproperties srcFile="project.properties" />

    <!-- quick check on sdk.dir -->
    <fail
            message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through the ANDROID_HOME environment variable."
            unless="sdk.dir"
    />

    <!--
        Import per project custom build rules if present at the root of the project.
        This is the place to put custom intermediary targets such as:
            -pre-build
            -pre-compile
            -post-compile (This is typically used for code obfuscation.
                           Compiled code location: ${out.classes.absolute.dir}
                           If this is not done in place, override ${out.dex.input.absolute.dir})
            -post-package
            -post-build
            -pre-clean
    -->
    <import file="custom_rules.xml" optional="true" />

    <!-- Import the actual build file.

         To customize existing targets, there are two options:
         - Customize only one target:
             - copy/paste the target into this file, *before* the
               <import> task.
             - customize it to your needs.
         - Customize the whole content of build.xml
             - copy/paste the content of the rules files (minus the top node)
               into this file, replacing the <import> task.
             - customize to your needs.

         ***********************
         ****** IMPORTANT ******
         ***********************
         In all cases you must update the value of version-tag below to read 'custom' instead of an integer,
         in order to avoid having your file be overridden by tools such as "android update project"
    -->
    <!-- version-tag: VERSION_TAG -->
    <import file="${sdk.dir}/tools/ant/uibuild.xml" />

</project>

有自定义需求的同学请自觉研究一下 Ant 工具的使用,这里不赘述。

0x05 添加 project.properties 文件

接下来我们需要在工程根目录添加 project.properties 文件,并指定待测设备的 Android 版本,添加以下行:

target=android-20

至此,我们已经成功的创建了 UiAutomator 工程,下面让我们编写一个名为 BaseUiTest 的测试检验工程是否能成功编译:

package com.lovexiaov.uiautotest;

import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;


public class BaseUiTest extends UiAutomatorTestCase {

    public void testEnvIsOk() {
        UiDevice.getInstance().pressHome();
    }
}

命令行执行 ant build 或在 IDEA 主窗口点击三角按钮构建,如下图:

构建工程

如果在构建过程中没有出错,你应该会看到如下图中所示的bin目录及其中的 UiAutoTest.jar 文件。

执行结果

怎么样,很简单吧?赶快体验一下吧!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,287评论 25 708
  • 感受风吹过耳边,走在一条繁华的大街上,在这个大街,车辆快速的行驶,摩托声,自行车手,汽车的轰鸣声,路边工人施工的声...
    seraph霸气阅读 228评论 0 0
  • 我破开了黑暗的夜 穿过层层的迷雾 迎着黄金色的朝晖 奔跑着 沾满了花瓣与露珠 与风一起起舞 我跳进海里 追逐着水里...
    大师兄说阅读 248评论 0 0
  • 春天 在稚嫩的泥土里 埋一粒善良的种子 用爱来浇瓘 让它长成一棵参天大树 上面结满丰硕的诗句 .
    泰山寒梅阅读 248评论 4 1
  • 1、我、聂好、两三个男孩子、一个不能看清脸身高很高穿着42中校服的男孩子、一个能看清脸长相很小很精致的男孩子、一个...
    阿PP阿DD阅读 222评论 0 0