Android SDK提供了以下工具,用于支持对APP应用程序进行自动功能用户界面测试:
uiautomatorviewer
用户监视器
uiautomatorviewer
用于扫描和分析Android APP应用程序UI组件的GUI工具。
UIautomatorviewer工具提供了一个方便的可视界面,用于检查布局层次结构并显示测试设备上显示的各个ui组件的特性。 使用此信息,可以使用稍后要测试的特定UI组件的选择器对象创建uiautomator测试。
要分析要测试的APP应用程序的UI组件,请在安装了示例中所示的APP应用程序后执行以下操作:
将安卓设备连接到开发计算机
打开终端窗口,然后单击/tools/
使用此命令运行工具
uiautomatorviewer
按照以下命令操作
将显示以下窗口: 这是ui自动查看器的默认窗口。
单击右上角的设备图标。 将开始获取设备上当前打开的屏幕的UI XML快照。 这个会变成这样。
然后,设备屏幕的快照将显示在uiautomatorviewer窗口中。
在此窗口的右侧,您将看到两个分区。 顶部分区说明节点结构、UI组件的放置和包含方法。 单击每个节点,以下部分将显示详细信息。
例如,请考虑下图。 单击此按钮,可以看到上面的部分选择了Button,下面的部分显示其详细信息。 由于此按钮是可单击的,因此clickable属性设置为true。
UI Automator Viewer还可以帮助您以不同的方向检查UI。 例如,您只需将设备重定向为横向,然后再次捕获屏幕快照。 如下图所示-
用户监视器
现在,您可以创建自己的测试用例,使用uiautomatorviewer运行并检查它们。 要创建自己的测试用例,必须执行以下步骤-
在项目资源管理器中,右键单击刚创建的新项目,选择" Properties Java Build Path ",然后执行以下操作-
单击" Add Library JUnit ",然后选择" JUnit3"以添加JUnit支持。
单击添加外部jars .并导航到SDK目录。 在platforms目录中,选择最新的SDK版本,然后添加uiautomator.jar和android.jar文件。
使用UiAutomatorTestCase扩展课程
是的。 必要的测试用例。
编码代码后,请按照以下步骤生成测试JAR并将其部署到目标Android测试设备上:
创建生成输出JAR所需的生成配置文件。 要生成构建配置文件,请打开终端并运行以下命令:
/tools/androidcreateuitest-project-n-t1-p
是包含uiautomator测试源文件的项目的名称,其中是相应项目目录的路径。
从命令行设置ANDROID_HOME变量。
set ANDROID_HOME=
导航到包含build.xml文件的项目目录,然后生成测试JAR。
ant build
使用adb push命令将生成的测试JAR文件部署到测试设备上。
adb push /data/local/tmp/
按照以下命令运行测试-
adbshelluiautomatorruntestlaunchsettings.jar-ccom.UIA.example.my.launch settings
范例
以下示例说明了ui测试的使用。 包含可在uiautomatorviewer中使用的基本APP应用程序。
要尝试此示例,必须在实际设备上执行此操作,并按照开头介绍的uiautomatorviewer中的步骤操作。
步骤
说明
1
使用Android studio在com.codingdict.myapplication包下创建Android APP应用程序。
2
修改src/MainActivity.java文件以添加活动代码。
3
修改布局XML文件res/layout/activity_main.xml,根据需要添加GUI组件。
4
创建src/second.java文件以添加活动代码。
5
修改布局XML文件res/layout/view.xml,根据需要添加GUI组件。
6
运行应用程序并选择正在运行的Android设备并在其上安装应用程序并验证结果。
以下是 MainActivity.java 的内容。
package com.codingdict.myapplication;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent in =new Intent(MainActivity.this,second.class);
startActivity(in);
}
});
}
}
这是 second.java 的内容。
package com.codingdict.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class second extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
Button b1=(Button)findViewById(R.id.button2);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(second.this,"Thanks",Toast.LENGTH_LONG).show();
}
});
}
}
以下是 activity_main.xml 的内容
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UI Animator Viewer"
android:id="@+id/textView"
android:textSize="25sp"
android:layout_centerHorizontal="true" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials point"
android:id="@+id/textView2"
android:layout_below="@+id/textView"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView"
android:textColor="#ff36ff15"
android:textIsSelectable="false"
android:textSize="35dp" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/abc"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:id="@+id/button"
android:layout_marginTop="98dp"
android:layout_below="@+id/imageView"
android:layout_centerHorizontal="true" />
这是 view.xml 的内容。
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Button"
android:id="@+id/button2"
android:layout_gravity="center_horizontal"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
这是 Strings.xml 的内容。
My Application
这是 AndroidManifest.xml 的内容。
package="com.codingdict.myapplication" >
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name=".MainActivity"
android:label="@string/app_name" >
让我们尝试运行您的UI测试应用程序。我假设您已将实际的Android移动设备与计算机相关联。要从Android工作室运行应用程序,请打开项目的某个活动文件,然后单击
工具栏中的“运行” 图标。在开始申请之前,Android studio将显示以下窗口,以选择您要运行Android应用程序的选项。
选择您的移动设备作为选项,然后检查将显示应用程序屏幕的移动设备。现在只需按照ui automator
viewer部分顶部提到的步骤操作,即可对此应用程序执行ui测试。