Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android studio測試--Uiautomator

android studio測試--Uiautomator

編輯:關於Android編程

安裝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:




    

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

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved