Android App開(kāi)發(fā)的自動(dòng)化測(cè)試框架UI Automator使用教程
Android的自動(dòng)化測(cè)試有很多框架,其中ui automator是google官方提供的黑盒UI相關(guān)的自動(dòng)化測(cè)試工具,(GitHub主頁(yè):case使用java寫,今天實(shí)踐了一下官方文檔中樣例程序,其中還是有一些小問(wèn)題需要總結(jié)一下的。
環(huán)境準(zhǔn)備:
1.JDK(是的,你沒(méi)看錯(cuò),基礎(chǔ)的android開(kāi)發(fā)環(huán)境必備),以及對(duì)應(yīng)的環(huán)境變量配置,不會(huì)的可以自己百度下下
2.Android Studio(IDE尊崇個(gè)人意愿)
3.android SDK以及配置
4.ANT(主要用于build我們的腳本,生成jar包)
ant的搭建主要分幾步:
1.下載ant安裝文件并且解壓安裝;
2.新建系統(tǒng)環(huán)境變量ANT_HOME,參數(shù)值是你的ant安裝目錄;
3.在Path環(huán)境變量中添加ant安裝目錄的bin文件夾,比如我的就是C:\cod\apache-ant-1.9.6\bin
4.配置完以后,測(cè)試一下,在命令行下輸入ant -version,如果顯示你所安裝的ant版本信息,證明環(huán)境變量配置成功
使用流程
1、使用ADT創(chuàng)建一個(gè)java的項(xiàng)目
在創(chuàng)建項(xiàng)目的時(shí)候要加上JUnit與你使用的Android platforms中對(duì)應(yīng)的android.jar與uiautomator.jar

2、新建一個(gè)包(我這里就只叫com)
3、再這個(gè)包下創(chuàng)建一個(gè)class,輸入以下java代碼,代碼全是官方文檔上的代碼,除了最上面的package
package com;
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 Runer extends UiAutomatorTestCase {
public void testDemo() throws UiObjectNotFoundException {
// Simulate a short press on the HOME button.
getUiDevice().pressHome();
// We're now in the home screen. Next, we want to simulate
// a user bringing up the All Apps screen.
// If you use the uiautomatorviewer tool to capture a snapshot
// of the Home screen, notice that the All Apps button's
// content-description property has the value “Apps”. We can
// use this property to create a UiSelector to find the button.
UiObject allAppsButton = new UiObject(new UiSelector()
.description("Apps"));
// Simulate a click to bring up the All Apps screen.
allAppsButton.clickAndWaitForNewWindow();
// In the All Apps screen, the Settings app is located in
// the Apps tab. To simulate the user bringing up the Apps tab,
// we create a UiSelector to find a tab with the text
// label “Apps”.
UiObject appsTab = new UiObject(new UiSelector()
.text("Apps"));
// Simulate a click to enter the Apps tab.
appsTab.click();
// Next, in the apps tabs, we can simulate a user swiping until
// they come to the Settings app icon. Since the container view
// is scrollable, we can use a UiScrollable object.
UiScrollable appViews = new UiScrollable(new UiSelector()
.scrollable(true));
// Set the swiping mode to horizontal (the default is vertical)
appViews.setAsHorizontalList();
// Create a UiSelector to find the Settings app and simulate
// a user click to launch the app.
UiObject settingsApp = appViews.getChildByText(new UiSelector()
.className(android.widget.TextView.class.getName()),
"Settings");
settingsApp.clickAndWaitForNewWindow();
// Validate that the package name is the expected one
UiObject settingsValidation = new UiObject(new UiSelector()
.packageName("com.android.settings"));
assertTrue("Unable to detect Settings",
settingsValidation.exists());
UiObject reportBug = new UiObject(new UiSelector().text("Sound"));
reportBug.clickAndWaitForNewWindow();
UiObject soundValidation = new UiObject(new UiSelector()
.text("Volumes"));
assertTrue("Unable to detect Sound",
soundValidation.exists());
getUiDevice().pressHome();
}
}
我這里在使用ADT自已的ant插件時(shí)提示
build.xml:26: Class not found: javac1.8
網(wǎng)上查了查,是插件與我java環(huán)境不符,下載最新的ant插件就可以了http://ant.apache.org/bindownload.cgi

下載這個(gè)tar.gz包,解壓,然后將apache-ant-1.9.4\bin目錄添加到環(huán)境變量PATH中
然后cmd到android sdk的tools目錄,使用andrlid list命令,記住你將要在模擬器中運(yùn)行的(也是你剛剛導(dǎo)入android.jar與uiautomator.jar包時(shí)所在的platforms)
在cmd下使用
android create uitest-project -n <name> -t <android-sdk-ID> -p <path>
-n 為生成的jar包名稱,自已任意定義,
-t 為上面查看到的值,我這里是1
-p 為輸出路徑,這里就是剛才創(chuàng)建的java項(xiàng)目所在的路徑
android create uitest-project -n AutoRunner -t 1 -p D:\myAndroidStudy\androidTest
然后再cmd進(jìn)入D:\myAndroidStudy\androidTest,使用ant build命令生成AutoRunner.jar文件
5、將這個(gè)AutoRunner.jar文件push到模擬器中
adb push AutoRunner.jar /data/local/tmp
6、使用
adb shell uiautomator runtest AutoRunner.jar –c com.Runer
使Runer類運(yùn)行

我的代碼里又在官方基礎(chǔ)上多了一個(gè)點(diǎn)擊”sound”的操作與點(diǎn)擊Home鍵操作
UiObject reportBug = new UiObject(new UiSelector().text("Sound"));
reportBug.clickAndWaitForNewWindow();
UiObject soundValidation = new UiObject(new UiSelector()
.text("Volumes"));
assertTrue("Unable to detect Sound",
soundValidation.exists());
getUiDevice().pressHome();
image

這個(gè)其實(shí)也只是一個(gè)簡(jiǎn)單的玩具代碼,沒(méi)有什么意義,但是官方作為一個(gè)引導(dǎo),其中也使用了一些最常見(jiàn)的接口。以后再深入的學(xué)習(xí)uiautomator
總結(jié)
優(yōu)點(diǎn):
1.可以對(duì)所有操作進(jìn)行自動(dòng)化,操作簡(jiǎn)單;
2.不需要對(duì)被測(cè)程序進(jìn)行重簽名,且,可以測(cè)試所有設(shè)備上的程序,比如~某APP,比如~撥號(hào),比如~發(fā)信息等等
3.對(duì)于控件定位,要比robotium簡(jiǎn)單一點(diǎn)點(diǎn)
缺點(diǎn):
1.uiautomator需要android level 16以上才可以使用,因?yàn)樵趌evel 16及以上的API里面才帶有uiautomator工具
2.如果想要使用resource-id定位控件,則需要level 18及以上才可以
3.對(duì)中文支持不好(不代表不支持,第三方j(luò)ar可以實(shí)現(xiàn))
4.個(gè)人感覺(jué),控件定位不如robotium那樣層級(jí)分明,僅僅個(gè)人感覺(jué),用戶行為注入還是和插樁有點(diǎn)點(diǎn)區(qū)別的
相關(guān)文章
Android實(shí)現(xiàn)Activity界面切換添加動(dòng)畫(huà)特效的方法
這篇文章主要介紹了Android實(shí)現(xiàn)Activity界面切換添加動(dòng)畫(huà)特效的方法,非常實(shí)用的技巧,需要的朋友可以參考下2014-08-08
Android 滑動(dòng)Scrollview標(biāo)題欄漸變效果(仿京東toolbar)
這篇文章主要介紹了Android 滑動(dòng)Scrollview標(biāo)題欄漸變效果(仿京東toolbar),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
Android文本視圖TextView實(shí)現(xiàn)聊天室效果
這篇文章主要介紹了Android文本視圖TextView實(shí)現(xiàn)聊天室效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
不依賴于Activity的Android全局懸浮窗的實(shí)現(xiàn)
在Android應(yīng)用開(kāi)發(fā)中,經(jīng)常要遇到做全局懸浮窗的效果,本文的內(nèi)容主要是如何不依賴于Activity的全局懸浮窗的實(shí)現(xiàn)及原理,有需要的可以參考。2016-07-07
Android編程實(shí)現(xiàn)懸浮窗獲取并顯示當(dāng)前內(nèi)存使用量的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)懸浮窗獲取并顯示當(dāng)前內(nèi)存使用量的方法,涉及Android針對(duì)窗口及內(nèi)存的相關(guān)操作技巧,需要的朋友可以參考下2017-07-07
Android中利用zxing實(shí)現(xiàn)自己的二維碼掃描識(shí)別詳解
這篇文章主要給大家介紹了關(guān)于Android中利用zxing實(shí)現(xiàn)自己的二維碼掃描識(shí)別的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用zxing具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-09-09
Android自定義View的三種實(shí)現(xiàn)方式總結(jié)
本篇文章主要介紹了Android自定義View的三種實(shí)現(xiàn)方式總結(jié),非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-02-02
Android實(shí)現(xiàn)輸入法彈出時(shí)把布局頂上去和登錄按鈕頂上去的解決方法
這篇文章主要介紹了Android實(shí)現(xiàn)輸入法彈出時(shí)把布局頂上去和登錄按鈕頂上去的解決方法,需要的朋友可以參考下2017-11-11
Android項(xiàng)目遷移到AndroidX的方法步驟
這篇文章主要介紹了Android項(xiàng)目遷移到AndroidX的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12

