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”
0x01 依次選擇 “Android”--> “Empty Module”,然后點擊 “Next”
0x02 配置項目信息(項目名材原,路徑等)沸久,然后點擊“Finish”
0x03 添加測試依賴庫
點擊界面右上角的圖標(biāo),如下圖所示余蟹,也可以通過點擊 “File”-->“Project Structure...”完成此操作
依次選擇“Libraries”-->“+”-->“Java”
在文件選擇窗口中選擇 [你的 Android sdk 目錄 ]/platforms/android-xx
文件夾下的 android.jar
和 uiautomotor.jar
卷胯,然后點擊“OK”
在彈出的“Choose Modules” 窗口中選擇我們剛創(chuàng)建的工程,這里為 UiAutoTest
威酒,點擊“OK”
如果一切正常的話窑睁,你會看到如下界面,點擊“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)建過程中沒有出錯暖眼,你應(yīng)該會看到如下圖中所示的bin
目錄及其中的 UiAutoTest.jar
文件惕耕。
怎么樣,很簡單吧诫肠?趕快體驗一下吧司澎!