android studio测试--Uiautomator

安装Android Support Repository

这里写图片描述

最低版本

 minSdkVersion 18

添加Uiautomator

在build.gradle添加对Uiautomator的依赖

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.1.0'
    testCompile 'junit:junit:4.12'

    //uiautomator
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.0'
}

待测试功能

随便写了个demo
xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="app.test.myapplication.MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_marginStart="14dp"
        android:text="Button" />
</RelativeLayout>

Activity:

private int clicks=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final TextView textView= (TextView) findViewById(R.id.textView);

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView.setText("click: " + clicks);
                clicks++;
            }
        });
    }

测试

在androidTest下,添加测试类。
这里写图片描述

测试代码:

public class MainTest extends UiAutomatorTestCase {

    public void testMain() 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();

        getUiDevice().pressHome();

        Context context = InstrumentationRegistry.getContext();
        Intent launchIntent = new Intent();
        launchIntent.setComponent(new ComponentName("app.test.myapplication", "app.test.myapplication.MainActivity"));
        context.startActivity(launchIntent);

        UiObject main = new UiObject(new UiSelector().resourceId("app.test.myapplication:id/button"));
        main.click();
    }
}

相关的类:
这里写图片描述

异常:
这里写图片描述

官网–Uiautomator

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值