Android UIAutomator浅谈
  简介
  Uiautomator是谷歌推出的,用于UI自动化测试的工具,也是普通的手工测试,点击每个控件元素看看输出的结果是否符合预期。比如登陆界面分别输入正确和错误的用户名密码然后点击登陆按钮看看是否能否登陆以及是否有错误提示等。
  注意:UI Automator测试框架是基于instrumentation的API,运行在Android JunitRunner 之上,同时UI Automator Test只运行在Android 4.3(API level 18)以上版本。
  准备
  集成UI Automator,首先需要保证App项目已经依赖了Gradle Testing。然后在gradle中添加如下依赖即可。
  dependencies {
  androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
  }
  UI元素查找
  在Android开发中,可以种findViewById来找到相应的控件,但是UI Automator是用来测试界面的,如果也是用相同的方式,那么则会依赖很大。UI Automator使用一种类似的方式来查找元素。UI Automator是根据元素的text、hint、contentDescription等属性进行查找的,为了使UI Automator能够获取到以上的属性值,请测试的UI界面是可以访问的,同时UI Automator也是可以访问这些控件元素。
  可以使用uiautomatorviewer,来获取界面元素的层次关系以及各个元素的属性,然后UI Automator可以根据这些属性信息查找到对应的控件元素。
  获取步骤:
  * 启动设备上的App
  * 把设备连接到开发机器上
  * 打开Android sdk目录, <android-sdk>/tools/ 。
  * 启动 uiautomatorviewer
  然后可以操作 uiautomatorviewer
  * 点击Device ScreenShot,稍等一会,获取到了屏幕的快照,界面右侧是当前界面的布局结构以及属性信息。
  确保UI可以访问上面介绍过,UI Automator是根据元素的text、hint、contentDescription等属性进行查找的,所以需要尽可能给布局中的元素添加上面的属性。有时候程序员需要自定义一些控件,那么请实现 AccessibilityNodeProvider ,以确保可以正常使用。
  访问UI控件
  UI Automator 提供 UiDevice 类,这个类提供一些方法,可以获取设备的一些状态和属性,同时可以执行一系列动作来操作设备。下面是一个示例,演示怎么样货到 UiDevice 对象,以及按下Home键。
import org.junit.Before;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.Until;
...
@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 18)
public class ChangeTextBehaviorTest {
private static final String BASIC_SAMPLE_PACKAGE
= "com.example.android.testing.uiautomator.BasicSample";
private static final int LAUNCH_TIMEOUT = 5000;
private static final String STRING_TO_BE_TYPED = "UiAutomator";
private UiDevice mDevice;
@Before
public void startMainActivityFromHomeScreen() {
// Initialize UiDevice instance
mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
// Start from the home screen
mDevice.pressHome();
// Wait for launcher
final String launcherPackage = mDevice.getLauncherPackageName();
assertThat(launcherPackage, notNullValue());
mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
LAUNCH_TIMEOUT);
// Launch the app
Context context = InstrumentationRegistry.getContext();
final Intent intent = context.getPackageManager()
.getLaunchIntentForPackage(BASIC_SAMPLE_PACKAGE);
// Clear out any previous instances
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent);
// Wait for the app to appear
mDevice.wait(Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(0)),
LAUNCH_TIMEOUT);
}
}
  然后可以使用 findObject() 方法来获取到界面的UI控件,如下所示,在一个设备上面找出符合规则的UI元素,然后再执行一些列动作。
UiObject cancelButton = mDevice.findObject(new UiSelector()
.text("Cancel"))
.className("android.widget.Button"));
UiObject okButton = mDevice.findObject(new UiSelector()
.text("OK"))
.className("android.widget.Button"));
// Simulate a user-click on the OK button, if found.
if(okButton.exists() && okButton.isEnabled()) {
okButton.click();
}
  指定一个选择器
  有时候,需要访问一个特定的UI组件,可以使用 UiSelector 。这个类提供一些列的方法,帮助找到特定的UI组件。
  当有多个符合条件UI元素被查找到时候,第一个元素会不是使用。可以传递的一个UiSelector到构造方法,相当于链式访问。如果没有元素被找到,那么会抛出 UiAutomatorObjectNotFoundException 异常。
  也可以使用 childSelector() 来帅选多个UiSelector对象,如下所示,ListView的子元素中有很多是相同的,可以根据这个方法,选择某一个子元素。
  UiObject appItem = new UiObject(new UiSelector()
  .className("android.widget.ListView")
  .instance(1)
  .childSelector(new UiSelector()
  .text("Apps")));
  上面的查找还是很复杂,我们可以根据ResourceId 来替代文本查找的方式。文本找到的方式很脆弱,而且很容使测试失败,而且当语言环境变化了,文本查找需要对照多个翻译版本的文本了。
  执行动作
  当我们根据文本查找找到对应的文本元素后,我们可以随心所欲的进行操作了。
  click() 点击
  dragTo() 拖动当前元素
  setText() 设置文本
  swipeUp() 向上滑动,同理也有向下、向左、向右,swipeDown、swipeLeft、swipeRight。
  UI Automator也可以提供一个Context,这样可以方便发送一个Intent或者是启动一个Activity。
public void setUp() {
// Launch a simple calculator app
Context context = getInstrumentation().getContext();
Intent intent = context.getPackageManager()
.getLaunchIntentForPackage(CALC_PACKAGE);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
// Clear out any previous instances
context.startActivity(intent);
mDevice.wait(Until.hasObject(By.pkg(CALC_PACKAGE).depth(0)), TIMEOUT);
}
  集合操作
  可以使用 UiCollection 来模拟用户对一组UI控件进行操作,比如有时候界面上有个Listview。为了创建一个 UiCollection 对象,可以指定一个UiSelector搜索这个UI容器上满足这个条件的所有子元素。
  下面代码演示如何在一个一组控件中进行操作。
UiCollection videos = new UiCollection(new UiSelector()
.className("android.widget.FrameLayout"));
// Retrieve the number of videos in this collection:
int count = videos.getChildCount(new UiSelector()
.className("android.widget.LinearLayout"));
// Find a specific video and simulate a user-click on it
UiObject video = videos.getChildByText(new UiSelector()
.className("android.widget.LinearLayout"), "Cute Baby Laughing");
video.click();
// Simulate selecting a checkbox that is associated with the video
UiObject checkBox = video.getChild(new UiSelector()
.className("android.widget.Checkbox"));
if(!checkBox.isSelected()) checkbox.click();
  操作可以滚动的UI元素
  可以使用 UiScrollable 来模拟用户对界面的滚动操作(水平、垂直),这个技术可以帮我们搞定,当某个界面没有显示的时候,我们可以滚动界面,把那个界面显示出来。
  UiScrollable settingsItem = new UiScrollable(new UiSelector()
  .className("android.widget.ListView"));
  UiObject about = settingsItem.getChildByText(new UiSelector()
  .className("android.widget.LinearLayout"), "About tablet");
  about.click();