文章目录
 
  
 
 
一、引言
 
- 描述:UI Automator 是一个界面测试框架,适用于整个系统上以及多个已安装应用间的跨应用功能界面测试。这里我将会拿QQ软件的说说模块进行测试。
- 知识点:Ui Automator
- 难度:初级
- 效果
  
二、了解(Android官方文档)
 
1、UiDevice 类
 
 
- 改变设备的旋转。
- 按硬件键,如“音量调高按钮”。
- 按返回、主屏幕或菜单按钮。
- 打开通知栏。
- 截取当前窗口的屏幕截图。
2、UI Automator API
 
- UiCollection:枚举容器的界面元素,目的是为了计数,或者按可见文本或内容说明属性来定位子元素。
- UiObject:表示设备上可见的界面元素。
- UiScrollable:支持搜索可滚动界面容器中的项目。
- UiSelector:表示对设备上的一个或多个目标界面元素的查询。
- Configurator:可让您设置用于运行 UI Automator 测试的关键参数。
3、UI Automator 查看器
 
 
三、使用
 
1、依赖
 
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
 
2、代码
 
 
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
    UiDevice device;
    @Before
    public void init() {
        
        device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        
        device.pressHome();
        
        String launcherPackage = device.getLauncherPackageName();
        device.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
                1000);
        
        Context context = ApplicationProvider.getApplicationContext();
        Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.tencent.mobileqq");
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        context.startActivity(intent);
        device.wait(Until.hasObject(By.pkg("com.tencent.mobileqq").depth(0)),0);;
    }
    @Test
    public void test() throws InterruptedException {
        
        device.findObject(By.text("动态")).click();  
        device.findObject(By.res("com.tencent.mobileqq","uae")).click();   
        Thread.sleep(200);  
        device.findObject(By.res("com.tencent.mobileqq","dsj")).click();
        Thread.sleep(200);
        device.findObject(By.res("com.tencent.mobileqq","hkl")).click();
        Thread.sleep(200);
        device.findObject(By.res("com.tencent.mobileqq","itv")).setText("云端new守夜人\nAndroid uiautomator 测试");
        device.findObject(By.res("com.tencent.mobileqq","ivTitleBtnRightText")).click();
    }
}