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 18
2、写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

原创粉丝点击