在前面的系列文章。我与介绍java实现 Android
自己主动化測试(1)怎样安装和卸载一个应用(java)
Android
自己主动化測试(2)依据ID查找对象(java)
;然后又介绍了用python语言来实现Android 自己主动化測试(3) 依据ID查找对象&touch&type
(python)

还说过兴许要写点关于UI測试和代码覆盖測试的文章。今天要介绍的就是UI測试。

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硬件button。或按压主屏幕键和菜单键。

      获取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 UI Test &
Android Code Coverage Test

版权声明:本文博主原创文章。博客,未经同意不得转载。

Android 自己的自动化测试(4)<uiautomator>的更多相关文章

  1. Android 自己的自动化测试(5)<robotium>

    大约Android自己的自动化测试UI测试,前出台Android 自己主动化測试(4)<uiautomator>, 在android原生的单元測试框架上,利用uiautomator.jar ...

  2. Android Native App自动化测试实战讲解(上)(基于python)

    1.Native App自动化测试及Appuim框架介绍 android平台提供了一个基于java语言的测试框架uiautomator,它一个测试的Java库,包含了创建UI测试的各种API和执行自动 ...

  3. android真机自动化测试

    appium执行用例时报错问题: 问题解析: 一般该种情况都是因为来连接了多个设备,验证办法:cmd->执行adb devices  看结果是否是多个devices ,如果是这个问题,停掉多余设 ...

  4. Android 自己的自动化测试(2)依据ID查找对象(java)

    前一篇文章是写 Android 自己的自动化测试(1)如何安装和卸载应用程序(java) ,以下再探索一下假设在普通java应用程序中,依据ID来查找对象 1.类库依赖: The library de ...

  5. Android Native App自动化测试实战讲解(下)(基于python)

    6.Appuim自动化测试框架API讲解与案例实践(三) 如图1,可以在主函数里通过TestSuite来指定执行某一个测试用例: 6.1,scroll():如图2 从图3中可以看到当前页面的所有元素r ...

  6. Android Hybrid App自动化测试实战讲解(基于python)

    1.Hybrid App自动化测试概要 什么是Hybrid App? Hybrid App(混合模式移动应用)是指介于web-app.native-app这两者之间的app,兼具“Native App ...

  7. Android自动化测试工具之—UiAutomator环境配置

    1.相关软件下载: 1)JDK: 1.6及其以上版本 2)Eclipse 3)Android SDK 其中Eclipse和Android SDK已经被Google打包成ADT(Android Deve ...

  8. 【android】uiselectoer 自动化测试

    转:http://blog.csdn.net/sasoritattoo/article/details/17579739 Android自动化测试主要分为Monkeyrunner.Rubotium.U ...

  9. Appium+Robotframework实现Android应用的自动化测试-2:Windows中启动Appium和模拟器

    一.启动Appium 安装好了之后,在桌面或者菜单中找到Appium,分别双击或点击打开Appium.exe,如果一切正常,接着会出现一个Appium启动后的界面窗口,如下图所示. 1.1 Andro ...

随机推荐

  1. 用CSS/CSS3 实现 水平居中和垂直居中的完整攻略

    水平居中:行内元素解决方案 只需要把行内元素包裹在一个属性display为block的父层元素中,并且把父层元素添加如下属性即可:   .parent { text-align:center; } 水 ...

  2. 给定N个整数集合是否存在两个其和刚好为指定常数的元素

    又一次学习一遍<算法导论>,看到了这个问题: 描写叙述一个执行时间为O(nlgn)的算法,使之能在给定一个由n个整数构成的集合S和还有一个整数 X 时,推断出S中是否存在有两个其和刚好等于 ...

  3. 由查找session IP 展开---函数、触发器、包

    由查找session IP 展开---函数.触发器.包 一.userenv函数.sys_context函数 --查看当前client会话的session IP信息 SQL>select sys_ ...

  4. 学习Android之SharedPreferences使用

    效果图例如以下: 当我们想让自己的属性设置保存下来,这时就须要SharedPreferences. 上面这个小程序,音乐状态是保存下来的.使用的上一次退出的状态. 进入DDMS,data文件下的dat ...

  5. html_day3

    总结学习html的第一天 表格的结构说明 <table></table> <tr></tr> <td></td> <th& ...

  6. css-文本及其他

    <!DOCTYPE html>css7-文本和其他 text-align行内元素对齐方式,值为 左/中/右 对齐:left/right/center.test{text-align:cen ...

  7. 最近因为textview高度问题疯了疯了疯了

    1.textview有\r\n什么的就算不明白,我的文本最后一个字符是\r,结果我死活算不对,最后发现了==! NSString * str = [_messageModels[indexPath.r ...

  8. 上拉、下拉UITableView,交互式 模态弹出(自定义弹出动画)

    部分代码 InteractiveTransition 类继承NSObject: - (instancetype)initWithPresentingController:(UITableViewCon ...

  9. 写一个Windows上的守护进程(3)句柄的管理

    写一个Windows上的守护进程(3)句柄的管理 在Windows中编程,跟HANDLE打交道是家常便饭.为了防止忘记CloseHandle,我都是使用do-while-false手法: void f ...

  10. Qt之等待提示框三(QLabel进行多图片切换)

    之前分享过的等待提示框有用QMovie播放gif图片实现的,也有纯代码实现的,今天再次分享另一种实现方式,如题目所示:QLabel进行图片的切换!     进行用户登录的时候,往往都需要后台线程进行用 ...