首页 > 代码库 > Android 自动化测试(4)<uiautomator> 单元测试

Android 自动化测试(4)<uiautomator> 单元测试

      在之前的系列文章中,我介绍过用java来实现过 Android 自动化测试(1)如何安装和卸载一个应用(java)Android 自动化测试(2)根据ID查找对象(java);然后又介绍了用python语言来实现Android 自动化测试(3) 根据ID查找对象&touch&type (python)。还说过后续要写点关于单元测试和代码覆盖测试的文章。今天要介绍的就是单元测试。

     1、 概要

      做过java单元测试的同学,使用Android的单元测试比较简单,参见 如何进行Android单元测试,采用这种方式,业务逻辑上的测试在很多情况下,就已经解决了。还有一个明显的缺陷就是测试界面不方便。而对于android应用程序来说,界面占据了很重要的一个部分。

      这个时候可以使用uiautomator.jar这个类库。  这里我不详细讲具体的Android 的 uiautomator类库怎么使用。具体的使用可以参见Android UI Testing (英文版), 和 Android uiautomator 使用入门官方教程(中文版)

      2、核心类

      我主要提一下里面最重要的核心类UiDevice、UISelector和UiObject ,如何查找对象和作用于对象是测试的核心。

      UiDevice

      Represents the device state. In your tests, you can call methods on the  UiDevice  instance to check for the state of various properties, such as current orientation or display size. Your tests also can use the  UiDevice  instance to perform device level actions, such as forcing the device into a specific rotation, pressing the d-pad hardware button, or pressing the Home and Menu buttons.
      UiDevice代表设备状态。在测试时,可以调用UiDevice实例的方法来检查不同属性的状态,如当前的屏幕旋转方向货展示大小。测试代码还能使用UiDevice实例来执行设备级的操作,如强制设备横竖屏,按压d-pad硬件按钮,或按压主屏幕键和菜单键。
      获取UiDevice实例,模拟按压主屏幕键的代码如下: getUiDevice (). pressHome ();

     UiSelector 
      Represents a search criteria to query and get a handle on specific elements in the currently displayed UI. If more than one matching element is found, the first matching element in the layout hierarchy is returned as the target UiObject. When constructing a  UiSelector , you can chain together multiple properties to refine your search. If no matching UI element is found, a UiAutomatorObjectNotFoundException  is thrown. You can use the  childSelector()  method to nest multiple  UiSelector  instances. For example, the following code example shows how to specify a search to find the first  ListView  in the currently displayed UI, then search within that  ListView  to find a UI element with the text property Apps. 
      UiSelector代表一种搜索标准,可以在当前展示界面上查询和获取特定元素的句柄。若找到多于一个的匹配元素,则返回布局层次结构上的第一个匹配元素作为目标UiObject。当构造一个UiSelector对象时,可以使用链式调用多个属性来缩小查询范围。如无匹配元素,则返回异常 UiAutomatorObjectNotFoundException 。你还可以使用 childSelector()  方法来嵌套多个Uiselector实例。例如。下面的代码演示如何制定查询来定位在当前界面的第一个ListView,然后在返回的ListView内定位一个带有Apps文本属性的界面元素。 

UiObject  appItem  =   new   UiObject ( new   UiSelector () . className ( “android.widget.ListView” ). instance ( 1 ) . childSelector ( new   UiSelector (). text ( “Apps” )));
      UiObject 
      Represents a UI element. To create a  UiObject  instance, use a UiSelector that describes how to search for, or select, the UI element.
The following code example shows how to construct  UiObject  instances that represent a Cancel button and a OKbutton in your application.
UiObject代表一个UI元素。为创建一个UiObject实例,使用用来描述如何搜索、选定UI元素的UiSelector实例:
UiObject  cancelButton  =   new   UiObject ( new   UiSelector (). text ( “Cancel” )); 
UiObject  okButton  =   new   UiObject ( new   UiSelector (). text ( “OK” ));

      You can reuse the  UiObject  instances that you have created in other parts of your app testing, as needed. Note that the uiautomator test framework searches the current display for a match every time your test uses a  UiObject instance to click on a UI element or query a property.
      In the following code example, the uiautomator test framework searches for a UI element with the text property OK. If a match is found and if the element is enabled, the framework simulates a user click action on the element.You can also restrict the search to find only elements of a specific class. For example, to find matches of the  Button class:
      必要时,可以重用测试项目中已经创建的UiObject实例。注意,测试用例每次使用UiObject实例来点击UI元素或查询属性时,uiautomator测试框架会搜索当前的界面来寻找匹配。在下面的代码中,uiautomator测试框架搜索带有OK文本属性的UI元素。若发现匹配,并且该元素启用,框架会模拟用
户的在该元素上的点击操作。  

if ( okButton . exists ()   &&  okButton . isEnabled ()) {
    okButton . click ();
}

还可以限制搜索在几个特定的类中寻找元素,例如,为发现Button类的匹配:

UiObject  cancelButton  =   new   UiObject ( new   UiSelector (). text ( “Cancel” ) .className ( “android.widget.Button” )); 
UiObject  okButton  =   new   UiObject ( new   UiSelector (). text ( “OK” ) .className ( “android.widget.Button” ));


      3、 详细样例,注意类库中需要引用android.jar和uiautomator.jar包啊

package xzy.test.uiautomator;

import java.io.IOException;

import android.os.RemoteException;

import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;

public class CalTest extends UiAutomatorTestCase {

	public void testDemo() throws UiObjectNotFoundException, RemoteException {

		UiDevice device = getUiDevice();
		// 唤醒屏幕
		device.wakeUp();
		assertTrue("screenOn: can't wakeup", device.isScreenOn());
		// 回到HOME
		device.pressHome();
		sleep(1000);

		// 启动计算器App
		try {
			Runtime.getRuntime().exec(
					"am start -n com.android.calculator2/.Calculator");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		sleep(1000);
		UiObject oneButton = new UiObject(new UiSelector().text("1"));
		assertTrue("oneButton not found", oneButton.exists());
		UiObject plusButton = new UiObject(new UiSelector().text("+"));
		assertTrue("plusButton not found", plusButton.exists());

		sleep(100);

		UiObject equalButton = new UiObject(new UiSelector().text("="));
		assertTrue("equalButton not found", equalButton.exists());

		oneButton.click();
		sleep(100);
		plusButton.click();
		sleep(100);
		oneButton.click();

		equalButton.click();
		sleep(100);

		UiObject switcher = new UiObject(
				new UiSelector()
						.resourceId("com.android.calculator2:id/display"));
		UiObject result = switcher.getChild(new UiSelector().index(0));
		System.out.print("text is :" + result.getText());
		assertTrue("result != 2", result.getText().equals("2"));
	}
}


      4、总结一下

      单元测试比较简单,但是很有效,对于要做自动化测试的团队,和要提供稳定而又质量的交付,单元测试是很重要的噢 。Android已经有一套非常完善的单元测试支持了,UI测试也 OK


      5、后面会介绍一些采用 robotium 框架进行 Android Unit Test  & Android Code Coverage Test

Android 自动化测试(4)<uiautomator> 单元测试