要写UIAutomator的testcase,首先要用Eclipse创建一个JavaProject
需要将Junit的lib加入到工程里
添加android.jar和uiautomator.jar
创建编译配置文件
<android-sdk>/tools/android create uitest-project -n<name> -t 1 -p <path>
说明一下各个参数的作用,如果已经将androidsdk的路径配置到了系统的path中,输入命令“androidcreate uitest-project”就可以查看到相应的帮助
-n --name : Project name. 就是在eclipse中创建的项目的名字。
-t --target : TargetID of the new project. [required] 这个id是本机上androidtargets的id,可以通过命令 “androidlist”来查询,得到如下图的结果,选择android-17以上版本前面所对应的id
-p 项目工程路径
根据我电脑的情况我的命令是这样的
D:\work\android-sdk\tools>android create uitest-project-n UiAutomator -t 6 -p D:\work\eclipse\workshop\UiAutomator
创建成功的输出如下
从eclipse中可以看到有3个新增加的文件
在build.xml上点击右键,选择“RunAs”-> "Ant Build",可以看到下面的输出,这个输出实际上是一个帮助,是对build.xml的作用进行说明的。在build.xml中配置上相应的选项可以完成相应的操作。
在build.xml的配置上,将default后面的选项改为"build"后,就可以buildjar包了。
同样执行RunAs操作,就可以build成功了。目标文件声称在工程目录下的bin文件夹下。
将jarpush到手机中,
adb pushUiAutomator.jar /data/local/tmp/
然后执行就可以了
adb shell uiautomator runtest UiAutomator.jar -ccom.uia.example.my.test
Run testcases on the target device
adb shell uiautomator runtest <JARS> -c <CLASSES> [options]
<JARS>--The <JARS> argument is the name of oneor more JAR files that you deployed to the target device which contain your uiautomatortestcases.You can list morethan one JAR file by using a space as a separator.
-c <CLASSES>--The <CLASSES> argument is a listof test classes or test methods in <JARS> to run.
Each class or method must be fully qualified with thepackage name, in one of these formats:
package_name.class_name
package_name.class_name#method_name
You can list multiple classes or methods by using a space asa separator.
Class | Description |
com.android.uiautomator.core.UiCollection | Used to enumerate a container's user interface (UI) elements for the purpose of counting, or targeting a sub elements by a child's text or description. |
com.android.uiautomator.core.UiDevice | Provides access to state information about the device. You can also use this class tosimulate user actions on the device, such as pressing the d-pad hardware button or pressing the Home and Menu buttons. |
com.android.uiautomator.core.UiObject | Represents a user interface (UI) element. |
com.android.uiautomator.core.UiScrollable | Provides support for searching for items in a scrollable UI container. |
com.android.uiautomator.core.UiSelector | Represents a query for one or more target UI elements on a device screen. |
com.android.uiautomator.core.Configurator | Allows you to set key parameters for running uiautomator tests. |
package com.sony.test;
import com.android.uiautomator.core.UiCollection;
import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObject;
importcom.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiScrollable;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.core.UiWatcher;
importcom.android.uiautomator.testrunner.UiAutomatorTestCase;
import android.util.Log;
public class testUI extends UiAutomatorTestCase {
protected voidsetUp() throws Exception {
super.setUp();
getUiDevice().registerWatcher("watchDialog", newUiWatcherDialog());
}
public classUiWatcherDialog implements UiWatcher {
@Override
public booleancheckForCondition() {
Log.d("shijian", "checkForCondition");
return false;
}
}
@Override
protected voidtearDown() throws Exception {
getUiDevice().removeWatcher("watchDialog");
super.tearDown();
}
public voidtestDemo() throws UiObjectNotFoundException {
// UiDevice
UiDevicemUiDevice = getUiDevice();
mUiDevice.pressHome();
// UiObject&& UiSelector
UiObjectmAppItem = new UiObject(newUiSelector().className("android.widget.ImageView")
.packageName("com.sonyericsson.home").index(2));
mAppItem.clickAndWaitForNewWindow();
//UiCollection
UiCollectionallApps = new UiCollection(new UiSelector().className(
"android.widget.AdapterView").index(1));
int count =allApps.getChildCount(new UiSelector()
.className("android.widget.RelativeLayout"));
Log.d("shijian", "count " + count);
UiObjectsettings = allApps.getChildByText(
newUiSelector().className("android.widget.TextView"), "Settings");
settings.click();
//UiScrollable
UiScrollablesettingsItem = new UiScrollable(new UiSelector()
.className("android.widget.ListView").packageName("com.android.settings").index(0));
UiObject about= settingsItem.getChildByText(
newUiSelector().className("android.widget.TextView"), "Aboutphone");
about.click();
}
}