uiautomator2基本配置、核心定位元素、代码样例

一、打开build.gradle(app)添加依赖,后同步:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
//    模块添加依赖
    androidTestImplementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'

}

二、通过sdk/tooles/bin/uiautomatorviewer.bat打开工具,获取元素

三、核心定位元素

resource-id 资源id

text 文字属性,一般是显示在组件上的文字

content-desc 描述

checked 选择属性,常见开关、单元、复选的开与关的状态

focused 焦点属性,如输入框光标在闪动,说明焦点在输入框

四、搜索对象 BySelector

资源id:res(sourceName)

通过text:text(text)

通过描述定位:desc(desc)

focused(boolean isFocused)

查找资源id:

mUidevice.findObject(By.res('com.android.calculator2:id/digit_7')).click()

mUidevice.findObject(By.text('7')).click()

mUidevice.findObject(By.desc('减')).click()

输入文本:.setText('1234');

 

五、基本模拟动作

1.1、常见操作 类 UiObject2

点击:public boolean click()

滑动:public boolean longClick()

拖动:public void drag(Point dest)

mUidevice.findObject(By.res("com.xxxx:id/xxx")).drag(new Point(224,1213))

输入: public boolean setTest(String text)

1.2、常见设备操作 类 UiDevice

点击 click(int x,int y)

按键 pressKeyCode(int keyCode)

滑动 swipe(int startX,int startY,int endX,int endY,int steps) -----一个步长5毫秒

(1)public boolean pressSearch() ,点击查找功能键
(2)public boolean pressBack() ,点击后退键
(3)public boolean pressDelete() ,点击删除键
(4)public boolean pressEnter() ,点击回车键
(5)public boolean pressHome(),点击Home键
(6)public boolean pressMenu(),点击菜单键
(7)public boolean pressRecentApps(),点击在运行的APP键

(1)public boolean openNotification(),展开通知栏
(2)public boolean openQuickSettings(),展开快速设置栏

六、示例代码

package mk.com;

import android.app.Instrumentation;
import android.os.RemoteException;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.uiautomator.By;
import androidx.test.uiautomator.UiDevice;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
public class MKTest {
    public Instrumentation mlnstrumentation = InstrumentationRegistry.getInstrumentation();;
    public UiDevice ud = UiDevice.getInstance(mlnstrumentation);

    int h = ud.getDisplayHeight();
    int w = ud.getDisplayWidth();


    @Before
    public void setUp() {
        ud.pressBack();
        ud.pressBack();
        ud.pressBack();
        ud.pressBack();
    }

    @Test
    public void DemoTest() throws RemoteException, InterruptedException {

//        以上为套路代码
        ud.findObject(By.text("微信")).click();
        Thread.sleep(2000);
        System.out.println("========屏幕宽:"+w+",屏幕高:"+h);
//        滑动
//        Boolean s = ud.swipe(w/2,(new Double(h*0.25)).intValue(),w/2,(new Double(h*0.75)).intValue(),1);
//        点击查找图标
        ud.click((new Double(w*0.82)).intValue(),(new Double(h*0.06)).intValue());
        Thread.sleep(2000);
//        搜索二卡联通
        ud.findObject(By.text("搜索")).setText("二卡");
        Thread.sleep(2000);
//        点击出现的联系人第一个
        ud.click((new Double(w*0.5)).intValue(),(new Double(h*0.19)).intValue());
        Thread.sleep(2000);
//        点击输入
        ud.findObject(By.focused(true)).setText("晚安!");
        Thread.sleep(2000);
//        点击发送
        ud.findObject(By.text("发送")).click();
    }
}

 

 

 

 

 

 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值