国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android App開發(fā)的自動化測試框架UI Automator使用教程

2019-12-12 06:02:35
字體:
供稿:網(wǎng)友

Android的自動化測試有很多框架,其中ui automator是google官方提供的黑盒UI相關(guān)的自動化測試工具,(GitHub主頁:case使用java寫,今天實踐了一下官方文檔中樣例程序,其中還是有一些小問題需要總結(jié)一下的。
環(huán)境準備:
1.JDK(是的,你沒看錯,基礎(chǔ)的android開發(fā)環(huán)境必備),以及對應(yīng)的環(huán)境變量配置,不會的可以自己百度下下
2.Android Studio(IDE尊崇個人意愿)
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.配置完以后,測試一下,在命令行下輸入ant -version,如果顯示你所安裝的ant版本信息,證明環(huán)境變量配置成功

使用流程
1、使用ADT創(chuàng)建一個java的項目
在創(chuàng)建項目的時候要加上JUnit與你使用的Android platforms中對應(yīng)的android.jar與uiautomator.jar

2016711171444606.png (638×286)

2、新建一個包(我這里就只叫com)
3、再這個包下創(chuàng)建一個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();    }  }
4、使用ant工具生成build.xml
我這里在使用ADT自已的ant插件時提示
build.xml:26: Class not found: javac1.8

網(wǎng)上查了查,是插件與我java環(huán)境不符,下載最新的ant插件就可以了http://ant.apache.org/bindownload.cgi

2016711171556435.png (540×271)

下載這個tar.gz包,解壓,然后將apache-ant-1.9.4/bin目錄添加到環(huán)境變量PATH中
然后cmd到android sdk的tools目錄,使用andrlid list命令,記住你將要在模擬器中運行的(也是你剛剛導(dǎo)入android.jar與uiautomator.jar包時所在的platforms)
在cmd下使用

android create uitest-project -n <name> -t <android-sdk-ID> -p <path>

-n 為生成的jar包名稱,自已任意定義,
-t 為上面查看到的值,我這里是1
-p 為輸出路徑,這里就是剛才創(chuàng)建的java項目所在的路徑

android create uitest-project -n AutoRunner -t 1 -p D:/myAndroidStudy/androidTest

然后再cmd進入D:/myAndroidStudy/androidTest,使用ant build命令生成AutoRunner.jar文件
5、將這個AutoRunner.jar文件push到模擬器中

adb push AutoRunner.jar /data/local/tmp

6、使用

adb shell uiautomator runtest AutoRunner.jar 

主站蜘蛛池模板:
曲阜市|
盐池县|
汤原县|
固阳县|
塘沽区|
舒兰市|
嘉峪关市|
竹山县|
金川县|
讷河市|
连城县|
蕲春县|
莱阳市|
锡林郭勒盟|
枣强县|
玛曲县|
高淳县|
阳曲县|
江油市|
密山市|
阜康市|
庆元县|
锦屏县|
肇源县|
浦北县|
麦盖提县|
松溪县|
保靖县|
堆龙德庆县|
察雅县|
绿春县|
万山特区|
内乡县|
赣榆县|
清原|
桐庐县|
黔江区|
蓝田县|
达州市|
达日县|
平定县|