工具准备
一、准备好java环境(JDK)和安卓环境(SDK、ADT) jdk1.6+ \eclipse\SDK \ADT 详情百度,安装java环境 二、打开eclipse三、创建步骤:右键新建====》java项目====》填写项目名称====》完成选择新建项目====》右键新建====》包====》填写包名====》填写包名====》完成选择新建项目====》右键新建====》文件夹====》填写文件夹名(libs存放依赖包)====》把android.jar和uiautomator.jar包复制黏贴进来====》选择两个包====》右键加载到buildpath里面选择新建项目====》选择包====》右键新建====》类====》填写类名+扩展名====》完成四、编辑代码
示例:
package com.jekouxueyuan;import android.R.color;import com.android.uiautomator.core.UiDevice;import com.android.uiautomator.core.UiObject;import com.android.uiautomator.core.UiObjectNotFoundException;import com.android.uiautomator.core.UiSelector;import com.android.uiautomator.testrunner.UiAutomatorTestCase;public class Test extends UiAutomatorTestCase { public void testDemo() throws UiObjectNotFoundException{ UiDevice.getInstance().pressHome(); UiObject browserObject = new UiObject(new UiSelector().text("Browser")); browserObject.clickAndWaitForNewWindow(); UiObject editoObject = new UiObject(new UiSelector().className("android.widget.EditText")); editoObject.click(); UiDevice.getInstance().pressDelete(); editoObject.setText("www.baidu.com"); UiDevice.getInstance().pressEnter(); sleep(2000); }}
运行编译:
1、使用cmd2、切换到项目路劲下,进入该项目中3、使用E:\New\newspace\UiAutomatorDemo>android create uitest-project -n uiautomatordemo -t 1 -p E:\New\newspace\UiAutomatorDemo 生成一个编译文件build.xml文件4、进入eclipse中更改build文件,help改为build5、运行编译文件ant6、把生成的jar包push到手机中:E:\New\newspace\UiAutomatorDemo>adb push E:\New\newspace\UiAutomatorDemo\bin\uiautomatordemo.jar /data/local/tmp/7、运行jar包启动测试E:\New\newspace\UiAutomatorDemo>adb shell uiautomator runtest uiautomatordemo.jar -c com.jekouxueyuan.Test