Android UiAutoMator自动化测试
来源:互联网 发布:php上传文件到服务器 编辑:程序博客网 时间:2024/05/16 10:42
1、在build.gradle中添加:
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.0'并且把minSdkVersion改为18:
minSdkVersion 182、写demo布局:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#005500" android:orientation="vertical"> <EditText android:id="@+id/input_1" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginTop="50dp" android:background="#ffffff"/> <TextView android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center_vertical" android:text="+" android:textColor="#ffffff"/> <EditText android:id="@+id/input_2" android:layout_width="match_parent" android:layout_height="50dp" android:background="#ffffff"/> <TextView android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center_vertical" android:text="=" android:textColor="#ffffff"/> <EditText android:id="@+id/result" android:layout_width="match_parent" android:layout_height="50dp" android:background="#ffffff"/></LinearLayout>3、新建测试文件,并继承UiAutomatorTestCase
代码:
public class UiTest extends UiAutomatorTestCase { public void testDemo() throws UiObjectNotFoundException { UiDevice.getInstance(getInstrumentation()).pressHome(); UiObject main = new UiObject(new UiSelector().description("MainActivity")); main.clickAndWaitForNewWindow(); UiObject input1 = new UiObject(new UiSelector().resourceId("com.ht.uitest:id/input_1")); input1.setText("1"); sleep(1000); UiObject input2 = new UiObject(new UiSelector().resourceId("com.ht.uitest:id/input_2")); input2.setText("2"); sleep(1000); UiObject result = new UiObject(new UiSelector().resourceId("com.ht.uitest:id/result")); result.setText("4"); sleep(1000); assertEquals("3", result.getText()); sleep(1000); }}
注意方法名必须以test开头,不能运行的时候会报找不到测试方法的异常。
大意是:
(1)返回home界面;
(2)打开名字为MainActivity的应用,注意,该应用必须放在第一页,不然会报找不到对象的异常:
android.support.test.uiautomator.UiObjectNotFoundException: UiSelector[DESCRIPTION=MainActivity]at android.support.test.uiautomator.UiObject.clickAndWaitForNewWindow(UiObject.java:452)at android.support.test.uiautomator.UiObject.clickAndWaitForNewWindow(UiObject.java:430)at com.ht.uitest.UiTest.testDemo(UiTest.java:19)at java.lang.reflect.Method.invoke(Native Method)(3)第一个输入框填写1,暂停1秒(便于我们肉眼观察);第二个输入框填写2,暂停1秒;结果输入框填写4,暂停1秒;最后判断结果,明显可以看到我们输入的结果有误,所以会报异常:
junit.framework.ComparisonFailure: expected:<[3]> but was:<[4]>at junit.framework.Assert.assertEquals(Assert.java:85)at junit.framework.Assert.assertEquals(Assert.java:91)at com.ht.uitest.UiTest.testDemo(UiTest.java:30)at java.lang.reflect.Method.invoke(Native Method)4、在测试类上右键 -> run
参考:
解放双手——Android自动化测试
在 Android studio 上运用 UI Automator 执行自动化测试
在Android Sudio中使用Uiautomator
阅读全文
0 0
- android uiautomator自动化测试
- UiAutomator android自动化测试
- Android-自动化测试UIAutomator
- Android自动化测试uiautomator
- Android UiAutoMator自动化测试
- Android Uiautomator 自动化测试
- Android Uiautomator 自动化测试
- Android自动化测试之UIAutomator
- Android自动化测试(UiAutomator)
- Android自动化测试工具uiautomator
- Python + Android + Uiautomator自动化测试
- Android自动化测试之UIAutomator
- Python + Android + Uiautomator自动化测试
- android uiautomator自动化测试记录
- Android自动化测试(UiAutomator)
- Android UiAutomator & UiAutomatorViewer 自动化测试
- Android自动化测试uiautomator入门
- [Android自动化测试]UiAutomator入门
- php发送邮件
- Node.js Express框架
- ActiveMQ消息传送机制以及ACK机制详解 AcitveMQ是作为一种消息存储和分发组件,涉及到client与broker端数据交互的方方面面,它不仅要担保消息的存储安全性,还要提供额外的
- Python爬虫入门六之Cookie的使用
- 嵌入式linux学习中重要的网址
- Android UiAutoMator自动化测试
- 8.3 证明STINGY SAT是NP完全问题
- BZOJ 1003: [ZJOI2006]物流运输
- java基础类型之间的转换
- swoole入门教程
- 2.5 Git Basics
- Mac系统下Tomcat端口号被占用,没法正常启动的问题。
- 站内信设计
- 关于java中正则的一个小部分