使用AndroidStudio执行UI Automator 自动化测试
2015-09-15 13:27
477 查看
摘要
最近在网上看到UIAutomator 2.0版本,并且貌似使用的框架都改变了,所以今天就抽空试了下We’re pleased to announce the release of UIAutomator 2.0! This version is a significant update from the previous release. Most importantly, UI Automator is now based on Android Instrumentation and you can build and run tests with the ‘./gradlew connectedCheck’ command.
大体的意思就是UIAutomator2.0基于的框架改成了android的instrumentatioin的框架。并且可以通过gradle进行配置了,虽然说实话不太清楚为什么要将UiAutomator的框架改成instrumentation,但是我们先试试大概答题的环境出来吧。
下来我们就来试试通过Android Studio 来进行UiAutomator的配置吧
UiAutomator2.0的jar包并不是在以前SDK/platforms/android-19/下。现在我们要这么做通过Android SDK Manager中的 Android Support Repository 项进行安装
下载下来的jar包的路径为/extras/android/m2repository
新建一个android项目,编写一个简单的应用
在build.gradle中配置依赖项:
dependencies { androidTestCompile 'com.android.support.test:runner:0.3' // Set this dependency to use JUnit 4 rules androidTestCompile 'com.android.support.test:rules:0.3' // Set this dependency to build and run Espresso tests androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2' // Set this dependency to build and run UI Automator tests androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1' }
设置AndroidJunitRunner为默认的testInstrumentationRunner
android { defaultConfig { testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } }
下来就编写属于你自己的测试代码了,在androidTest目录下面新建测试类
/** * Created by cvtpc on 2015/9/14. */ public class LoginTest extends InstrumentationTestCase { protected UiDevice device = null; protected String appName = "magicCard"; public void runApp(String appName) throws UiObjectNotFoundException, RemoteException { device = UiDevice.getInstance(getInstrumentation()); device.pressHome(); device.waitForWindowUpdate("", 2000); UiObject2 allAppsButton = device.findObject(By.desc("Apps")); allAppsButton.click(); device.waitForWindowUpdate("", 2000); UiScrollable appViews = new UiScrollable(new UiSelector().scrollable(true)); appViews.setAsHorizontalList(); UiObject settingsApp = appViews.getChildByText(new UiSelector().className(TextView.class.getName()), appName); settingsApp.clickAndWaitForNewWindow(); assertTrue("Unable to detect app", settingsApp != null); } @Override public void setUp() throws RemoteException, UiObjectNotFoundException { this.runApp(appName); } @Override public void tearDown() throws RemoteException, UiObjectNotFoundException { //Empty for the moment } public void testUS1() { UiObject2 usernameLabel = device.findObject(By.clazz(TextView.class.getName()).text("Username")); assertTrue("Username label not found", usernameLabel != null); } }
基于Instrument的方便一点就是不需要remote debug的方式进行调试。并且做参数化之类的也方便了很多。
相关文章推荐
- Longest Consecutive Sequence
- MiniGUI文档参考手册 基于v1.6.10文本
- STL 之 deque的用法
- 完美激活Flash builder 4.7
- UVA-1252 Twenty Questions (状压DP)
- Rearrange a string so that all same characters become d distance away minDistance priority queue
- UUID 压缩为22位
- 【异常处理】Incorrect string value: '\xF0\x90\x8D\x83...' for column... Emoji表情字符过滤的Java实现
- iOS UI基础知识
- 1007. Maximum Subsequence Sum (25)
- treeviewhelper用法,找child的UIElement
- UIpanel之修改 clipping size 数值
- Xamarin.IOS UIAlertController 自定义字体
- UIButton
- [转载] ZooKeeper实现分布式队列Queue
- easy ui datagrid 增,删,改,查等基本操作
- ueditor编辑器 提交表单
- ue标签不见了,如何解决?
- iOS真机UI调试利器——Reveal
- 关于easyUI在子页面增加显示tabs的一个问题