来自:http://university.utest.com 做者: 译者:Elaine00html
Android UI 测试有两种测试类型, 其中一种白盒测试须要测试人员了解到应用的源码。白盒测试,测试人员可使用为Android应用设计测试用例。第二类被称为黑盒测试,其中测试人员没有访问应用程序的源代码,可是你须要测试应用程序的行为,当它运行在一台设备。java
在Android用户界面测试,你要测试的应用程序如何与一个真实的用户交互。UI测试确保应用程序返回响应用户一系列操做的正确的UI输出,如键盘输入或按工具栏,菜单,对话框,图像,和其余用户界面控件。为了实现这一目标,你有两个选择。一是在真实的应用中来使用这个应用,而且尝试各类途径来发现应用的异常表现和Bugs。这种作法显然是费时,容易出错,你能够尝试有限的状况下。linux
另外一种方案是自动化测试。自动化测试涉及到建立的程序来完成测试任务(测试用例)覆盖特定的使用场景,而后使用测试框架来自动地执行测试用例和可重复的方式。android
第一个工具咱们要看到的是uiautomatorviewer,一个用来来扫描和分析Android应用程序的UI组件的GUI工具。为了使用uiautomatorviewer,你必须首先下载并安装SDK和根据指示设置ADT束Eclipse IDE。安装后,该工具存在于/tools/文件夹,您能够从命令行输入启动它:uiautomatorviewer。git
可是这个工具的用途是什么?使用uiautomatorviewer,你能够检查一个应用的UI来查看应用的布局和组件以及相关的属性。这是很是重要的,特别是当你想构建自动化测试,经过了解各个部件的应用层次的布局,你可使用uiautomator(在本课程的一部分API)来建立自动化测试。shell
下面我将一步一步带教你如何使用uiautomatorviewer来查看Calculator应用的布局和组件的IDapp
如今你知道了:框架
你如今知道如何使用uiautomator建立一个拥有简单的功能的用户界面测试。在下面的部分中,您将构建一个功能性的UI测试,能够检查应用程序当您添加数字“7”和“1”如何工做。ide
With uiautomator API, 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, 你必须:
一样能够从 “Properties” 选择“Java build path” -> “Libraries” -> “Add external JARs” ,在platforms目录下选择最新的SDK版本和 uiautomator.jar文件,android.jar文件.
注:API中有不少类,但在API中三个最重要的类是:
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();.
在写完测试场景的代码以后,你必须按照如下步骤来 编译你的代码,建立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/ )
为了能够正常运行测试代码,须要先在控制台: adb shell uiautomator runtest MyFirstTest.jar -c nak.test.CalculatorTest. 注: 名字须要与项目名和类名相同 MyFirstTest.jar和nak.test.CalculatorTest. Voila! 接着测试执行过程当中能够查看7和1的按钮是发生什么变化的。
如今,你要注意下应用的测试或测试的极端条件下的应用程序的不良表现。你能够写一个你所能够想到的测试场景。
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(); } }