码迷,mamicode.com
首页 > 编程语言 > 详细

uiautomator代码例子--java

时间:2019-09-27 19:18:38      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:cal   命名   remote   interrupt   types   conf   const   text   depend   

package com.example.myapplication;

import android.app.Instrumentation;
import android.os.RemoteException;
import android.view.KeyEvent;
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;
import androidx.test.uiautomator.By;
import androidx.test.uiautomator.UiDevice;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.IOException;

@RunWith(AndroidJUnit4ClassRunner.class)
public class Ui2Test {
    public Instrumentation mInstrumentation;
    public UiDevice mUidevice;

    @Before
    public void setUp() {
        mInstrumentation = InstrumentationRegistry.getInstrumentation();
        mUidevice = UiDevice.getInstance(mInstrumentation);
    }
    @Test
    public void DemoTest() throws IOException, InterruptedException {
        //资源id的形式
        mUidevice.pressKeyCode(KeyEvent.KEYCODE_HOME);
        mUidevice.executeShellCommand("am start -n com.miui.calculator/.cal.CalculatorActivity");
        Thread.sleep(1000);
        mUidevice.findObject(By.res("com.miui.calculator:id/btn_5_s")).click();
        //文本的方式
        mUidevice.findObject(By.text("7")).click();
        //通过描述
        mUidevice.findObject(By.desc("减")).click();
        mUidevice.executeShellCommand("am force-stop com.miui.calculator"); //关闭应用,只传入packagename就好了。


        //可以通过链式同时选定多个条件
//        mUidevice.findObject(By.res("").checked(false)).click();
//        //焦点类
//        mUidevice.findObject(By.res("").focused(true)).setText("1234");

    }
//    @Test
//    public void calculatorTest() throws InterruptedException {
//        //滑动解锁,10个步长为50ms。
//        mUidevice.swipe(519,1505,519,306,10);
//        mUidevice.pressKeyCode(KeyEvent.KEYCODE_HOME);
//        Thread.sleep(2000); //等待2s
//    }
}

build.gradle的配置

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 18
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    //生成jar包
    task makeJar(type:Copy) {
        delete 'build/libs/network.jar'
        from('build/intermediates/packaged-classes/release/') //jar文件来源
        into('build/libs/') //生成路径
        include('classes.jar')
        rename('classes.jar','network.jar') //命名为network.jar
    }
    makeJar.dependsOn(build)
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    //UiAutomator Testing 添加测试依赖
    androidTestImplementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
}

uiautomator代码例子--java

标签:cal   命名   remote   interrupt   types   conf   const   text   depend   

原文地址:https://www.cnblogs.com/c-x-a/p/11599534.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!