Android UiAutomator2.0

來源:互聯網
上載者:User

標籤:tca   stp   開始   title   graphics   tomato   one   time   最小   

一、環境搭建

  JDK(java環境)、SDK(adb appt環境),這兩個已經不想再敘述了直接看詳見-->

  android studio 安裝,:https://developer.android.google.cn/studio/index.html

安裝完成,開啟後大概是這個樣子的:

二、建立測試工程

1.開始建立一個工程:點擊的“Start a new Android Studio project ”

2.進入建立工程介面,設定app的名稱、包名,及工程路徑,點擊 “Next”

3.選擇平台類型及sdk的最小版本,點擊 “Next”

4.選擇Empty Activity,點擊“Next”;選擇No Activity,點擊“Finish”  

5.工程建立完成。(預設是Android目錄模式,且有一個預設的Module【app】)

三、編寫指令碼

在編寫指令碼前說明幾點:(執行個體並非以上建立的工程目錄,但並不影響此教程)

  a:指令碼執行個體中將測試指令碼寫在main中,以便後續build apk後,能在CTS架構中運行。

  b:需先配置AndroidManifest.xml檔案、build.gradle(Module:app)檔案、以及settings.gradle檔案。

1.settings檔案內容

2.AndroidManifest.xml檔案中添加Instrumentation標籤

3.build.gradle(Module:app)檔案中添加測試依賴的包。檔案代碼如下:

apply plugin: ‘com.android.application‘android {    compileSdkVersion 26    buildToolsVersion "26.0.1"    defaultConfig {        applicationId "com.zzw.testdemo"        minSdkVersion 24        targetSdkVersion 26        versionCode 1        versionName "1.0"        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"    }    buildTypes {        release {            minifyEnabled false            proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘        }    }}dependencies {    compile fileTree(dir: ‘libs‘, include: [‘*.jar‘])    androidTestCompile(‘com.android.support.test.espresso:espresso-core:2.2.2‘, {        exclude group: ‘com.android.support‘, module: ‘support-annotations‘    })    compile ‘com.android.support:appcompat-v7:26.+‘    testCompile ‘junit:junit:4.12‘    compile ‘com.android.support.test:runner:0.5‘    // Set this dependency to use JUnit 4 rules    compile ‘com.android.support.test:rules:0.5‘    // Set this dependency to build and run Espresso tests    compile ‘com.android.support.test.espresso:espresso-core:2.2.2‘    // Set this dependency to build and run UI Automator tests    compile ‘com.android.support.test.uiautomator:uiautomator-v18:2.1.2‘    // Add uiTestHelper    compile project(‘:uiTestHelper‘)}
View Code

 4.接下來就是代碼的編寫了,這裡貼出一個例子

package com.zzw.testdemo;import android.graphics.Rect;import android.os.Environment;import android.os.SystemClock;import android.support.test.InstrumentationRegistry;import android.support.test.uiautomator.UiDevice;import android.support.test.uiautomator.UiObject;import android.support.test.uiautomator.UiScrollable;import android.support.test.uiautomator.UiSelector;import org.junit.After;import org.junit.Before;import org.junit.Ignore;import org.junit.Test;import java.io.File;import java.io.IOException;import java.util.Locale;/** * Created by zhangying on 2017/12/19. */public class TestPermission2 {    private UiDevice mDevice;    private static long LONG_TIMEOUT = 5000;    private static long SHORT_TIMEOUT = 3000;    private static String TEST_PACKAGE_NAME ="com.android.settings";    private static String TEST_MAIN_ACTIVITY="com.android.settings/.Settings";    @Before    public void setUp() throws IOException {        mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());        mDevice.executeShellCommand("pm clear "+TEST_PACKAGE_NAME);    }    @After    public void tearDown(){        mDevice.pressHome();    }    /**     * Case : 開啟設定->點擊語言&IME->點擊語言->切換語言(en || cn)     *     */    @Test    public void test01_a()throws Throwable {        try {            // Launch settings application            mDevice.executeShellCommand("am start -n " + TEST_MAIN_ACTIVITY);            // Sleep 5s            SystemClock.sleep(LONG_TIMEOUT);            // Setting list interface            UiScrollable settingList = new UiScrollable(new UiSelector().resourceId("com.android.settings:id/dashboard_container"));            // Sliding lookup "Languages & input" or "語言和IME"            UiObject languageInput = mDevice.findObject(new UiSelector().textMatches("Languages & input|語言和IME"));            settingList.scrollIntoView(languageInput);            //UiObject languageInput = settingList.getChildByText(new UiSelector().textMatches("Languages & input|語言和IME")            //  ,Locale.getDefault().getLanguage().equals("en")?"Languages & input":"語言和IME");            // Click "Languages & input" or "語言和IME"            languageInput.clickAndWaitForNewWindow();            // Click "Languages" or "語言"            mDevice.findObject(new UiSelector().textMatches("Languages|語言")).clickAndWaitForNewWindow();            // Add language control            UiObject addLanguage = mDevice.findObject(new UiSelector().resourceId("com.android.settings:id/add_language"));            // Language list interface control            UiObject dragList = mDevice.findObject(new UiSelector().resourceId("com.android.settings:id/dragList"));            // The first language displayed rect            Rect rect = dragList.getChild(new UiSelector().className("android.widget.RelativeLayout").index(0)).getBounds();            // Language cn control            UiObject cn = dragList.getChild(new UiSelector().textMatches(".*中文(中國)"));            // Language en control            UiObject en = dragList.getChild(new UiSelector().text("English (United States)"));            // Add language list interface control            UiScrollable findList = new UiScrollable(new UiSelector().className("android.widget.ListView"));            // If language cn and en exists in the language list interface            if (cn.exists() && en.exists()) {                UiObject first = dragList.getChild(new UiSelector().className("android.widget.RelativeLayout").index(0))                        .getChild(new UiSelector().className("android.widget.TextView"));                if (first.getText().equals("English (United States)")) {                    // if the first language displayed "English (United States)" , Switch to cn                    cn.dragTo(rect.centerX(), rect.top, 500);                } else if (first.getText().matches(".*中文(中國)")) {                    // if it displayed ".*中文(中國)" ,  Switch to en                    en.dragTo(rect.centerX(), rect.top, 500);                } else {                    // else none of them , Switch to en                    en.dragTo(rect.centerX(), rect.top, 500);                }            } else if (cn.exists() && !en.exists()) {                // if cn exists and en not exists , add en and switch to en                addLanguage.clickAndWaitForNewWindow();                findList.getChildByText(new UiSelector().text("English"), "English").clickAndWaitForNewWindow();                findList.getChildByText(new UiSelector().text("United States"), "United States").clickAndWaitForNewWindow();                SystemClock.sleep(SHORT_TIMEOUT);                en.dragTo(rect.centerX(), rect.top, 500);            } else if (en.exists() && !cn.exists()) {                // if en exists and cn not exists , add cn and switch to cn                addLanguage.clickAndWaitForNewWindow();                UiObject chinese = mDevice.findObject(new UiSelector().textMatches(".*中文"));                findList.scrollIntoView(chinese);                chinese.clickAndWaitForNewWindow();                UiObject china = mDevice.findObject(new UiSelector().text("中國"));                if (china.exists()) {                    china.clickAndWaitForNewWindow();                }                SystemClock.sleep(SHORT_TIMEOUT);                cn.dragTo(rect.centerX(), rect.top, 500);            } else {                // else none of them , add en and switch to en                addLanguage.clickAndWaitForNewWindow();                findList.getChildByText(new UiSelector().text("English"), "English").clickAndWaitForNewWindow();                findList.getChildByText(new UiSelector().text("United States"), "United States").clickAndWaitForNewWindow();                SystemClock.sleep(LONG_TIMEOUT);                en.dragTo(rect.centerX(), rect.top, 500);            }            SystemClock.sleep(LONG_TIMEOUT);        }catch (Throwable e){            // if error exist ,print stack trace log and screenshot to sdcard/test01_a.png            e.printStackTrace();            mDevice.takeScreenshot(new File(Environment.getExternalStorageDirectory(),this.getClass().getName()+".png"));        }finally {            mDevice.pressBack();            mDevice.pressBack();            mDevice.pressBack();        }    }    @Test    public void test02_a() {    }    @Ignore    public void test03_a() {    }}
View Code

四、運行測試

1.首先來看下在哪裡運行測試,及怎麼運行測試。步驟見圖解

2.編輯運行測試的配置

3.運行測試

實際上測試執行是這樣的:(a~d:安裝主程式和測試程式; e:執行測試)

a. adb push E:\Androids\AndroidStudioProjects\TestDemo\calculator\build\outputs\apk\calculator-debug.apk /data/local/tmp/com.zzw.testdemo
b. adb shell pm install -r "/data/local/tmp/com.zzw.testdemo"
c. adb push E:\Androids\AndroidStudioProjects\TestDemo\calculator\build\outputs\apk\calculator-debug-androidTest.apk /data/local/tmp/com.zzw.testdemo.test
d. adb shell pm install -r "/data/local/tmp/com.zzw.testdemo.test"
e. adb shell am instrument -w -r -e debug false -e class com.zzw.testdemo.TestPermission2#test01_a com.zzw.testdemo.test/android.support.test.runner.AndroidJUnitRunner

五、產生報告

六、使用CTS架構運行測試指令碼

  要使用CTS測試架構進行測試產生報告:需要將測試指令碼寫在main中(編寫指令碼處也提到過),然後將產生的主程式 app-debug.apk放置CTS的testcase目錄下,配置完成後即可運行測試。詳見 《Android UiAutomator - CTS Frame》cts 7.0以上部分=>>>>

Android UiAutomator2.0

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.