您的位置:首页 > 产品设计 > UI/UE

UIAutomator

2016-06-15 15:08 459 查看

简介

[align=center]UiAutomator 是 Google 在 Android4.1 推出的一套黑盒 UI 自动化测试框架,用于做 UI 自动化的框架,主要模拟人来操作手机,如点击、长按、滑动、按键、文本输入等操作。UiAutomator 通过组件的属性如 ID、文本、描述、类名、实例等来查询定位到相应的组件,再注入各种模式操作事件来达到模拟人操作手机的目的。[/align]


创建UIAutomator2.0 的工程

  1、创建一个
android 工程

  我们打开 AS,创建一个空应用:

  2、添加依赖和测试代码

  先在 build.grade 里面添加依赖。

<span style="font-size:18px;">apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
applicationId "com.hujc.androidtest"
minSdkVersion 18
targetSdkVersion 23
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'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.0.1'

androidTestCompile 'com.android.support.test:runner:0.4'
// Set this dependency to use JUnit 4 rules
androidTestCompile 'com.android.support.test:rules:0.4'
// Set this dependency to build and run Espresso tests
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
// Set this dependency to build and run UI Automator tests
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
}
</span>


3、在
androidTest 下面添加测试代码

package com.hujc.androidtest;

import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.Until;
import android.test.InstrumentationTestCase;

/**
* @author hujc on 2016/6/15
*/
public class MainUiautomatorTest extends InstrumentationTestCase {

private UiDevice device;

@Override
public void setUp() throws Exception {
super.setUp();

device = UiDevice.getInstance(getInstrumentation());

// Perform a short press on the HOME button
device.pressHome();

// Bring up the default launcher by searching for
// a UI component that matches the content-description for the launcher button
UiObject2 allAppsButton = device.findObject(By.desc("Apps"));

// Perform a click on the button to bring up the launcher
allAppsButton.click();

device.wait(Until.hasObject(By.text("Calculator")), 3000);
UiObject2 androidTest = device.findObject(By.text("Calculator"));
androidTest.click();
}

public void testCalculator() throws Exception {

// Wait till the Calculator's buttons are on the screen
device.wait(Until.hasObject(By.text("7")), 3000);

// Select the button for 9
UiObject2 buttonSeven = device.findObject(By.text("7"));
buttonSeven.click();

// Select the button for *
UiObject2 buttonMultiply = device.findObject(By.desc("multiply"));
buttonMultiply.click();

// Press 7 as we are calculating 7*6
UiObject2 buttonSix = device.findObject(By.text("6"));
buttonSix.click();

// Select the button for =
UiObject2 buttonEquals = device.findObject(By.desc("equals"));
buttonEquals.click();

device.wait(Until.hasObject(By.clazz("android.widget.EditText")), 3000);
UiObject2 resultText = device.findObject(By.clazz("android.widget.EditText"));
String result = resultText.getText();
assertTrue(result.equals("42"));

}
}


  运行

  运行很简单,右击测试文件,点击运行即可。



  成功运行

总结

UIAutomator可以通过模拟人来操作手机来实现自动化黑盒测试,并且是可以跨应用的测试,使用起来还是比较方便的,网上文档资料也很多。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 自动化测试