来自:http://university.utest.com    作者:Angelos Nakulas (All Authored Courses)      译者:Elaine00

目录

  • 简介
  • 什么是 Android UI 测试?
  • 如何使用uiautomatorviewer来扫描和分析UI组件
  • 一步一步带你学uiautomatorviewer
  • 如何使用uiautomator API来创建用户界面测试
  • 源码

简介

Android UI 测试有两种测试类型, 其中一种白盒测试需要测试人员了解到应用的源码。白盒测试,测试人员可以使用为Android应用设计测试用例。第二类被称为黑盒测试,其中测试人员没有访问应用程序的源代码,但是你需要测试应用程序的行为,当它运行在一台设备。

什么是Android UI测试?

在Android用户界面测试,你要测试的应用程序如何与一个真实的用户交互。UI测试确保应用程序返回响应用户一系列操作的正确的UI输出,如键盘输入或按工具栏,菜单,对话框,图像,和其他用户界面控件。为了实现这一目标,你有两个选择。一是在真实的应用中来使用这个应用,并且尝试各种途径来发现应用的异常表现和Bugs。这种做法显然是费时,容易出错,你可以尝试有限的情况下。

另一种方案是自动化测试。自动化测试涉及到创建的程序来完成测试任务(测试用例)覆盖特定的使用场景,然后使用测试框架来自动地执行测试用例和可重复的方式。

如何使用uiautomatorviewer来扫描和分析UI组件

第一个工具我们要看到的是uiautomatorviewer,一个用来来扫描和分析Android应用程序的UI组件的GUI工具。为了使用uiautomatorviewer,你必须首先下载并安装SDK和根据指示设置ADT束Eclipse IDE。安装后,该工具存在于/tools/文件夹,您可以从命令行输入启动它:uiautomatorviewer。

但是这个工具的用途是什么?使用uiautomatorviewer,你可以检查一个应用的UI来查看应用的布局和组件以及相关的属性。这是非常重要的,特别是当你想构建自动化测试,通过了解各个部件的应用层次的布局,你可以使用uiautomator(在本课程的一部分API)来创建自动化测试。

一步一步带你学uiautomatorviewer

下面我将一步一步带教你如何使用uiautomatorviewer来查看Calculator应用的布局和组件的ID

  1. 首先通过USB接口连接你的设备到PC端:通过“Settings -> Developer options” 打开 USB调试.
  2. 打开Calculator应用。
  3. 通过命令行,输入: uiautomatorviewer (如果路径有问题 ,你可以切换到  /tools/ 目录),这个将打开uiautomatorviewer.
  4. 通过uiautomatorviewer窗体点击 “Device screenshot” (查看上面.)
  5. 一个计算机应用的打开窗口将显示出来
  6. 下面将来查看按钮“7”的ID:点击  “7” (下面第一个截图) ,查看右边的panel来找到 “resource-id” 的值
    (下面第二个截图).


    现在你知道了:

    • 数字“7”的ID ( “com.android.calculator2:id/digit7”).
    • 如上我们找到数字 “1”的ID (“com.android.calculator2:id/digit1”)
    • 找到加号“+”的ID  (“com.android.calculator2:id/plus”)
    • 找到 “=”的ID (“com.android.calculator2:id/equal”)
  7. 同时需要注意的是,当你鼠标悬停在应用程序的屏幕的右上方,uiautomatorviewer面板显示视图的层次结构和右下面板显示鼠标下的部件的更多信息。

    你现在知道如何使用uiautomator创建一个拥有简单的功能的用户界面测试。在下面的部分中,您将构建一个功能性的UI测试,可以检查应用程序当您添加数字“7”和“1”如何工作。

如何使用uiautomator API来创建用户界面测试

With uiautomatorAPI, you can create functional UI tests without the need to have access to the source code of the application. The uiautomator
is a Java library containing APIs to create customized functional UI
tests, and an execution engine to automate and run the tests.

使用uiautomator API,您可以创建功能的UI测试并且不需要访问应用程序的源代码。uiautomator 是一个包含java类库的API,可以用来创建定制功能的UI测试并且有一个自动化引擎来执行测试。

要运行uiautomator, 你必须:

      1. 测试前需要先在测试设备上面安装app.

        • 现在通过以下步骤来分析app的UI组件. 记录下你需要测试的组件的属性 (比如, resource-id, 组件的文字, etc).
      2. Create automated tests to simulate specific user interactions on your application.
          • 使用Eclipse,创建一个新的java项目(例如, “MyFirstTest” ).
          • 在项目中你需要导入uiautomator相关的类库. 右键点击项目 “Properties” select “Java build path” -> “Libraries” -> “Add library” -> “JUnit” -> “Next” -> “Finish”.

            同样可以从 “Properties” 选择“Java build path” -> “Libraries” -> “Add external JARs” ,在platforms目录下选择最新的SDK版本和 uiautomator.jar文件,android.jar文件.

          • 使用 uiautomator API, 书写代码(CalculatorTest.java)来测试 Calculator应用.在uiautomator API
            下面有很多的java类,你可以使用这些类来写测试用例.你写的 java测试类
            (CalculatorTest.java) 必须继承UiAutomatorTestCase,
            UiAutomatorTestCase是一个提供多种有用方法的基础类,你可以使用API的类来写测试方法测试你的程序如何工作。

        注:API中有很多类,但在API中三个最重要的类是:

      3. UiDevice-这类表示测试程序开始的时候必须调用getuidevice()这个方法。当你有一个设备的引用,你可以做很多事情了。例如,您可以使用getuidevice().takescreenshot(storepath)实现设备的截图。你可以使用getUiDevice().pressHome()来点击“Home” ;或getUiDevice().sleep()来使得设备休眠了

        UiSelector-
        通过这个类,你可以定义你需要测试的布局元素,筛选属性例如as text value,
        content-description, class名字和状态。例如,获取带有文字 “Bluetooth”的控件 , UiObject button = new UiObject(new UiSelector().text(“Bluetooth”)); 你也可以选择一个组件通过类名,描述和ID。

        UiObject-
        这个类表示的是简单的UI组件,一旦引用,你可以通过点击代码:
        UiObject seven = new UiObject(new UiSelector().resourceId(“com.android.calculator2:id/digit7″)); seven.click();, 你可以读取文字:seven.getText();, 或者你可以设置文字, 但是需要先清除文字之后再设置 seven.setText();.

      4. 编译你的测试用例为一个JAR文件,并且把它安装在您的测试装置与您的应用程序。

        在写完测试场景的代码之后,你必须按照以下步骤来 编译你的代码,创建jar文件,并且把文件导入设备。
        1. 查看/sdk/tools/目录. (例如: cd /home/angelos/Desktop/adt-bundle-linux-x86-20131030/sdk/tools/ )
        2. 输入 /tools/android create uitest-project -n -t 5 -p
        (例如:
        android create uitest-project -n MyFirstTest -t 5 -p
        /home/angelos/workspace/MyFirstTest/) 注:

        为了寻找参数t的正确值,列出Android目标列表。
        3. Windows 输入: set ANDROID_HOME= <path_to_your_sdk>.
        Linux 输入: export ANDROID_HOME= <path_to_your_sdk>.
        (例如: export ANDROID_HOME=/home/angelos/Desktop/adt-bundle-linux-x86-20131030/sdk/ )
        4. 找到你项目的目录 (例如: cd /home/angelos/workspace/MyFirstTest/ )
        5. 输入ant build.
        6. 找到 platform-tools目录. (Example: cd /home/angelos/Desktop/adt-bundle-linux-x86-20131030/sdk/platform-tools )
        7.把jar导入你的设备(Example: adb push
        /home/angelos/workspace/MyFirstTest/bin/MyFirstTest.jar /data/local/tmp/
        )

      5. 运行测试并且查看测试结果

        为了可以正常运行测试代码,需要先在控制台: adb shell uiautomator runtest MyFirstTest.jar -c nak.test.CalculatorTest. 注: 名字需要与项目名和类名相同 MyFirstTest.jarnak.test.CalculatorTest. Voila! 接着测试执行过程中可以查看7和1的按钮是发生什么变化的。

      6. 注意任何错误或测试中发现的缺陷。

        现在,你要注意下应用的测试或测试的极端条件下的应用程序的不良表现。你可以写一个你所可以想到的测试场景。

源码

package nak.test;

//Import the uiautomator libraries

import com.android.uiautomator.core.UiObject;

import com.android.uiautomator.testrunner.UiAutomatorTestCase;

import com.android.uiautomator.core.UiSelector;

import com.android.uiautomator.core.UiObjectNotFoundException;

import com.android.uiautomator.core.UiScrollable;

public class CalculatorTest extends UiAutomatorTestCase {   

public void testingCalculator() throws UiObjectNotFoundException {   

// First we testing the press of the HOME button.

getUiDevice().pressHome();

 // using the uiautomatorviewer tool we found that the button for the "applications" has

 //the value “Apps” (screen9)

 // so we use this property to create a UiSelector to find the button. 

UiObject Applications = new UiObject(new UiSelector().description("Apps"));

// testing the click to bring up the All Apps screen.

Applications.clickAndWaitForNewWindow();

// In the All Apps screen, the "Calculator" application is located in 

// the Apps tab. So, we create a UiSelector to find a tab with the text 

// label “Apps”.

UiObject apps = new UiObject(new UiSelector().text("Apps"));

// and then testing the click to this tab in order to enter the Apps tab.

apps.click();

// All the applications are in a scrollable list 

// so first we need to get a reference to that list

UiScrollable ListOfapplications = new UiScrollable(new UiSelector().scrollable(true));

// and then trying to find the application    

// with the name Calculator

UiObject Calculator = ListOfapplications.getChildByText(new UiSelector().className(android.widget.TextView.class.getName()),"Calculator");

Calculator.clickAndWaitForNewWindow();

// now the Calculator app is open

// so we can test the press of button "7" using the ID "com.android.calculator2:id/digit7"

//we found by using uiautomatorviewer

UiObject seven = new UiObject(new UiSelector().resourceId("com.android.calculator2:id/digit7"));

seven.click();

// now we test the press of button "+"

UiObject plus = new UiObject(new UiSelector().resourceId("com.android.calculator2:id/plus"));

plus.click();

// and then the press of button "1"

UiObject one = new UiObject(new UiSelector().resourceId("com.android.calculator2:id/digit1"));

one.click();

// we test the press of button "="

UiObject result = new UiObject(new UiSelector().resourceId("com.android.calculator2:id/equal"));

result.click();

//and finally we test the press of "Back" button

getUiDevice().pressBack();

}   

}

使用uiautomatorviewer和uiautomator来做android的UI测试的更多相关文章

  1. GT-----如何做Android应用流量测试?

    1.如何判断一个应用的流量偏高? 如果看流量的绝对值看不出高低,那就找几个同类型的产品对比一下,如果完成同样的事物,被测应用比同类产品高很多,那就偏高了,可能有优化的空间. 2.如何找到有效的优化点? ...

  2. 使用uiautomator做UI测试

    转载~~~~~~~~~~~~~~~~~~~~~~~~ 若有侵权,请及时联系本博主,博主将第一时间撤销 在Android 4.1发布的时候包含了一种新的测试工具–uiautomator,uiautoma ...

  3. [转载]使用uiautomator做UI测试

    这个只是单纯的mark一下.还没有认真去研究.鉴于最近也不会做手机的自动化测试,所以留作以后参考吧. 转自: http://blog.chengyunfeng.com/?p=504 在Android ...

  4. [zhuan]使用uiautomator做UI测试

    http://blog.chengyunfeng.com/?p=504 在Android 4.1发布的时候包含了一种新的测试工具–uiautomator,uiautomator是用来做UI测试的.也就 ...

  5. Appium做Android功能自动化测试

    前言 做Android端功能自动化已有2年多的时间了,使用过的功能自动化框架有Robotium.Uiautomator.Appium.最近研究自动化case复用的方案,调研了Appium的自动化框架, ...

  6. 现在创业做App,先做 Android 还是 iOS?

    随着互联网+的高速发展,现在创业大部分都是在布局移动端,初期往往摆在面前最大的难题是,如何分配有限的成本,在最快的速度内占领市场?这个大难题会影响创始人在团队和产品建设方方面面的决定.缩小至移动App ...

  7. 一位资深开发的个人经历 【转自百度贴吧 java吧 原标题 4年java 3年产品 现在又开始做android了】

    楼主2007年从一家天津的三流大学毕业.毕业前报了一个职位培训,毕业后可以推荐工作.因为推荐的公司都是北京的,所以就来北京了. 找了一个月工作,没有找到要我的,就在出租屋里宅了起来,打着考研的旗号,又 ...

  8. 最近开始做Android了

    最近开始做Android,在学习的过程中发现找以前知识很不方便啊,于是决定以后还是把知识记录在博客里吧,说不定也能为他人提供参考!

  9. 想做Android Wear开发?你得先搞明白这四件事

    手环和手表的腕上穿戴之争,随着Apple Watch发布和Android Wear不断完善而告一段落.尽管续航上略有缺陷,但手表以其类似手机可扩展的生态环境赢得了众多巨头的支持. Google曾透露, ...

随机推荐

  1. ASINetworkQueue 队列下载

    我们通过一个例子介绍一下请求队列使用,我们设计了一个应用,用户点击GO按钮从服务器同时下载两张图片显示在画面中. 我们直接看看主视图控制器ViewController.h代码如下: #import “ ...

  2. UIImage 调整图片大小

    -(UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size { UIGraphicsBeginImageContext(size); [img d ...

  3. SqlServer基础:游标

    记录下今天用到的游标: DECLARE @TempID INTDECLARE @Number INTSET @Number=1DECLARE myCursor CURSOR FOR     SELEC ...

  4. 3.IP地址分类_规划_子网掩码

    IP地址分类_规划_子网掩码 3.1MAC地址 网卡的身份证号———MAC地址 MAC地址的长度为48位(6个字节),通常表示为12个16进制数,每2个16进制数之间用冒号隔开,如:08:00:20: ...

  5. Android之绚丽的图片游览效果--有点像W7效果,透明的倒影,层叠的图片,渐变的颜色透明度

    这里转载一个牛人的博客:http://www.cnblogs.com/tankaixiong/archive/2011/02/24/1964340.html 下面,是我参照他的博客实现的一个效果图.这 ...

  6. iOS调用HTML

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. RPC、SQL、NFS属于OSI的哪一层

    第一层:物理层 第二层:数据链路层 802.2.802.3ATM.HDLC.FRAME RELAY 第三层:网络层 IP.IPX.ARP.APPLETALK.ICMP 第四层:传输层 TCP.UDP. ...

  8. ZooKeeper -- 分布式开源协调服务

    ZooKeeper是一个为分布式应用所设计的开源协调服务,适用于大型的分布式系统,可以提供统一命名服务.状态同步服务.集群管理.分布式应用配置项的管理等服务.ZooKeeper支持Java和C两种编程 ...

  9. Http错误 404.3-Not Found....或者500.19 Internal Server Error

    解决方法:以管理员身份打开VS2010x64位兼容命令提示:aspnet_regiis -i

  10. EBS R12.2安装,使用的操作系统用户

    在安装时,错误使用了oracle rdbms的对应的操作系统用户,导致安装前,验证时"web server install prerequisites"选项验证失败: (本图其它两 ...