在Android Sudio中使用Uiautomator
2016-07-05 13:45
567 查看
原文地址
我使用的环境要求:
1、Android Studio 2.0
2、SDK Manager需要安装Android Support Repository,没有安装的需要自己去下,如图:
【步骤1】新建一个Android工程
不需要创建Activity
【步骤2】配置gradle(app)
内容如下:
[plain] view
plain copy
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
//引入uiautomator
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.0'
}
修改gradle之后,需要同步一下,才能把uiautomator包导入,如图
【步骤3】创建TestCase
在src/androidTest/java目录下创建测试类
类名可以随便取,写上你的用例,可以参照下面的例子:
[java] view
plain copy
package com.cxq.uiautomatordemo;
import android.support.test.uiautomator.UiAutomatorTestCase;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;
/**
* Created by CrystalChen on 2016/4/21.
*/
public class UiTest extends UiAutomatorTestCase {
public void testDemo() throws UiObjectNotFoundException {
getUiDevice().pressHome();
UiObject Calculator = new UiObject(new UiSelector().description("计算器"));
Calculator.clickAndWaitForNewWindow();
UiObject seven = new UiObject(new UiSelector().resourceId("com.android.calculator2:id/digit7"));
seven.click();
UiObject plus = new UiObject(new UiSelector().resourceId("com.android.calculator2:id/plus"));
plus.click();
UiObject one = new UiObject(new UiSelector().resourceId("com.android.calculator2:id/digit1"));
one.click();
UiObject result = new UiObject(new UiSelector().resourceId("com.android.calculator2:id/equal"));
result.click();
getUiDevice().pressBack();
}
}
【步骤4】运行,右键你的测试类,Run
如果按照上面的那个用例写,机子会打开计算器,自动输入7+1=
例子中的控件id会有变动,需要自行修改。
【补充】如果后期还需要运行测试用例,可以通过如下的adb命令调用
adb shell am instrument -w -r -e debug false -e class com.cxq.uiautomatordemo.UiTest com.cxq.uiautomatordemo.test/android.test.InstrumentationTestRunner
我使用的环境要求:
1、Android Studio 2.0
2、SDK Manager需要安装Android Support Repository,没有安装的需要自己去下,如图:
【步骤1】新建一个Android工程
不需要创建Activity
【步骤2】配置gradle(app)
内容如下:
[plain] view
plain copy
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
//引入uiautomator
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.0'
}
修改gradle之后,需要同步一下,才能把uiautomator包导入,如图
【步骤3】创建TestCase
在src/androidTest/java目录下创建测试类
类名可以随便取,写上你的用例,可以参照下面的例子:
[java] view
plain copy
package com.cxq.uiautomatordemo;
import android.support.test.uiautomator.UiAutomatorTestCase;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;
/**
* Created by CrystalChen on 2016/4/21.
*/
public class UiTest extends UiAutomatorTestCase {
public void testDemo() throws UiObjectNotFoundException {
getUiDevice().pressHome();
UiObject Calculator = new UiObject(new UiSelector().description("计算器"));
Calculator.clickAndWaitForNewWindow();
UiObject seven = new UiObject(new UiSelector().resourceId("com.android.calculator2:id/digit7"));
seven.click();
UiObject plus = new UiObject(new UiSelector().resourceId("com.android.calculator2:id/plus"));
plus.click();
UiObject one = new UiObject(new UiSelector().resourceId("com.android.calculator2:id/digit1"));
one.click();
UiObject result = new UiObject(new UiSelector().resourceId("com.android.calculator2:id/equal"));
result.click();
getUiDevice().pressBack();
}
}
【步骤4】运行,右键你的测试类,Run
如果按照上面的那个用例写,机子会打开计算器,自动输入7+1=
例子中的控件id会有变动,需要自行修改。
【补充】如果后期还需要运行测试用例,可以通过如下的adb命令调用
adb shell am instrument -w -r -e debug false -e class com.cxq.uiautomatordemo.UiTest com.cxq.uiautomatordemo.test/android.test.InstrumentationTestRunner
相关文章推荐
- LigerUI刷新选项卡、设置选项卡标题
- DuiLib库 下载最新SVN地址
- Android 高级UI设计笔记20:RecyclerView 的详解之RecyclerView添加Item点击事件
- Duilib 使用Zip 资源
- IOS-UIView的移动
- Helpers\TableBuilder
- Helpers\Request
- 在非UI线程中显示Toast
- UIView的userInteractionEnabled属性的意义
- 设置UITableViewCell选中的样式
- iOS中使用UITextView时候的一些小技巧
- APUE ioctl
- ZUI前段框架简介
- Request
- solr admin UI
- 图片圆角处理封装
- DocsBuilder2
- 通过ServletRequestAware接口获取request
- Requirements
- Pull Requests