Android UiAutomator2.0

蹉跎莫遣韶光老,人生唯有读书好。这篇文章主要讲述Android UiAutomator2.0相关的知识,希望能为你提供帮助。
一、环境搭建
JDK(java环境)、SDK(adb appt环境),这两个已经不想再叙述了直接看详见-->
android studio 安装,下载地址:https://developer.android.google.cn/studio/index.html
安装完成,打开后大概是这个样子的:

Android UiAutomator2.0

文章图片

二、新建测试工程
1.开始新建一个工程:点击上图的“Start a new Android Studio project ”
2.进入创建工程界面,设置app的名称、包名,及工程路径,点击 “Next”
Android UiAutomator2.0

文章图片

3.选择平台类型及sdk的最小版本,点击 “Next”
Android UiAutomator2.0

文章图片

4.选择Empty Activity,点击“Next”;选择No Activity,点击“Finish”   
Android UiAutomator2.0

文章图片

5.工程创建完成。(默认是Android目录模式,且有一个默认的Module【app】)
这里附上以下界面的较为详细的介绍 > > > > >
Android UiAutomator2.0

文章图片

三、编写脚本
在编写脚本前说明几点:(实例并非以上新建的工程目录,但并不影响此教程)
a:脚本实例中将测试脚本写在main中,以便后续build apk后,能在CTS框架中运行。
b:需先配置AndroidManifest.xml文件、build.gradle(Module:app)文件、以及settings.gradle文件。
【Android UiAutomator2.0】1.settings.gradle文件内容
Android UiAutomator2.0

文章图片

2.AndroidManifest.xml文件中添加Instrumentation标签
Android UiAutomator2.0

文章图片

3.build.gradle(Module:app)文件中添加测试依赖的包。文件代码如下:
Android UiAutomator2.0

文章图片
Android UiAutomator2.0

文章图片
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
Android UiAutomator2.0

文章图片
  4.接下来就是代码的编写了,这里贴出一个例子
Android UiAutomator2.0

文章图片
Android UiAutomator2.0

文章图片
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 zzw 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 : 打开设置-> 点击语言& 输入法-> 点击语言-> 切换语言(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 "语言和输入法" UiObject languageInput = mDevice.findObject(new UiSelector().textMatches("Languages & input|语言和输入法")); settingList.scrollIntoView(languageInput); //UiObject languageInput = settingList.getChildByText(new UiSelector().textMatches("Languages & input|语言和输入法") //,Locale.getDefault().getLanguage().equals("en")?"Languages & input":"语言和输入法"); // Click "Languages & input" or "语言和输入法" 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.首先来看下在哪里运行测试,及怎么运行测试。步骤见图解
Android UiAutomator2.0

文章图片

2.编辑运行测试的配置
Android UiAutomator2.0

文章图片

3.运行测试
Android UiAutomator2.0

文章图片

实际上测试执行是这样的:(1~4:安装主程序和测试程序; 5:执行测试)
adb push E:\\Androids\\AndroidStudioProjects\\TestDemo\\calculator\\build\\outputs\\apk\\calculator-debug.apk /data/local/tmp/ com.zzw.testdemoadb shell pm install-r /data/local/tmp/com.zzw.testdemo adb push E:\\Androids\\AndroidStudioProjects\\TestDemo\\calculator\\build\\outputs\\apk\\calculator-debug-androidTest.apk /data/local/tmp/ com.zzw.testdemo.testadb shell pm install -r /data/local/tmp/com.zzw.testdemo.testadb shell am instrument -r -e debug false -e class com.zzw.testdemo.TestPermission2#test01_a com.zzw.testdemo.test/android.support.test.runner.AndroidJUnitRunner

 
五、生成报告
Android UiAutomator2.0

文章图片

六、使用CTS框架运行测试脚本
要使用CTS测试框架进行测试生成报告:需要将测试脚本写在main中(编写脚本处也提到过),然后将生成的主程序 app-debug.apk放置CTS的testcase目录下,配置完成后即可运行测试。详见 《Android UiAutomator - CTS Frame》cts 7.0以上部分=> > > >

    推荐阅读