使用 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 文件。

执行结果

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容

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