Android 基础知识

Android 主页 Android 概述 Android 下载安装和开发环境配置 Android Studio IDE Android 架构 Android 应用程序组件 Android Hello World 示例 Android 资源 Android 活动 Android 服务 Android 广播接收器 Android 内容提供者 Android 片段 Android Intents/Filters

Android 用户界面

Android UI 布局 Android UI 控件 Android 事件处理 Android 样式和主题 Android 自定义组件

Android 高级概念

Android 拖放 Android 通知 Android 基于位置的服务 Android 发送电子邮件 Android 发送短信 Android 拨打电话 Android 发布应用程序

Android 实用示例

Android 警报对话框 Android 动画 Android 音频捕捉 Android 音频管理器 Android 自动完成 Android 最佳实践 Android 蓝牙 Android 相机 Android 剪贴板 Android 自定义字体 Android 数据备份 Android 开发者工具 Android 模拟器 Android Facebook 集成 Android 手势 Android 谷歌地图 Android 图像效果 Android 图像切换 Android 内部存储 Android JetPlayer Android JSON 解析器 Android Linkedin 集成 Android 旋转加载器 Android 本地化 Android 登录应用 Android 媒体播放器 Android 多点触控 Android 导航 Android 网络连接 Android NFC 指南 Android PHP/MySQL Android 进度圈 Android 进度条 Android 推送通知 Android 渲染脚本 Android RSS 阅读器 Android 屏幕投射 Android SDK 管理器 Android 传感器 Android 会话管理 Android 共享首选项 Android SIP 协议 Android 拼写检查器 Android SQLite 数据库 Android 支持库 Android 测试 Android 文字转语音 Android TextureView Android Twitter 集成 Android UI 设计 Android UI 模式 Android UI 测试 Android WebView 布局 Android Wi-Fi Android Widgets Android XML 解析器

Android 其他

Android 面试问题 Android 有用的资源 Android 测验


Android - UI 用户界面测试

Android SDK 提供以下工具来支持对您的应用程序进行自动化的功能性 UI 测试。

  • uiautomatorviewer
  • uiautomator

uiautomatorviewer

一种用于扫描和分析 Android 应用程序的 UI 组件的 GUI 工具。

uiautomatorviewer 工具提供了一个方便的可视界面来检查布局层次结构并查看测试设备上显示的各个 UI 组件的属性。 使用此信息,您可以稍后使用选择器对象创建 uiautomator 测试,这些选择器对象针对要测试的特定 UI 组件。

要分析您要测试的应用程序的 UI 组件,请在安装示例中给出的应用程序后执行以下步骤。

  • 将您的 Android 设备连接到您的开发机器
  • 打开一个终端窗口并导航到 <android-sdk>/tools/
  • 使用此命令运行工具
uiautomatorviewer

将遵循如下所示的命令

Android UI 测试教程

您将看到出现以下窗口。 它是 uiautomatorviewer 的默认窗口。

Android UI 测试教程
  • 单击右上角的设备图标。 它将开始拍摄设备中当前打开的屏幕的 UI XML 快照。 会是这样的。

Android UI 测试教程

之后,您将在 uiautomatorviewer 窗口中看到设备屏幕的快照。

Android UI 测试教程

在此窗口的右侧,您将看到两个分区。 上部分解释了节点结构,UI 组件的排列和包含方式。 单击每个节点可在下部分区中提供详细信息。

例如,考虑下图。 当您点击按钮时,您可以在上部分区中看到 Button 被选中,在下部分区中显示其详细信息。 由于这个按钮是可点击的,所以它的可点击属性设置为 true。

Android UI 测试教程

uiautomatorviewer 还可以帮助您检查不同方向的 UI。 例如,只需将您的设备方向更改为横向,然后再次捕获屏幕截图。 如下图所示 −

Android UI 测试教程

uiautomator

现在您可以创建自己的测试用例并使用 uiautomatorviewer 运行它来检查它们。 为了创建自己的测试用例,您需要执行以下步骤 −

  • 在 Project Explorer 中,右键单击您创建的新项目,然后选择 Properties > Java Build Path,并执行以下操作 −

  • 单击 Add Library > JUnit 然后选择 JUnit3 以添加 JUnit 支持。

  • 单击 Add External JARs... 并导航到 SDK 目录。 在平台目录下,选择最新的 SDK 版本并添加 uiautomator.jar 和 android.jar 文件。

  • 使用 UiAutomatorTestCase 扩展您的类

  • 对必要的测试用例。

  • 编写测试代码后,请按照以下步骤构建测试 JAR 并将其部署到目标 Android 测试设备。

  • 创建所需的构建配置文件以构建输出 JAR。 要生成构建配置文件,请打开终端并运行以下命令:

<android-sdk>/tools/android create uitest-project -n <name> -t 1 -p <path>
  • <name> 是包含您的 uiautomator 测试源文件的项目的名称,<path> 是相应项目目录的路径。

  • 从命令行,设置 ANDROID_HOME 变量。

set ANDROID_HOME=<path_to_your_sdk>
  • 转到 build.xml 文件所在的项目目录并构建测试 JAR。
ant build
  • 使用 adb push 命令将生成的测试 JAR 文件部署到测试设备。
adb push <path_to_output_jar> /data/local/tmp/
  • 通过以下命令运行测试 −
adb shell uiautomator runtest LaunchSettings.jar -c com.uia.example.my.LaunchSettings

示例

下面的示例演示了 UI 测试的使用。 它创建了一个可用于 uiautomatorviewer 的基本应用程序。

要试验这个例子,你需要在一个实际的设备上运行它,然后按照开头解释的 uiautomatorviewer 步骤进行操作。

步骤 描述
1 您将使用 Android Studio 在 com.tutorialspoint.myapplication 包下创建一个 Android 应用程序。
2 修改 src/MainActivity.java 文件添加活动代码。
3 修改布局 XML 文件 res/layout/activity_main.xml 如果需要,添加任何 GUI 组件。
4 创建 src/second.java 文件以添加 Activity 代码。
5 如果需要,修改布局 XML 文件 res/layout/view.xml 添加任何 GUI 组件。
6 运行应用程序并选择一个正在运行的 android 设备并在其上安装应用程序并验证结果。

这是MainActivity.java的内容。

package com.tutorialspoint.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.tutorialspoint.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 的内容

以下代码中的abc表示tutorialspoint.com的标志
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   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">
   
   <TextView
      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" />
      
   <TextView
      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" />
      
   <ImageView
      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" />
      
   <Button
      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" />

</RelativeLayout>

这是 view.xml 的内容。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical" android:layout_width="match_parent"
   android:layout_height="match_parent">

   <Button
      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" />
</RelativeLayout>

这是Strings.xml的内容。

<resources>
   <string name="app_name">My Application</string>
</resources>

这是AndroidManifest.xml的内容。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.tutorialspoint.myapplication" >
   
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name=".MainActivity"
         android:label="@string/app_name" >
         
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
         
      </activity>
      
      <activity android:name=".second"></activity>
      
   </application>
</manifest>

让我们尝试运行您的 UI 测试应用程序。 我假设您已将实际的 Android 移动设备与您的计算机连接起来。要从 Android Studio 运行应用程序,请打开项目的一个活动文件,然后单击工具栏中的运行 Eclipse 运行图标 图标。在启动您的应用程序之前,Android Studio 将显示以下窗口以选择您要运行 Android 应用程序的选项。

Android UI 测试教程

选择您的移动设备作为选项,然后检查将显示应用程序屏幕的移动设备。 现在只需按照顶部在 uiautomatorviewer 部分下提到的步骤来在此应用程序上执行 ui 测试。