This will guide you through the steps to write your first uiautomator test using gradle as it build system.

What is gradle?

“Gradle combines the power and flexibility of Ant with the dependency management and conventions of Maven into a more effective way to build.”

For more visit gradle page.

What is uiautomator?

“The uiautomator testing framework lets you test your user interface (UI) efficiently by creating automated functional UI testcases that can be run against your app on one or more devices.”

For more visit uiautomator page.

Java source code

Create a directory named bluetooth:

$ mkdir bluetooth

Create a java project layout:

$ cd bluetooth
$ mkdir -p src/main/java/com/github/wiliamsouza/bluetooth

Create a java file BluetoothTest.java with the following content:

package com.github.wiliamsouza.bluetooth;

import java.io.IOException;

import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiScrollable;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase; public class BluetoothTest extends UiAutomatorTestCase { public void setUp() throws UiObjectNotFoundException, IOException {
getUiDevice().pressHome();
String packageName = "com.android.settings";
String component = packageName + "/.Settings";
String action = "am start -a android.intent.action.MAIN -n "; // start settings application
Runtime.getRuntime().exec(action + component); UiObject settingsUi = new UiObject(new UiSelector().packageName(packageName));
assertTrue("Application settings not started", settingsUi.waitForExists(5000));
} public void testBluetooth() throws UiObjectNotFoundException {
UiScrollable scroll = new UiScrollable(new UiSelector().scrollable(true));
UiObject layout = scroll.getChildByText(new UiSelector().className(android.widget.LinearLayout.class.getName()),"Bluetooth", true);
UiObject switchBluetooth = layout.getChild(new UiSelector().className(android.widget.Switch.class.getName()));
assertTrue("Unable to find bluetooth switch object", switchBluetooth.exists());
switchTo(switchBluetooth, "Bluetooth");
} private void switchTo(UiObject switchObject, String name) throws UiObjectNotFoundException {
// Before start test ensure switch is off
if (switchObject.isChecked()) {
switchObject.click();
sleep(3000);
} switchObject.click();
sleep(3000);
assertTrue("Unable to turn " + name + " on", switchObject.isChecked()); switchObject.click();
sleep(3000);
assertFalse("Unable to turn " + name + " off", switchObject.isChecked());
}
}

Gradle

Create a build.gradle with the following content:

apply plugin: 'java'
apply plugin: 'idea' sourceCompatibility = 1.5
targetCompatibility = 1.5 version = '0.1' project.ext {
dexDir = new File('build/dex')
distDir = new File('./dist')
} repositories {
mavenCentral()
} dependencies {
compile fileTree(dir: androidSdkHome + '/platforms/' + androidSdkTarget, include: '*.jar')
compile group: 'junit', name: 'junit', version: '4.11'
} jar {
doLast {
tasks.dex.execute()
}
} task dex(dependsOn: jar, type:Exec) {
println 'Building dex...'
project.dexDir.mkdirs()
workingDir '.'
commandLine androidSdkHome + '/' + androidSdkBuildToolsDir + '/' + 'dx', '--dex', '--no-strict', '--output=' + buildDir +'/dex/' + project.name + '.jar', jar.archivePath
doLast {
tasks.dist.execute()
}
} task dist(dependsOn:dex, type:Copy) {
project.distDir.mkdirs()
from(project.dexDir)
into(project.distDir)
include('*.jar')
}

Configure

Now lets configure the build system, Create a file gradle.properties containing:

androidSdkHome=/home/wiliam/bin/android-sdk-linux
androidSdkTarget=android-18
androidSdkBuildToolsDir=build-tools/18.1.1
  • androidSdkHome – Point to the root directory of android SDK.
  • androidSdkTarget – Is the target, points to a directory inside platforms.
  • androidSdkBuildToolsDir – Location of dx program.

Build

$ gradle build

Install

$ adb push dist/bluetooth.jar /data/local/tmp/

Run

$ adb shell uiautomator runtest bluetooth.jar -c com.github.wiliamsouza.bluetooth.BluetoothTest

Android uiautomator gradle build system的更多相关文章

  1. 解决Android Studio Gradle Build Running慢的问题

    Android Studio方便好用,但是Android Studio Gradle Build Running很慢 解决方法: C:\Users\你的用户名\.gradle 目录下新建一个文件名为 ...

  2. 解决Android Studio Gradle Build特别慢的问题

    解决Android Studio Gradle Build 特别慢的问题 C:\Users\你的用户名\.gradle目录下新建一个文件名为gradle.properties的文件.内容为:org.g ...

  3. Android Studio Gradle Build Running 特别慢的问题探讨

    本文的本本win7 64bit 6G android studio2.1 在运行程序的时候Gradle Build Running 特别慢,一个helloworld都快2min了 1.开启gradle ...

  4. Android Studio --> Gradle Build设置自动

    ps:http://www.cnblogs.com/kangyi/p/4448398.html 应用场景 通常情况下我们的apps发布后也就是release模式下log是不显示的,debug模式下是显 ...

  5. Android Studio Gradle build 报错:Received status code 400 from server: Bad Request

    错误提示如下 Could not GET 'https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.1.2/ ...

  6. Android Studio Gradle Build Running 加速

    加速效果

  7. android studio Gradle Build速度加快方法

    设置离线编译就可以解决这个问题了.如下图所示:

  8. Gradle: The New Android Build System

    Gradle: The New Android Build System Google selected Gradle as the foundation of the Android SDK bui ...

  9. Android Gradle 构建工具(Android Gradle Build Tools)是什么?

    转载地址:http://mrfu.me/android/2015/07/17/New_Android_Gradle_Build_Tools/ 译者地址:[翻]一览新的 Android Gradle 构 ...

随机推荐

  1. 2014年03月09日攻击百度贴吧的XSS蠕虫源码

    var n=PageData.user.user_forum_list.info.length; var num=0; var config = { titles: ["\u4f60\u76 ...

  2. google大赛 入围赛250分真题

    Problem StatementYou have a collection of music files with names formatted as "genre-artist-alb ...

  3. 说说php取余运算(%)的那点事

    http://www.phpddt.com/php/php-take-over.html       fmod()与 % 区别   都是取余 fmod是函数 原型float fmod(float x, ...

  4. adb或appium下多设备中指定设备的启动

    一.先用adb devices列出所有设备序列号 List of devices attached012BDC7N78954789 device132AEC8N57897458 device 二.进入 ...

  5. iOS后台如何保持socket长连接和数据传输

    工程中使用tcp长连接来和服务端进行数据传输,在IOS平台上,由于苹果的后台机制,会有以下问题: 当程序退到后台的时候,所有线程被挂起,系统会回收所有的socket资源,那么socket连接就会被关闭 ...

  6. POJ 1300 Door Man(欧拉回路的判定)

    题目链接 题意 : 庄园有很多房间,编号从0到n-1,能否找到一条路径经过所有开着的门,并且使得通过门之后就把门关上,关上的再也不打开,最后能回到编号为0的房间. 思路 : 这就是一个赤裸裸的判断欧拉 ...

  7. ExtJs之Ext.util.TextMetrics

    <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...

  8. C#中的 序列化和反序列化

    什么是序列化和反序列化? 序列化就是把一个对象保存到一个文件或数据库字段中去,反序列化就是在适当的时候把这个文件再转化成原来的对象使用. 我想最主要的作用有: 1.在进程下次启动时读取上次保存的对象的 ...

  9. Windbg 常用命令整理

    kd> !idt -a      查看idt kd> dt _ktrap_frame   异常帧 kd> ba e1 Address 下硬件执行断点kd> ba w4 Addr ...

  10. linux下PostgreSQL数据库的源码安装

    实验环境>>>>>>>>>>>>>>>>>>操作系统:CentOS release 6.3 ...