使用 IntelliJ IDEA 創(chuàng)建 Android UiAutomator 測試工程

UPDATE: 做了個模板谣光,放在了 GitHub 上羡滑,有需要請自取。
UPDATE2: 又做了一個 Gradle 版的模板:UITest1.0

UiAutomator 是 Android 官方提供的一個功能非常強(qiáng)大的測試框架,利用該框架可以非常方便的測試應(yīng)用的穩(wěn)定性。官方也有詳細(xì)的教程指導(dǎo)我們使用 Eclipse 創(chuàng)建 UiAutomator 測試工程的說明似袁。

然而,隨著 Android 推出新一代的開發(fā) IDE——Android Studio咐刨,筆者也漸漸喜歡上了 JetBrains 家的產(chǎn)品,于是決定將 UiAutomator 測試工程搬到 IntelliJ IDEA 上來扬霜。下面我們將簡單講解一下使用 IDEA 創(chuàng)建 UiAutomator 測試工程的步驟定鸟。

說明:本文基于 Mac 環(huán)境講解,在 Windows 和 Linux 環(huán)境上可能稍有不同著瓶,請知曉联予。

0x00 打開 IDEA 并選擇 “ Create New Project”

創(chuàng)建新工程

0x01 依次選擇 “Android”--> “Empty Module”,然后點擊 “Next”

選擇工程類型

0x02 配置項目信息(項目名材原,路徑等)沸久,然后點擊“Finish”

配置項目信息

0x03 添加測試依賴庫

點擊界面右上角的圖標(biāo),如下圖所示余蟹,也可以通過點擊 “File”-->“Project Structure...”完成此操作


打開項目結(jié)構(gòu)配置界面

依次選擇“Libraries”-->“+”-->“Java”


添加依賴庫

在文件選擇窗口中選擇 [你的 Android sdk 目錄 ]/platforms/android-xx 文件夾下的 android.jaruiautomotor.jar卷胯,然后點擊“OK”

選擇依賴庫

在彈出的“Choose Modules” 窗口中選擇我們剛創(chuàng)建的工程,這里為 UiAutoTest威酒,點擊“OK”

Choose Modules

如果一切正常的話窑睁,你會看到如下界面,點擊“OK”完成依賴庫配置


完成依賴庫配置

0x04 添加 build.xml 文件

由于 UiAutomator 工程使用 Ant 構(gòu)建葵孤,所以還需要在工程中添加用于 Ant 構(gòu)建的 build.xml 文件担钮,這里直接拷貝了之前工程中的 build.xml 文件,內(nèi)容如下:

<?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>

有自定義需求的同學(xué)請自覺研究一下 Ant 工具的使用尤仍,這里不贅述箫津。

0x05 添加 project.properties 文件

接下來我們需要在工程根目錄添加 project.properties 文件,并指定待測設(shè)備的 Android 版本,添加以下行:

target=android-20

至此苏遥,我們已經(jīng)成功的創(chuàng)建了 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();
    }
}

命令行執(zhí)行 ant build 或在 IDEA 主窗口點擊三角按鈕構(gòu)建,如下圖:

構(gòu)建工程

如果在構(gòu)建過程中沒有出錯暖眼,你應(yīng)該會看到如下圖中所示的bin目錄及其中的 UiAutoTest.jar 文件惕耕。

執(zhí)行結(jié)果

怎么樣,很簡單吧诫肠?趕快體驗一下吧司澎!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市栋豫,隨后出現(xiàn)的幾起案子挤安,更是在濱河造成了極大的恐慌,老刑警劉巖丧鸯,帶你破解...
    沈念sama閱讀 221,198評論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蛤铜,死亡現(xiàn)場離奇詭異,居然都是意外死亡丛肢,警方通過查閱死者的電腦和手機(jī)围肥,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評論 3 398
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蜂怎,“玉大人穆刻,你說我怎么就攤上這事「懿剑” “怎么了氢伟?”我有些...
    開封第一講書人閱讀 167,643評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長幽歼。 經(jīng)常有香客問我朵锣,道長,這世上最難降的妖魔是什么甸私? 我笑而不...
    開封第一講書人閱讀 59,495評論 1 296
  • 正文 為了忘掉前任诚些,我火速辦了婚禮,結(jié)果婚禮上颠蕴,老公的妹妹穿的比我還像新娘泣刹。我一直安慰自己,他們只是感情好犀被,可當(dāng)我...
    茶點故事閱讀 68,502評論 6 397
  • 文/花漫 我一把揭開白布椅您。 她就那樣靜靜地躺著,像睡著了一般寡键。 火紅的嫁衣襯著肌膚如雪掀泳。 梳的紋絲不亂的頭發(fā)上雪隧,一...
    開封第一講書人閱讀 52,156評論 1 308
  • 那天,我揣著相機(jī)與錄音员舵,去河邊找鬼脑沿。 笑死,一個胖子當(dāng)著我的面吹牛马僻,可吹牛的內(nèi)容都是我干的庄拇。 我是一名探鬼主播,決...
    沈念sama閱讀 40,743評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼韭邓,長吁一口氣:“原來是場噩夢啊……” “哼措近!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起女淑,我...
    開封第一講書人閱讀 39,659評論 0 276
  • 序言:老撾萬榮一對情侶失蹤瞭郑,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后鸭你,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體屈张,經(jīng)...
    沈念sama閱讀 46,200評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,282評論 3 340
  • 正文 我和宋清朗相戀三年袱巨,在試婚紗的時候發(fā)現(xiàn)自己被綠了阁谆。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,424評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡愉老,死狀恐怖笛厦,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情俺夕,我是刑警寧澤,帶...
    沈念sama閱讀 36,107評論 5 349
  • 正文 年R本政府宣布贱鄙,位于F島的核電站劝贸,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏逗宁。R本人自食惡果不足惜映九,卻給世界環(huán)境...
    茶點故事閱讀 41,789評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望瞎颗。 院中可真熱鬧件甥,春花似錦、人聲如沸哼拔。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽倦逐。三九已至譬正,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背曾我。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評論 1 271
  • 我被黑心中介騙來泰國打工粉怕, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人抒巢。 一個月前我還...
    沈念sama閱讀 48,798評論 3 376
  • 正文 我出身青樓贫贝,卻偏偏與公主長得像,于是被迫代替她去往敵國和親蛉谜。 傳聞我的和親對象是個殘疾皇子稚晚,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,435評論 2 359

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,262評論 25 707
  • 感受風(fēng)吹過耳邊,走在一條繁華的大街上悦陋,在這個大街蜈彼,車輛快速的行駛,摩托聲俺驶,自行車手幸逆,汽車的轟鳴聲,路邊工人施工的聲...
    seraph霸氣閱讀 225評論 0 0
  • 我破開了黑暗的夜 穿過層層的迷霧 迎著黃金色的朝暉 奔跑著 沾滿了花瓣與露珠 與風(fēng)一起起舞 我跳進(jìn)海里 追逐著水里...
    大師兄說閱讀 238評論 0 0
  • 春天 在稚嫩的泥土里 埋一粒善良的種子 用愛來澆瓘 讓它長成一棵參天大樹 上面結(jié)滿豐碩的詩句 .
    泰山寒梅閱讀 242評論 4 1
  • 1暮现、我还绘、聶好、兩三個男孩子栖袋、一個不能看清臉身高很高穿著42中校服的男孩子拍顷、一個能看清臉長相很小很精致的男孩子、一個...
    阿PP阿DD閱讀 215評論 0 0