工具准备
一、准备好java环境(JDK)和安卓环境(SDK、ADT)
jdk1.6+ eclipseSDK 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、使用cmd 2、切换到项目路劲下,进入该项目中 3、使用 E:New ewspaceUiAutomatorDemo>android create uitest-project -n uiautomatordemo -t 1 -p E:New ewspaceUiAutomatorDemo 生成一个编译文件build.xml文件 4、进入eclipse中更改build文件,help改为build 5、运行编译文件ant 6、把生成的jar包push到手机中: E:New ewspaceUiAutomatorDemo>adb push E:New ewspaceUiAutomatorDemoinuiautomatordemo.jar /data/local/tmp/ 7、运行jar包启动测试 E:New ewspaceUiAutomatorDemo>adb shell uiautomator runtest uiautomatordemo.jar -c com.jekouxueyuan.Test