uiautomator代码例子--java

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
//    }
}

猜你喜欢

转载自www.cnblogs.com/c-x-a/p/11599534.html